From 7dbe9d4d6fd6a6b0f74a5b06953bea6c493250c5 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Mon, 20 Feb 2023 00:08:35 +0100 Subject: [PATCH] Migrate SearchVM from LiveData to Compose states --- .../ui/assistant/AssistantScaffold.kt | 4 +-- .../launcher2/ui/launcher/PagerScaffold.kt | 4 +-- .../launcher2/ui/launcher/PullDownScaffold.kt | 4 +-- .../ui/launcher/search/SearchColumn.kt | 26 +++++++------- .../launcher2/ui/launcher/search/SearchVM.kt | 35 +++++++++---------- .../launcher/searchbar/LauncherSearchBar.kt | 2 +- 6 files changed, 36 insertions(+), 39 deletions(-) diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/assistant/AssistantScaffold.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/assistant/AssistantScaffold.kt index 15bc47e9..797f10a1 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/assistant/AssistantScaffold.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/assistant/AssistantScaffold.kt @@ -154,7 +154,7 @@ fun AssistantScaffold( } val searchVM: SearchVM = viewModel() - val actions by searchVM.searchActionResults.observeAsState(emptyList()) + val actions by searchVM.searchActionResults val webSearchPadding by animateDpAsState( if (actions.isEmpty()) 0.dp else 48.dp ) @@ -174,7 +174,7 @@ fun AssistantScaffold( state = searchState ) - val value by searchVM.searchQuery.observeAsState("") + val value by searchVM.searchQuery val searchBarColor by viewModel.searchBarColor.observeAsState(Settings.SearchBarSettings.SearchBarColors.Auto) val searchBarStyle by viewModel.searchBarStyle.observeAsState(Settings.SearchBarSettings.SearchBarStyle.Transparent) diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/PagerScaffold.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/PagerScaffold.kt index 71851a6e..2b3c6100 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/PagerScaffold.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/PagerScaffold.kt @@ -100,7 +100,7 @@ fun PagerScaffold( val isSearchOpen by viewModel.isSearchOpen.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 searchState = rememberLazyListState() @@ -479,7 +479,7 @@ fun PagerScaffold( (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 searchBarStyle by viewModel.searchBarStyle.observeAsState(SearchBarStyle.Transparent) diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/PullDownScaffold.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/PullDownScaffold.kt index 1064cfa3..370e80df 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/PullDownScaffold.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/PullDownScaffold.kt @@ -98,7 +98,7 @@ fun PullDownScaffold( val density = LocalDensity.current - val actions by searchVM.searchActionResults.observeAsState(emptyList()) + val actions by searchVM.searchActionResults val isSearchOpen by viewModel.isSearchOpen.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) ) - val value by searchVM.searchQuery.observeAsState("") + val value by searchVM.searchQuery val searchBarColor by viewModel.searchBarColor.observeAsState(Settings.SearchBarSettings.SearchBarColors.Auto) val searchBarStyle by viewModel.searchBarStyle.observeAsState(Settings.SearchBarSettings.SearchBarStyle.Transparent) diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/SearchColumn.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/SearchColumn.kt index 60a8da4b..1ac361d8 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/SearchColumn.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/SearchColumn.kt @@ -80,23 +80,23 @@ fun SearchColumn( 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 apps by viewModel.appResults.observeAsState(emptyList()) - val workApps by viewModel.workAppResults.observeAsState(emptyList()) - val appShortcuts by viewModel.appShortcutResults.observeAsState(emptyList()) - val contacts by viewModel.contactResults.observeAsState(emptyList()) - val files by viewModel.fileResults.observeAsState(emptyList()) - val events by viewModel.calendarResults.observeAsState(emptyList()) - val unitConverter by viewModel.unitConverterResults.observeAsState(emptyList()) - val calculator by viewModel.calculatorResults.observeAsState(emptyList()) - val wikipedia by viewModel.wikipediaResults.observeAsState(emptyList()) - val website by viewModel.websiteResults.observeAsState(emptyList()) - val hiddenResults by viewModel.hiddenResults.observeAsState(emptyList()) + val apps by viewModel.appResults + val workApps by viewModel.workAppResults + val appShortcuts by viewModel.appShortcutResults + val contacts by viewModel.contactResults + val files by viewModel.fileResults + val events by viewModel.calendarResults + val unitConverter by viewModel.unitConverterResults + val calculator by viewModel.calculatorResults + val wikipedia by viewModel.wikipediaResults + val website by viewModel.websiteResults + val hiddenResults by viewModel.hiddenResults 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 missingShortcutsPermission by viewModel.missingAppShortcutPermission.collectAsState(false) diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/SearchVM.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/SearchVM.kt index 52a59f2d..dd7e22c9 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/SearchVM.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/SearchVM.kt @@ -1,10 +1,8 @@ package de.mm20.launcher2.ui.launcher.search import android.content.Context -import android.util.Log import androidx.appcompat.app.AppCompatActivity import androidx.compose.runtime.mutableStateOf -import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import de.mm20.launcher2.favorites.FavoritesRepository @@ -27,7 +25,6 @@ import de.mm20.launcher2.searchactions.actions.SearchAction import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job -import kotlinx.coroutines.ensureActive import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.combine @@ -51,25 +48,25 @@ class SearchVM : ViewModel(), KoinComponent { private val searchService: SearchService by inject() - val searchQuery = MutableLiveData("") - val isSearchEmpty = MutableLiveData(true) + val searchQuery = mutableStateOf("") + val isSearchEmpty = mutableStateOf(true) - val appResults = MutableLiveData>(emptyList()) - val workAppResults = MutableLiveData>(emptyList()) - val appShortcutResults = MutableLiveData>(emptyList()) - val fileResults = MutableLiveData>(emptyList()) - val contactResults = MutableLiveData>(emptyList()) - val calendarResults = MutableLiveData>(emptyList()) - val wikipediaResults = MutableLiveData>(emptyList()) - val websiteResults = MutableLiveData>(emptyList()) - val calculatorResults = MutableLiveData>(emptyList()) - val unitConverterResults = MutableLiveData>(emptyList()) - val searchActionResults = MutableLiveData>(emptyList()) + val appResults = mutableStateOf>(emptyList()) + val workAppResults = mutableStateOf>(emptyList()) + val appShortcutResults = mutableStateOf>(emptyList()) + val fileResults = mutableStateOf>(emptyList()) + val contactResults = mutableStateOf>(emptyList()) + val calendarResults = mutableStateOf>(emptyList()) + val wikipediaResults = mutableStateOf>(emptyList()) + val websiteResults = mutableStateOf>(emptyList()) + val calculatorResults = mutableStateOf>(emptyList()) + val unitConverterResults = mutableStateOf>(emptyList()) + val searchActionResults = mutableStateOf>(emptyList()) - val hiddenResults = MutableLiveData>(emptyList()) + val hiddenResults = mutableStateOf>(emptyList()) val favoritesEnabled = dataStore.data.map { it.favorites.enabled } - val hideFavorites = MutableLiveData(false) + val hideFavorites = mutableStateOf(false) private val hiddenItemKeys = favoritesRepository .getHiddenItemKeys() @@ -104,7 +101,7 @@ class SearchVM : ViewModel(), KoinComponent { searchJob?.cancel() } catch (_: CancellationException) { } - hideFavorites.postValue(query.isNotEmpty()) + hideFavorites.value = query.isNotEmpty() searchJob = viewModelScope.launch { dataStore.data.collectLatest { searchService.search( diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/searchbar/LauncherSearchBar.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/searchbar/LauncherSearchBar.kt index 2ac74def..ecc3a729 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/searchbar/LauncherSearchBar.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/searchbar/LauncherSearchBar.kt @@ -49,7 +49,7 @@ fun LauncherSearchBar( val searchVM: SearchVM = viewModel() - val hiddenItems by searchVM.hiddenResults.observeAsState(emptyList()) + val hiddenItems by searchVM.hiddenResults LaunchedEffect(focused) { if (focused) focusRequester.requestFocus()