KeysInteractor

public class KeysInteractor

This class provides the APIs to perform most common interactions with keys. e.g Load user’s favorite keys, add / remove a favorite key, load all keys for a specific badge, reorder favorite keys for a badge etc.

  • User’s badges which have keys

    Declaration

    Swift

    public private(set) var badgesWithKeys: [Badge]
  • Initialize a KeysInteractor instance

    Declaration

    Swift

    public init()
  • Subscribe to the keys load event for the specified type of keys.

    Declaration

    Swift

    public func subscribe(to type: KeysType, with closure: @escaping ([(Badge, CollectionChange<[KeyContainer]>)]) -> Void)
    Parameters
    type

    The type of keys to subscribe to. User’s favorite keys or all keys for a specific badge

    with

    A closure taking in an array of tuple (Badge, CollectionChange) This closure will be called more than once. e.g badge was added or removed, the number of keys for a badge changed, etc.

  • Fetch the latest data for the specified type of keys.

    Declaration

    Swift

    public func reload(_ type: KeysType)
    Parameters
    type

    The type of keys to fetch from the server.

  • Trigger action for a specific Key in KeyContainer

    Declaration

    Swift

    public func useKey(_ keyContainer: KeyContainer, keyIndex: Int, _ completion: ((_ success: Bool, _ error: UsherErrorProtocol?) -> Void)?)
    Parameters
    keyContainer

    The KeyContainer from which the key is to be used

    keyIndex

    The index of the actual key in the KeyContainer object

    completion

    Closure that will be called on completion.

  • Get the KeyContainer for a specified badgeId and keyId. Returns nil if not present.

    Declaration

    Swift

    public func favoriteKey(withBadgeId badgeId: String, keyId: String) -> KeyContainer?
  • Check if a key is marked as a favorite key.

    Declaration

    Swift

    public func isFavoriteKey(_ key: KeyContainer, badge: Badge) -> Bool
  • Mark or Unmark a key as favorite.

    Declaration

    Swift

    public func toggleFavoriteStatus(forKey key: KeyContainer, badge: Badge, serverSync: Bool = false)
    Parameters
    key

    the key to be marked or unmarked as favorite

    badge

    the badge object associated with the key

    serverSync

    specifies if the change should be synced to server or not. Set this to false to do several changes locally and sync all changes in batch using syncFavoriteKeys(forBadge:)

  • Use this function to reorder user’s favorite keys. This function syncs the favorite keys to server. The closure in subscribe(to:, with:) will also be triggered upon reorder.

    Declaration

    Swift

    public func moveFavoriteKey(_ keyContainer: KeyContainer, from sourceIndex: Int, to destinationIndex: Int)
    Parameters
    keyContainer

    The key to be moved

    sourceIndex

    Current index of the key

    destinationIndex

    Destination index of the key.

  • Add a key to user’s favorite keys. This function syncs the favorite keys to server if serverSync is true.

    Declaration

    Swift

    public func addFavoriteKey(_ key: KeyContainer, serverSync: Bool = true, delayedServerSync: Bool = false)
    Parameters
    key

    the key to be marked as favorite

    serverSync

    specifies if the change should be synced to server or not. Set this to false to do several changes locally and sync all changes in batch using syncFavoriteKeys(forBadge:)

    delayedServerSync

    specifies if the sync should be delayed or not. If the sync is delayed, all added favorite keys actions will be grouped to be execute together

  • Remove a key from user’s favorite keys. This function syncs the favorite keys to server if serverSync is true.

    Declaration

    Swift

    public func deleteFavoriteKey(_ key: KeyContainer, serverSync: Bool = true, delayedServerSync: Bool = false)
    Parameters
    key

    the key to be marked as favorite

    serverSync

    specifies if the change should be synced to server or not. Set this to false to do several changes locally and sync all changes in batch using syncFavoriteKeys(forBadge:)

    delayedServerSync

    specifies if the sync should be delayed or not. If the sync is delayed, all delete favorite keys actions will be grouped to be execute together

  • Explicitly trigger server sync for favorite keys of a badge. This method will be executed only if there is not synced changes locally as a result of calling deleteFavoriteKey(:serverSync:), addFavoriteKey(:serverSync:) or toggleFavoriteStatus(forKey:badge:serverSync:) with serverSync false

    Remark

    Make sure to call this method before the application is killed or user manually reload keys or local changes will be lost.

    Declaration

    Swift

    public func syncFavoriteKeys(forBadge badge: Badge)
    Parameters
    badge

    The badge to sync changes for.