(refactor) refactor ProfileManager class

This commit is contained in:
MM20
2026-07-19 18:27:37 +02:00
parent 10516ef9a9
commit 3f4deead1b
7 changed files with 77 additions and 97 deletions

View File

@@ -16,6 +16,7 @@ import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
@@ -80,7 +81,7 @@ fun SearchColumn(
val apps = viewModel.appResults
val workApps = viewModel.workAppResults
val privateApps = viewModel.privateSpaceAppResults
val profiles by viewModel.visibleProfiles.collectAsState(emptyList())
val profiles by viewModel.profiles.collectAsState(emptyList())
val profileStates by viewModel.profileStates.collectAsState(emptyList())
val appShortcuts = viewModel.appShortcutResults
@@ -191,7 +192,7 @@ fun SearchColumn(
},
highlightedItem = bestMatch as? Application,
profiles = profiles,
selectedProfileIndex = selectedAppProfileIndex,
selectedProfileIndex = selectedAppProfileIndex.takeIf { it in profiles.indices } ?: 0,
onProfileSelected = {
selectedAppProfileIndex = it
onHideKeyboard()

View File

@@ -83,20 +83,12 @@ class SearchVM : ViewModel(), KoinComponent {
val expandedCategory = mutableStateOf<SearchCategory?>(null)
val profiles = profileManager.profiles.shareIn(
viewModelScope,
SharingStarted.WhileSubscribed(),
replay = 1
)
val profiles = profileManager.profiles
val profileStates = profiles.flatMapLatest {
combine(it.map { profileManager.getProfileState(it) }) {
it.toList()
}
}.shareIn(viewModelScope, SharingStarted.WhileSubscribed(), replay = 1)
val visibleProfiles = combine(profiles, profileStates) { profs, states ->
profs.filterIndexed { i, _ -> states.getOrNull(i)?.hidden != true }
}.shareIn(viewModelScope, SharingStarted.WhileSubscribed(), replay = 1)
}
val hasProfilesPermission = permissionsManager.hasPermission(PermissionGroup.ManageProfiles)
@@ -152,24 +144,6 @@ class SearchVM : ViewModel(), KoinComponent {
init {
search("", forceRestart = true)
/*
* Handle clearing the search query when the user changes the private space
* lock from the search action chip
*/
viewModelScope.launch {
var prevPrivateLocked: Boolean? = null
combine(profiles, profileStates) { profiles, states -> profiles to states }
.collect { (profiles, states) ->
val privateIdx = profiles.indexOfFirst { it.type == Profile.Type.Private }
val isLocked = states.getOrNull(privateIdx)?.locked
if (prevPrivateLocked != null && isLocked != null && prevPrivateLocked != isLocked && searchQuery.value.isNotEmpty()) {
search("")
if (!isLocked) selectedAppProfileIndex.intValue = privateIdx
}
prevPrivateLocked = isLocked
}
}
}
fun launchBestMatchOrAction(context: Context) {