Skip to main content
Back to Android

OrganizationUnitSwitcher

The OrganizationUnitSwitcher composable shows the current organization unit in a dropdown field. Its menu lists the organization units the signed-in user can access, from ThunderIDClient.getAccessibleOrganizationUnits. Picking another one calls ThunderIDClient.switchOrganizationUnit, then reloads the list and refreshes ThunderIDState. To learn how a switch decides whether the user signs in again, see Switch Organizations.

OrganizationUnitSwitcher requires ThunderIDProvider in its ancestor composable hierarchy.

Usage​

kotlin
import dev.thunderid.android.api.account.OrganizationUnitScope
import dev.thunderid.compose.components.account.OrganizationUnitSwitcher
import dev.thunderid.compose.components.auth.guards.SignedIn

@Composable
fun Header() {
SignedIn {
OrganizationUnitSwitcher(
modifier = Modifier.fillMaxWidth(),
scope = OrganizationUnitScope.SIBLINGS
)
}
}

Parameters​

ParameterTypeRequiredDescription
modifierModifier❌Compose modifier applied to the switcher container.
scopeOrganizationUnitScope?❌Narrows the list. OrganizationUnitScope.SIBLINGS lists the accessible organization units that share the parent of the current organization unit. Defaults to null, which lists every accessible organization unit.
limitInt❌Page size of the list. Defaults to 10.
switchOptionsSwitchOrganizationUnitOptions❌Options passed to switchOrganizationUnit. Set browser when the application signs in through redirects or a backend. Defaults to SwitchOrganizationUnitOptions().
onSelect((AccessibleOrganizationUnit) -> Unit)?❌Takes over a pick. The switcher calls it with the organization unit instead of switching.
onSwitched(() -> Unit)?❌Called after a switch completes and ThunderIDState has been refreshed.
onSignInRequired((OrganizationUnitSwitchResult.SignInRequired) -> Unit)?❌Called when an app-native switch needs the user, with the flow to drive.
content(@Composable (OrganizationUnitSwitcherState) -> Unit)?❌Replaces the built-in dropdown. See Custom Rendering.

Example: Switch Through a Custom Tab​

With no browser, a switch that a device secret cannot complete runs the application's app-native AUTHENTICATION flow, which needs applicationId in the configuration. With a backend, a switch that needs the user fails. To sign in to the organization unit in a Custom Tab in either case, pass customTabBrowser(context):

kotlin
import androidx.compose.ui.platform.LocalContext
import dev.thunderid.android.api.auth.SwitchOrganizationUnitOptions
import dev.thunderid.android.api.auth.customTabBrowser
import dev.thunderid.compose.components.account.OrganizationUnitSwitcher

@Composable
fun Header() {
val context = LocalContext.current

OrganizationUnitSwitcher(
switchOptions = SwitchOrganizationUnitOptions(browser = customTabBrowser(context))
)
}

The host Activity forwards the redirect to FederatedAuthSession.onRedirect, the same setup as federated sign-in.

Behavior​

  • The field shows the current organization unit's name. When no name is known, it shows the loading text while the list loads, and the placeholder otherwise.
  • The menu lists each organization unit by its name, or its handle when it has no name. The handle appears as secondary text when it differs from the name.
  • A check mark marks the current organization unit. Picking it only closes the menu.
  • When the user can access more organization units than limit, the menu ends with a pager: Previous, the range on display, and Next.
  • During a switch, the field shows the switching text with a progress indicator, and the menu does not open.
  • A failed load or switch shows a translated message below the field, never the server's own text. When an app-native switch needs the user, the switcher shows a message asking the user to sign in again and calls onSignInRequired.
  • The switcher reloads the list when the signed-in user changes.

Custom Rendering​

Pass content to replace the built-in dropdown. The switcher keeps loading, paging, and switching, and passes an OrganizationUnitSwitcherState to your lambda.

kotlin
import dev.thunderid.compose.components.account.OrganizationUnitSwitcher

@Composable
fun OrganizationUnitList() {
OrganizationUnitSwitcher { state ->
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text("Working in ${state.currentName ?: "an unknown organization unit"}")

state.organizationUnits.forEach { unit ->
TextButton(
onClick = { state.onSwitch(unit) },
enabled = state.switchingId == null && unit.id != state.current
) {
Text(unit.name ?: unit.handle ?: unit.id)
}
}

state.error?.let { Text(it, color = MaterialTheme.colorScheme.error) }
}
}
}

OrganizationUnitSwitcherState Properties​

PropertyTypeDescription
organizationUnitsList<AccessibleOrganizationUnit>The page of organization units the signed-in user can access.
currentString?ID of the organization unit the session is in. It might not be on this page.
currentNameString?Name of the current organization unit, from the page, or else from the session. null when unknown.
isLoadingBooleantrue while the list loads.
errorString?Translated message for the last failed load or switch.
limitIntPage size.
offsetIntThe 0-based index of the page's first organization unit.
totalResultsIntNumber of organization units the user can access, across all pages.
onPageChange(Int) -> UnitLoads the page that starts at the given offset.
switchingIdString?ID of the organization unit a switch is in progress to. null when no switch is running.
onSwitch(AccessibleOrganizationUnit) -> UnitSwitches to the organization unit, or passes it to onSelect when set. In OrganizationUnitSwitcher, it does nothing for the current organization unit or while another switch is running.

Customization with BaseOrganizationUnitSwitcher​

BaseOrganizationUnitSwitcher draws the same dropdown from the data you pass. It holds no SDK state: it loads nothing and switches nothing, and calls onSwitch when the user picks another organization unit. It reads its strings from LocalThunderID, so it also requires ThunderIDProvider in its ancestor composable hierarchy.

kotlin
import dev.thunderid.android.api.account.AccessibleOrganizationUnit
import dev.thunderid.compose.components.account.BaseOrganizationUnitSwitcher

@Composable
fun TeamSwitcher(
units: List<AccessibleOrganizationUnit>,
currentId: String?,
onPick: (AccessibleOrganizationUnit) -> Unit
) {
BaseOrganizationUnitSwitcher(
organizationUnits = units,
current = currentId,
onSwitch = onPick
)
}

BaseOrganizationUnitSwitcher Parameters​

ParameterTypeRequiredDescription
organizationUnitsList<AccessibleOrganizationUnit>✅Organization units the menu lists.
currentString?✅ID of the current organization unit.
onSwitch(AccessibleOrganizationUnit) -> Unit✅Called with the organization unit when the user picks one other than the current one.
modifierModifier❌Compose modifier applied to the switcher container.
isLoadingBoolean❌Shows the loading state. Defaults to false.
errorString?❌Message shown below the field. Defaults to null.
limitInt❌Page size. Defaults to organizationUnits.size.
offsetInt❌The 0-based index of the page's first organization unit. Defaults to 0.
totalResultsInt❌Number of organization units across all pages. The menu shows the pager when it is greater than limit. Defaults to organizationUnits.size.
onPageChange(Int) -> Unit❌Called with the offset of the page to load.
switchingIdString?❌ID of the organization unit a switch is in progress to. Defaults to null.
currentNameString?❌Names the current organization unit when organizationUnits does not list it. Defaults to null.
content(@Composable (OrganizationUnitSwitcherState) -> Unit)?❌Replaces the built-in dropdown.

Translations​

The switcher reads every string from the translation bundle. To override a string, pass a ThunderIDI18n with your own bundle to ThunderIDProvider:

kotlin
import dev.thunderid.compose.ThunderIDProvider
import dev.thunderid.compose.i18n.ThunderIDI18n

ThunderIDProvider(
config = config,
i18n = ThunderIDI18n(
bundles = mapOf(
"en-US" to mapOf(
"organizationUnitSwitcher.label" to "Workspace",
"organizationUnitSwitcher.placeholder" to "Select a workspace"
)
)
)
) {
AppContent()
}
KeyDefault (en-US)Description
organizationUnitSwitcher.labelOrganizationLabel of the field. Set it to an empty string to hide the label.
organizationUnitSwitcher.placeholderSelect an organizationField text when the current organization unit has no known name.
organizationUnitSwitcher.switchingSwitching to {name}…Field text during a switch. {name} is the organization unit being switched to.
organizationUnitSwitcher.loadingLoading organization units…Field and menu text while the list loads.
organizationUnitSwitcher.emptyNo organization unitsMenu text when the list is empty.
organizationUnitSwitcher.unnamedUnnamed organization unitName of an organization unit that has neither a name nor a handle.
organizationUnitSwitcher.previousPreviousPager button.
organizationUnitSwitcher.nextNextPager button.
organizationUnitSwitcher.page{start} to {end} of {total}Pager range.
organizationUnitSwitcher.errors.loadCould not load your organization units. Please try again.Shown when the list fails to load.
organizationUnitSwitcher.errors.switchCould not switch to this organization unit. Please try again.Shown when a switch fails.
organizationUnitSwitcher.errors.notAccessibleThis organization unit is not available to this application.Shown when a load or switch fails with ORGANIZATION_UNIT_UNAVAILABLE.
organizationUnitSwitcher.errors.signInRequiredSign in again to switch to this organization unit.Shown when an app-native switch needs the user.

Explore with AI

ThunderID LogoThunderID Logo

Product

DocsAPIsSDKs
© Copyright Linux Foundation Europe.For web site terms of use, trademark policy and other project policies please see https://linuxfoundation.eu/en/policies.