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