Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the search bar text input #1466

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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 {
Expand Down