Migrate SearchVM from LiveData to Compose states
This commit is contained in:
parent
e5fe3a8816
commit
7dbe9d4d6f
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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<List<LauncherApp>>(emptyList())
|
||||
val workAppResults = MutableLiveData<List<LauncherApp>>(emptyList())
|
||||
val appShortcutResults = MutableLiveData<List<AppShortcut>>(emptyList())
|
||||
val fileResults = MutableLiveData<List<File>>(emptyList())
|
||||
val contactResults = MutableLiveData<List<Contact>>(emptyList())
|
||||
val calendarResults = MutableLiveData<List<CalendarEvent>>(emptyList())
|
||||
val wikipediaResults = MutableLiveData<List<Wikipedia>>(emptyList())
|
||||
val websiteResults = MutableLiveData<List<Website>>(emptyList())
|
||||
val calculatorResults = MutableLiveData<List<Calculator>>(emptyList())
|
||||
val unitConverterResults = MutableLiveData<List<UnitConverter>>(emptyList())
|
||||
val searchActionResults = MutableLiveData<List<SearchAction>>(emptyList())
|
||||
val appResults = mutableStateOf<List<LauncherApp>>(emptyList())
|
||||
val workAppResults = mutableStateOf<List<LauncherApp>>(emptyList())
|
||||
val appShortcutResults = mutableStateOf<List<AppShortcut>>(emptyList())
|
||||
val fileResults = mutableStateOf<List<File>>(emptyList())
|
||||
val contactResults = mutableStateOf<List<Contact>>(emptyList())
|
||||
val calendarResults = mutableStateOf<List<CalendarEvent>>(emptyList())
|
||||
val wikipediaResults = mutableStateOf<List<Wikipedia>>(emptyList())
|
||||
val websiteResults = mutableStateOf<List<Website>>(emptyList())
|
||||
val calculatorResults = mutableStateOf<List<Calculator>>(emptyList())
|
||||
val unitConverterResults = mutableStateOf<List<UnitConverter>>(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 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(
|
||||
|
||||
@ -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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user