In some cases it can be helpful to retrieve a list of all currently personalized XignIn-Managers, e.g. for displaying purposes when the app supports multiple activations simultaneously.
For this purpose the SDK offers two different functions that allow retrieving this data. The first approach is to fetch the list of currently personalized XignIn-Managers directly, as shown below:
internal func listAllPersonalizedXignInManagers() {
// Retrieve the personalizer component that grants access on personalized managers
let personalizer: Personalizer = XignSdk.shared.personalizer
do {
// Retrieve a list of all manager entities that are currently personalized
let currentPersonalizations: Array<IdentityManagerEntity> =
try personalizer.getIdentityManagersSynchronous()
// Process the list of XignIn-Managers, e.g. display the
// names and corresponding URLs of each manager.
let managerNamesAndURLs: [(String, URL)] =
currentPersonalizations.map { entity -> (String, URL) in
(entity.name, entity.url)
}
YourImplementation.showListOfPersonalizedManagersWithURLs(managerNamesAndURLs)
} catch {
// Fetching the identity managers failed, handle errors
}
}
internal fun listAllPersonalizedXignInManagers() {
// Retrieve the personalizer component that grants access on personalized managers
val personalizer: Personalizer = XignSdk.shared.personalizer
try {
// Retrieve a list of all manager entities that are currently personalized
val currentPersonalizations: List<IdentityManagerEntity> =
personalizer.getIdentityManagersSynchronous()
// Process the list of XignIn-Managers, e.g. display the
// names and corresponding URLs of each manager.
val managerNamesAndURLs: List<Pair<String, URL>> =
currentPersonalizations.map { entity ->
Pair(entity.name, entity.url)
}
YourImplementation.showListOfPersonalizedManagersWithURLs(managerNamesAndURLs)
} catch (t: Throwable) {
// Fetching the identity managers failed, handle errors
}
}
The second approach to retrieving the current list of personalized XignIn-Managers is to register an observer that is notified whenever changes in the database occur. This approach allows for automatic updates and the corresponding function can safely be called from the UI thread. Beware though, that using callbacks always requires a careful handling of weak references to avoid any memory leaks or other issues.
internal func subscribeToPersonalizedXignInManagersList() {
// Retrieve the personalizer component that grants access on personalized managers
let personalizer: Personalizer = XignSdk.shared.personalizer
// Register callbacks for observing database changes.
let idmsCancelable: GRDB.DatabaseCancellable =
personalizer.observeIdentityManagers(
onError: { error in
// Failed to fetch list of managers from the database - handle errors
},
// Specify a callback for changes in the database. Beware of weak references.
onChange: { (currentPersonalizations: Array<IdentityManagerEntity>) in
// Process the list of XignIn-Managers, e.g. display the
// names and corresponding URLs of each manager.
let managerNamesAndURLs: [(String, URL)] =
currentPersonalizations.map { entity -> (String, URL) in
(entity.name, entity.url)
}
YourImplementation.showListOfPersonalizedManagersWithURLs(managerNamesAndURLs)
}
)
}
On Android a call to the function returns the list as a LiveData object from the
Android LiveData Jetpack library can be safely
called from the UI thread.
internal fun subscribeToPersonalizedXignInManagersList(
activity: AppCompatActivity
) {
// Retrieve the personalizer component that grants access on personalized managers
val personalizer: Personalizer = XignSdk.shared.personalizer
// Register callbacks for observing database changes.
val idmsAsLiveData: LiveData<List<IdentityManagerEntity>> =
personalizer.getIdentityManagersAsLiveData()
// Specify a callback for changes in the database. Beware of weak references.
idmsAsLiveData.observe(activity) { currentPersonalizations: List<IdentityManagerEntity> ->
// Process the list of XignIn-Managers, e.g. display the
// names and corresponding URLs of each manager.
val managerNamesAndURLs: List<Pair<String, URL>> =
currentPersonalizations.map { entity ->
Pair(entity.name, entity.url)
}
YourImplementation.showListOfPersonalizedManagersWithURLs(managerNamesAndURLs)
}
}