Skip to content

Commit

Permalink
Merged PR android#1466
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Lammertsma committed Oct 4, 2024
1 parent ad13454 commit a7db73a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import androidx.compose.material3.adaptive.occludingVerticalHingeBounds
import androidx.compose.material3.adaptive.separatingVerticalHingeBounds
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
Expand Down Expand Up @@ -378,6 +379,11 @@ 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(start = 16.dp, 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.onToggleSearch() },
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,14 @@ 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 @@ -149,6 +158,17 @@ class HomeViewModel @Inject constructor(
}
}

fun onSearchTextChange(text: String) {
_searchText.value = text
}

fun onToggleSearch() {
_isSearching.value = !_isSearching.value
if (!_isSearching.value) {
onSearchTextChange("")
}
}

fun onCategorySelected(category: CategoryInfo) {
_selectedCategory.value = category
}
Expand Down

0 comments on commit a7db73a

Please sign in to comment.