private var searchJob: kotlinx.coroutines.Job? = null
fun loadNotes() {
searchJob?.cancel()
searchJob = viewModelScope.launch {
_uiState.value = NotesUiState.Loading
repository.observeAll().collect { notes ->
_uiState.value = if (notes.isEmpty()) NotesUiState.Empty
else NotesUiState.Success(notes)
}
}
}
fun searchNotes(query: String) {
searchJob?.cancel()
if (query.isBlank()) {
loadNotes()
return
}
searchJob = viewModelScope.launch {
repository.search(query).collect { notes ->
_uiState.value = if (notes.isEmpty()) NotesUiState.Empty
else NotesUiState.Success(notes)
}
}
}