From 84900838f74f29de13f1943b4695b9ab2fca71bb Mon Sep 17 00:00:00 2001 From: Incido Developer Date: Fri, 14 Aug 2026 23:48:54 +1200 Subject: [PATCH] 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. --- .../compose/ui/notelist/NoteListViewModel.kt | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/composeApp/src/main/kotlin/org/incido/compose/ui/notelist/NoteListViewModel.kt b/composeApp/src/main/kotlin/org/incido/compose/ui/notelist/NoteListViewModel.kt index e5ed3b5..7a29ad5 100644 --- a/composeApp/src/main/kotlin/org/incido/compose/ui/notelist/NoteListViewModel.kt +++ b/composeApp/src/main/kotlin/org/incido/compose/ui/notelist/NoteListViewModel.kt @@ -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 = emptyList(), @@ -42,9 +46,15 @@ class NoteListViewModel( private val rawQuery = MutableStateFlow("") private val _isSearchActive = MutableStateFlow(false) - val isSearchActive: StateFlow = _isSearchActive.asStateFlow() - val uiState: StateFlow = + /** + * 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 = combine( rawQuery .debounce(150) .flatMapLatest { q -> @@ -53,25 +63,21 @@ class NoteListViewModel( } else { repository.search(q) } - } - .stateIn( - scope = viewModelScope, - started = SharingStarted.WhileSubscribed(5_000), - initialValue = emptyList(), - ) - .let { notesFlow -> - kotlinx.coroutines.flow.MutableStateFlow(NoteListUiState()) - .also { state -> - viewModelScope.launch { - notesFlow.collect { latest -> - state.value = state.value.copy( - notes = latest, - isLoading = false, - ) - } - } - } - } + }, + _isSearchActive, + rawQuery, + ) { notes, searchActive, q -> + NoteListUiState( + notes = notes, + isLoading = false, + searchQuery = q, + isSearchActive = searchActive, + ) + }.stateIn( + scope = viewModelScope, + started = SharingStarted.WhileSubscribed(5_000), + initialValue = NoteListUiState(), + ) fun onQueryChange(q: String) { rawQuery.value = q