Last active
June 7, 2024 06:08
-
-
Save LloydBlv/ad10d75faae83ff9f0de1f7523ee873b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchViewModel @Inject constructor(private val searchRepo: SearchRepository): ViewModel() { | |
private val _searchQuery = MutableStateFlow("") | |
val searchResults: StateFlow<List<SearchResult>> = _searchQuery | |
.debounce(300) // Add a debounce to limit requests | |
.filter(String::isNotEmpty) // Ignore empty queries | |
.flatMapLatest(searchRepository::search) | |
.stateIn(viewModelScope, SharingStarted.Lazily, emptyList()) | |
fun setSearchQuery(query: String) { | |
_searchQuery.update { query } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment