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:
Incido Developer committed 2026-08-14 23:48:54 +12:00
1 parent 5e1b77bedf
commit 84900838f7
1 file changed
+28 -22
@@ -11,7 +11,7 @@ import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.stateIn 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 * Immutable UI state for [NoteListScreen]. A loading flag plus the
* current snapshot of notes; an empty list with [isLoading] == false * current snapshot of notes; an empty list with [isLoading] == false
* indicates the empty-state UI should be shown (F6). * 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( data class NoteListUiState(
val notes: List<Note> = emptyList(), val notes: List<Note> = emptyList(),
@@ -42,9 +46,15 @@ class NoteListViewModel(
private val rawQuery = MutableStateFlow("") private val rawQuery = MutableStateFlow("")
private val _isSearchActive = MutableStateFlow(false) 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 rawQuery
.debounce(150) .debounce(150)
.flatMapLatest { q -> .flatMapLatest { q ->
@@ -53,25 +63,21 @@ class NoteListViewModel(
} else { } else {
repository.search(q) repository.search(q)
} }
} },
.stateIn( _isSearchActive,
scope = viewModelScope, rawQuery,
started = SharingStarted.WhileSubscribed(5_000), ) { notes, searchActive, q ->
initialValue = emptyList(), NoteListUiState(
) notes = notes,
.let { notesFlow -> isLoading = false,
kotlinx.coroutines.flow.MutableStateFlow(NoteListUiState()) searchQuery = q,
.also { state -> isSearchActive = searchActive,
viewModelScope.launch { )
notesFlow.collect { latest -> }.stateIn(
state.value = state.value.copy( scope = viewModelScope,
notes = latest, started = SharingStarted.WhileSubscribed(5_000),
isLoading = false, initialValue = NoteListUiState(),
) )
}
}
}
}
fun onQueryChange(q: String) { fun onQueryChange(q: String) {
rawQuery.value = q rawQuery.value = q