From 8c46b2482445014b6d8abaa9e4c83e206dc0e1d6 Mon Sep 17 00:00:00 2001 From: Anish Biswal Date: Thu, 12 Sep 2024 15:33:10 +0530 Subject: [PATCH] Fixed the search bar text input --- .../com/example/jetcaster/ui/home/Home.kt | 14 ++++++++---- .../jetcaster/ui/home/HomeViewModel.kt | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/Home.kt b/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/Home.kt index 46a0cffbf0..69a822cbe9 100644 --- a/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/Home.kt +++ b/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/Home.kt @@ -378,6 +378,12 @@ private fun HomeAppBar( isExpanded: Boolean, modifier: Modifier = Modifier, ) { + val viewModel : HomeViewModel = hiltViewModel() + + val searchText by viewModel.searchText.collectAsState() + val isSearching by viewModel.isSearching.collectAsState() + + Row( horizontalArrangement = Arrangement.End, modifier = modifier @@ -386,14 +392,14 @@ private fun HomeAppBar( .padding(end = 16.dp, top = 8.dp, bottom = 8.dp) ) { SearchBar( - query = "", - onQueryChange = {}, + query = searchText, + onQueryChange = {viewModel.onSearchTextChange(it)}, placeholder = { Text(stringResource(id = R.string.search_for_a_podcast)) }, - onSearch = {}, + onSearch = {viewModel.onSearchTextChange(it)}, active = false, - onActiveChange = {}, + onActiveChange = {viewModel.onToogleSearch()}, leadingIcon = { Icon( imageVector = Icons.Default.Search, diff --git a/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/HomeViewModel.kt b/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/HomeViewModel.kt index 3d5450464d..7eedd5b0b6 100644 --- a/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/HomeViewModel.kt +++ b/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/HomeViewModel.kt @@ -43,6 +43,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.shareIn @@ -72,6 +73,15 @@ class HomeViewModel @Inject constructor( // Holds the view state if the UI is refreshing for new data private val refreshing = MutableStateFlow(false) + //first state whether the search is happening or not + private val _isSearching = MutableStateFlow(false) + val isSearching = _isSearching.asStateFlow() + + //second state the text typed by the user + private val _searchText = MutableStateFlow("") + val searchText = _searchText.asStateFlow() + + private val subscribedPodcasts = podcastStore.followedPodcastsSortedByLastEpisode(limit = 10) .shareIn(viewModelScope, SharingStarted.WhileSubscribed()) @@ -137,6 +147,18 @@ class HomeViewModel @Inject constructor( refresh(force = false) } + fun onSearchTextChange(text: String) { + _searchText.value = text + } + + fun onToogleSearch() { + _isSearching.value = !_isSearching.value + if (!_isSearching.value) { + onSearchTextChange("") + } + } + + fun refresh(force: Boolean = true) { viewModelScope.launch { runCatching {