fix(notelist): search icon now actually opens the search bar
In v0.2 the search-active flag lived on a separate StateFlow that the composable never observed: 'if (state.isSearchActive)' in NoteListScreen was always false, so tapping the search icon toggled the internal flag but never updated the UI. Refactor NoteListViewModel.uiState to use combine() over (notes, isSearchActive, rawQuery) so the flag and the current query are folded into the same emitted NoteListUiState. The composable already reads state.isSearchActive, so no changes are needed there. Result: tapping the search icon shows the search field; typing filters the list via the existing ObjectBox 'contains' query; tapping again closes.
This commit is contained in:
1 parent
5e1b77bedf
commit
84900838f7
1 file changed
+25
-19
@@ -11,7 +11,7 @@ import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.debounce
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
@@ -24,6 +24,10 @@ import org.incido.domain.repository.NoteRepository
|
||||
* Immutable UI state for [NoteListScreen]. A loading flag plus the
|
||||
* current snapshot of notes; an empty list with [isLoading] == false
|
||||
* indicates the empty-state UI should be shown (F6).
|
||||
*
|
||||
* [isSearchActive] / [searchQuery] are now properly folded into the same
|
||||
* uiState flow (v0.2.1 fix — they used to live on a separate StateFlow
|
||||
* that the composable never observed, making the search icon a no-op).
|
||||
*/
|
||||
data class NoteListUiState(
|
||||
val notes: List<Note> = emptyList(),
|
||||
@@ -42,9 +46,15 @@ class NoteListViewModel(
|
||||
|
||||
private val rawQuery = MutableStateFlow("")
|
||||
private val _isSearchActive = MutableStateFlow(false)
|
||||
val isSearchActive: StateFlow<Boolean> = _isSearchActive.asStateFlow()
|
||||
|
||||
val uiState: StateFlow<NoteListUiState> =
|
||||
/**
|
||||
* Combines the filtered notes flow, the search-active flag, and the
|
||||
* raw query into a single [NoteListUiState] that the composable can
|
||||
* `collectAsStateWithLifecycle`. All three pieces of state are
|
||||
* updated together so `if (state.isSearchActive)` in the screen
|
||||
* actually reflects the icon's toggled value.
|
||||
*/
|
||||
val uiState: StateFlow<NoteListUiState> = combine(
|
||||
rawQuery
|
||||
.debounce(150)
|
||||
.flatMapLatest { q ->
|
||||
@@ -53,25 +63,21 @@ class NoteListViewModel(
|
||||
} else {
|
||||
repository.search(q)
|
||||
}
|
||||
}
|
||||
.stateIn(
|
||||
},
|
||||
_isSearchActive,
|
||||
rawQuery,
|
||||
) { notes, searchActive, q ->
|
||||
NoteListUiState(
|
||||
notes = notes,
|
||||
isLoading = false,
|
||||
searchQuery = q,
|
||||
isSearchActive = searchActive,
|
||||
)
|
||||
}.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(5_000),
|
||||
initialValue = emptyList(),
|
||||
initialValue = NoteListUiState(),
|
||||
)
|
||||
.let { notesFlow ->
|
||||
kotlinx.coroutines.flow.MutableStateFlow(NoteListUiState())
|
||||
.also { state ->
|
||||
viewModelScope.launch {
|
||||
notesFlow.collect { latest ->
|
||||
state.value = state.value.copy(
|
||||
notes = latest,
|
||||
isLoading = false,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onQueryChange(q: String) {
|
||||
rawQuery.value = q
|
||||
|
||||
Reference in new issue
Block a user