diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/apps/AppResults.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/apps/AppResults.kt index 30d564b5..5e1a996d 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/apps/AppResults.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/apps/AppResults.kt @@ -205,7 +205,7 @@ fun LazyListScope.AppResults( if (showList) { ListResults( key = "apps", - items = apps.filter { it.user == profiles[selectedProfileIndex].userHandle }, + items = if (isProfileLocked) emptyList() else apps, before = before?.let { { it() } }, selectedIndex = selectedIndex, itemContent = { app, showDetails, index -> @@ -223,7 +223,7 @@ fun LazyListScope.AppResults( } else { GridResults( key = "apps", - items = apps.filter { it.user == profiles[selectedProfileIndex].userHandle }, + items = if (isProfileLocked) emptyList() else apps, before = before, itemContent = { GridItem( diff --git a/core/profiles/src/main/java/de/mm20/launcher2/profiles/ProfileManager.kt b/core/profiles/src/main/java/de/mm20/launcher2/profiles/ProfileManager.kt index 7ea66bb0..a692f0fe 100644 --- a/core/profiles/src/main/java/de/mm20/launcher2/profiles/ProfileManager.kt +++ b/core/profiles/src/main/java/de/mm20/launcher2/profiles/ProfileManager.kt @@ -42,20 +42,28 @@ class ProfileManager( private val scope = CoroutineScope(Dispatchers.Default + Job()) - private val profileStates: MutableStateFlow> = - MutableStateFlow(emptyList()) + /** + * An array of exactly 3 profiles with their states. + * - Index 0: Personal profile + * - Index 1: Work profile + * - Index 2: Private profile + * + * Profiles that don't exist are null. + */ + private val profileStates: MutableStateFlow> = + MutableStateFlow(arrayOf(null, null, null)) /** * List of profiles that are active and unlocked. */ val activeProfiles: Flow> = profileStates.map { it.mapNotNull { - if (it.state.locked) null else it.profile + if (it?.state?.locked != false) null else it.profile } }.shareIn(scope, SharingStarted.WhileSubscribed(), replay = 1) val profiles: Flow> = profileStates.map { - it.map { it.profile } + it.mapNotNull { it?.profile } }.shareIn(scope, SharingStarted.WhileSubscribed(), replay = 1) init { @@ -94,15 +102,24 @@ class ProfileManager( } } } + private suspend fun refreshProfiles() { mutex.withLock { - val profiles = mutableListOf() + val profiles = arrayOf(null, null, null) for (userHandle in launcherApps.profiles) { val serial = userManager.getSerialNumberForUser(userHandle) if (android.os.Build.MANUFACTURER == "samsung" && serial == 150L) continue // Hide Samsung Secure Folder - profiles.add( - ProfileWithState( + + val type = getProfileType(userHandle) + val index = when (type) { + Profile.Type.Personal -> 0 + Profile.Type.Work -> 1 + Profile.Type.Private -> 2 + } + + if (profiles[index] == null) { + profiles[index] = ProfileWithState( Profile( type = getProfileType(userHandle), userHandle = userHandle, @@ -110,7 +127,7 @@ class ProfileManager( ), getProfileState(userHandle), ) - ) + } } profileStates.value = profiles } @@ -118,13 +135,13 @@ class ProfileManager( fun getProfile(userHandle: UserHandle): Flow { return profileStates.map { - it.find { it.profile.userHandle == userHandle }?.profile + it.find { it?.profile?.userHandle == userHandle }?.profile } } fun getProfileState(profile: Profile?): Flow { return profileStates.map { profiles -> - profiles.find { it.profile == profile }?.state + profiles.find { it?.profile == profile }?.state } }