Migrate SearchVM from LiveData to Compose states

This commit is contained in:
MM20 2023-02-20 00:08:35 +01:00
parent e5fe3a8816
commit 7dbe9d4d6f
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
6 changed files with 36 additions and 39 deletions

View File

@ -154,7 +154,7 @@ fun AssistantScaffold(
} }
val searchVM: SearchVM = viewModel() val searchVM: SearchVM = viewModel()
val actions by searchVM.searchActionResults.observeAsState(emptyList()) val actions by searchVM.searchActionResults
val webSearchPadding by animateDpAsState( val webSearchPadding by animateDpAsState(
if (actions.isEmpty()) 0.dp else 48.dp if (actions.isEmpty()) 0.dp else 48.dp
) )
@ -174,7 +174,7 @@ fun AssistantScaffold(
state = searchState state = searchState
) )
val value by searchVM.searchQuery.observeAsState("") val value by searchVM.searchQuery
val searchBarColor by viewModel.searchBarColor.observeAsState(Settings.SearchBarSettings.SearchBarColors.Auto) val searchBarColor by viewModel.searchBarColor.observeAsState(Settings.SearchBarSettings.SearchBarColors.Auto)
val searchBarStyle by viewModel.searchBarStyle.observeAsState(Settings.SearchBarSettings.SearchBarStyle.Transparent) val searchBarStyle by viewModel.searchBarStyle.observeAsState(Settings.SearchBarSettings.SearchBarStyle.Transparent)

View File

@ -100,7 +100,7 @@ fun PagerScaffold(
val isSearchOpen by viewModel.isSearchOpen.observeAsState(false) val isSearchOpen by viewModel.isSearchOpen.observeAsState(false)
val isWidgetEditMode by viewModel.isWidgetEditMode.observeAsState(false) val isWidgetEditMode by viewModel.isWidgetEditMode.observeAsState(false)
val actions by searchVM.searchActionResults.observeAsState(emptyList()) val actions by searchVM.searchActionResults
val widgetsScrollState = rememberScrollState() val widgetsScrollState = rememberScrollState()
val searchState = rememberLazyListState() val searchState = rememberLazyListState()
@ -479,7 +479,7 @@ fun PagerScaffold(
(if (isWidgetEditMode) 128.dp else 0.dp) * (if (bottomSearchBar) 1 else -1) (if (isWidgetEditMode) 128.dp else 0.dp) * (if (bottomSearchBar) 1 else -1)
) )
val value by searchVM.searchQuery.observeAsState("") val value by searchVM.searchQuery
val searchBarColor by viewModel.searchBarColor.observeAsState(SearchBarColors.Auto) val searchBarColor by viewModel.searchBarColor.observeAsState(SearchBarColors.Auto)
val searchBarStyle by viewModel.searchBarStyle.observeAsState(SearchBarStyle.Transparent) val searchBarStyle by viewModel.searchBarStyle.observeAsState(SearchBarStyle.Transparent)

View File

@ -98,7 +98,7 @@ fun PullDownScaffold(
val density = LocalDensity.current val density = LocalDensity.current
val actions by searchVM.searchActionResults.observeAsState(emptyList()) val actions by searchVM.searchActionResults
val isSearchOpen by viewModel.isSearchOpen.observeAsState(false) val isSearchOpen by viewModel.isSearchOpen.observeAsState(false)
val isWidgetEditMode by viewModel.isWidgetEditMode.observeAsState(false) val isWidgetEditMode by viewModel.isWidgetEditMode.observeAsState(false)
@ -520,7 +520,7 @@ fun PullDownScaffold(
(if (isWidgetEditMode) 128.dp else 0.dp) * (if (bottomSearchBar) 1 else -1) (if (isWidgetEditMode) 128.dp else 0.dp) * (if (bottomSearchBar) 1 else -1)
) )
val value by searchVM.searchQuery.observeAsState("") val value by searchVM.searchQuery
val searchBarColor by viewModel.searchBarColor.observeAsState(Settings.SearchBarSettings.SearchBarColors.Auto) val searchBarColor by viewModel.searchBarColor.observeAsState(Settings.SearchBarSettings.SearchBarColors.Auto)
val searchBarStyle by viewModel.searchBarStyle.observeAsState(Settings.SearchBarSettings.SearchBarStyle.Transparent) val searchBarStyle by viewModel.searchBarStyle.observeAsState(Settings.SearchBarSettings.SearchBarStyle.Transparent)

View File

@ -80,23 +80,23 @@ fun SearchColumn(
var showWorkProfileApps by remember { mutableStateOf(false) } var showWorkProfileApps by remember { mutableStateOf(false) }
val hideFavs by viewModel.hideFavorites.observeAsState(true) val hideFavs by viewModel.hideFavorites
val favoritesEnabled by viewModel.favoritesEnabled.collectAsState(false) val favoritesEnabled by viewModel.favoritesEnabled.collectAsState(false)
val apps by viewModel.appResults.observeAsState(emptyList()) val apps by viewModel.appResults
val workApps by viewModel.workAppResults.observeAsState(emptyList()) val workApps by viewModel.workAppResults
val appShortcuts by viewModel.appShortcutResults.observeAsState(emptyList()) val appShortcuts by viewModel.appShortcutResults
val contacts by viewModel.contactResults.observeAsState(emptyList()) val contacts by viewModel.contactResults
val files by viewModel.fileResults.observeAsState(emptyList()) val files by viewModel.fileResults
val events by viewModel.calendarResults.observeAsState(emptyList()) val events by viewModel.calendarResults
val unitConverter by viewModel.unitConverterResults.observeAsState(emptyList()) val unitConverter by viewModel.unitConverterResults
val calculator by viewModel.calculatorResults.observeAsState(emptyList()) val calculator by viewModel.calculatorResults
val wikipedia by viewModel.wikipediaResults.observeAsState(emptyList()) val wikipedia by viewModel.wikipediaResults
val website by viewModel.websiteResults.observeAsState(emptyList()) val website by viewModel.websiteResults
val hiddenResults by viewModel.hiddenResults.observeAsState(emptyList()) val hiddenResults by viewModel.hiddenResults
val bestMatch by viewModel.bestMatch val bestMatch by viewModel.bestMatch
val isSearchEmpty by viewModel.isSearchEmpty.observeAsState(true) val isSearchEmpty by viewModel.isSearchEmpty
val missingCalendarPermission by viewModel.missingCalendarPermission.collectAsState(false) val missingCalendarPermission by viewModel.missingCalendarPermission.collectAsState(false)
val missingShortcutsPermission by viewModel.missingAppShortcutPermission.collectAsState(false) val missingShortcutsPermission by viewModel.missingAppShortcutPermission.collectAsState(false)

View File

@ -1,10 +1,8 @@
package de.mm20.launcher2.ui.launcher.search package de.mm20.launcher2.ui.launcher.search
import android.content.Context import android.content.Context
import android.util.Log
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import de.mm20.launcher2.favorites.FavoritesRepository import de.mm20.launcher2.favorites.FavoritesRepository
@ -27,7 +25,6 @@ import de.mm20.launcher2.searchactions.actions.SearchAction
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
@ -51,25 +48,25 @@ class SearchVM : ViewModel(), KoinComponent {
private val searchService: SearchService by inject() private val searchService: SearchService by inject()
val searchQuery = MutableLiveData("") val searchQuery = mutableStateOf("")
val isSearchEmpty = MutableLiveData(true) val isSearchEmpty = mutableStateOf(true)
val appResults = MutableLiveData<List<LauncherApp>>(emptyList()) val appResults = mutableStateOf<List<LauncherApp>>(emptyList())
val workAppResults = MutableLiveData<List<LauncherApp>>(emptyList()) val workAppResults = mutableStateOf<List<LauncherApp>>(emptyList())
val appShortcutResults = MutableLiveData<List<AppShortcut>>(emptyList()) val appShortcutResults = mutableStateOf<List<AppShortcut>>(emptyList())
val fileResults = MutableLiveData<List<File>>(emptyList()) val fileResults = mutableStateOf<List<File>>(emptyList())
val contactResults = MutableLiveData<List<Contact>>(emptyList()) val contactResults = mutableStateOf<List<Contact>>(emptyList())
val calendarResults = MutableLiveData<List<CalendarEvent>>(emptyList()) val calendarResults = mutableStateOf<List<CalendarEvent>>(emptyList())
val wikipediaResults = MutableLiveData<List<Wikipedia>>(emptyList()) val wikipediaResults = mutableStateOf<List<Wikipedia>>(emptyList())
val websiteResults = MutableLiveData<List<Website>>(emptyList()) val websiteResults = mutableStateOf<List<Website>>(emptyList())
val calculatorResults = MutableLiveData<List<Calculator>>(emptyList()) val calculatorResults = mutableStateOf<List<Calculator>>(emptyList())
val unitConverterResults = MutableLiveData<List<UnitConverter>>(emptyList()) val unitConverterResults = mutableStateOf<List<UnitConverter>>(emptyList())
val searchActionResults = MutableLiveData<List<SearchAction>>(emptyList()) val searchActionResults = mutableStateOf<List<SearchAction>>(emptyList())
val hiddenResults = MutableLiveData<List<SavableSearchable>>(emptyList()) val hiddenResults = mutableStateOf<List<SavableSearchable>>(emptyList())
val favoritesEnabled = dataStore.data.map { it.favorites.enabled } val favoritesEnabled = dataStore.data.map { it.favorites.enabled }
val hideFavorites = MutableLiveData(false) val hideFavorites = mutableStateOf(false)
private val hiddenItemKeys = favoritesRepository private val hiddenItemKeys = favoritesRepository
.getHiddenItemKeys() .getHiddenItemKeys()
@ -104,7 +101,7 @@ class SearchVM : ViewModel(), KoinComponent {
searchJob?.cancel() searchJob?.cancel()
} catch (_: CancellationException) { } catch (_: CancellationException) {
} }
hideFavorites.postValue(query.isNotEmpty()) hideFavorites.value = query.isNotEmpty()
searchJob = viewModelScope.launch { searchJob = viewModelScope.launch {
dataStore.data.collectLatest { dataStore.data.collectLatest {
searchService.search( searchService.search(

View File

@ -49,7 +49,7 @@ fun LauncherSearchBar(
val searchVM: SearchVM = viewModel() val searchVM: SearchVM = viewModel()
val hiddenItems by searchVM.hiddenResults.observeAsState(emptyList()) val hiddenItems by searchVM.hiddenResults
LaunchedEffect(focused) { LaunchedEffect(focused) {
if (focused) focusRequester.requestFocus() if (focused) focusRequester.requestFocus()