Start to separate typeahead search from the view model

This commit is contained in:
Jackson Harper 2023-04-19 13:01:22 +08:00
parent f3019b0a6e
commit 5261d054d4
4 changed files with 15 additions and 17 deletions

View file

@ -66,7 +66,6 @@ fun LibraryViewContent(libraryViewModel: LibraryViewModel, modifier: Modifier) {
)
val cardsData: List<SavedItemCardDataWithLabels> by libraryViewModel.itemsLiveData.observeAsState(listOf())
val searchedCardsData: List<SavedItemCardDataWithLabels> by libraryViewModel.searchItemsLiveData.observeAsState(listOf())
Box(
modifier = Modifier

View file

@ -97,7 +97,9 @@ class LibraryViewModel @Inject constructor(
if (text == "") {
searchItemsLiveData.value = listOf()
} else {
load(clearPreviousSearch = true)
viewModelScope.launch {
performTypeaheadSearch(true)
}
}
}
@ -133,12 +135,8 @@ class LibraryViewModel @Inject constructor(
loadInitialFilterValues()
viewModelScope.launch {
if (searchTextLiveData.value != "") {
performSearch(clearPreviousSearch)
} else {
syncItems()
loadUsingSearchAPI()
}
syncItems()
loadUsingSearchAPI()
}
}
@ -189,9 +187,7 @@ class LibraryViewModel @Inject constructor(
}
suspend fun handleFilterChanges() {
if (searchTextLiveData.value != "") {
performSearch(true)
} else if (appliedSortFilterLiveData.value != null && appliedFilterLiveData.value != null) {
if (appliedSortFilterLiveData.value != null && appliedFilterLiveData.value != null) {
itemsLiveDataInternal = dataService.libraryLiveData(appliedFilterLiveData.value!!, appliedSortFilterLiveData.value!!, activeLabelsLiveData.value ?: listOf())
itemsLiveData.removeSource(itemsLiveDataInternal)
itemsLiveData.addSource(itemsLiveDataInternal, itemsLiveData::setValue)
@ -237,7 +233,7 @@ class LibraryViewModel @Inject constructor(
}
}
private suspend fun performSearch(clearPreviousSearch: Boolean) {
private suspend fun performTypeaheadSearch(clearPreviousSearch: Boolean) {
if (clearPreviousSearch) {
cursor = null
}

View file

@ -66,17 +66,16 @@ fun SearchBar(
@Composable
fun SearchField(
searchText: String,
onSearch: () -> Unit,
onSearchTextChanged: (String) -> Unit,
navController: NavHostController,
) {
var showClearButton by remember { mutableStateOf(false) }
val keyboardController = LocalSoftwareKeyboardController.current
val focusRequester = remember { FocusRequester() }
TextField(
modifier = Modifier
.fillMaxWidth()
// .padding(vertical = 2.dp)
.onFocusChanged { focusState ->
showClearButton = (focusState.isFocused)
}
@ -115,9 +114,9 @@ fun SearchField(
},
maxLines = 1,
singleLine = true,
keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(onDone = {
keyboardController?.hide()
keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Search),
keyboardActions = KeyboardActions(onSearch = {
onSearch()
}),
)

View file

@ -49,6 +49,10 @@ fun SearchView(
Row {
SearchField(
searchText,
onSearch = {
libraryViewModel.loadUsingSearchAPI()
navController.popBackStack()
},
onSearchTextChanged = { libraryViewModel.updateSearchText(it) },
navController = navController
)