Skip to content
Merged
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 @@ -54,7 +54,6 @@ class HomeScreenTests {
uiState = HomeUiState.NoPosts(
isLoading = false,
errorMessages = listOf(ErrorMessage(0L, R.string.load_error)),
searchInput = "",
),
showTopAppBar = false,
onToggleFavorite = {},
Expand All @@ -64,7 +63,6 @@ class HomeScreenTests {
openDrawer = {},
homeListLazyListState = rememberLazyListState(),
snackbarHostState = snackbarHostState,
onSearchInputChanged = {},
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.clearText
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
Expand Down Expand Up @@ -68,10 +71,8 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
Expand All @@ -97,7 +98,6 @@ import com.example.jetnews.deeplink.util.DeepLinkPattern
import com.example.jetnews.model.Post
import com.example.jetnews.model.PostsFeed
import com.example.jetnews.ui.components.JetnewsSnackbarHost
import com.example.jetnews.ui.modifiers.interceptKey
import com.example.jetnews.ui.navigation.ListDetailScene
import com.example.jetnews.ui.theme.JetnewsTheme
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -178,8 +178,7 @@ fun HomeScreen(
openDrawer = openDrawer,
homeListLazyListState = rememberLazyListState(),
snackbarHostState = snackbarHostState,
onSearchInputChanged = { homeViewModel.onSearchInputChanged(it) },
searchInput = uiState.searchInput,
searchInputState = homeViewModel.searchInputState,
)
}

Expand All @@ -198,8 +197,7 @@ fun HomeFeedScreen(
homeListLazyListState: LazyListState,
snackbarHostState: SnackbarHostState,
modifier: Modifier = Modifier,
searchInput: String = "",
onSearchInputChanged: (String) -> Unit,
searchInputState: TextFieldState = rememberTextFieldState(),
) {
HomeScreenWithList(
uiState = uiState,
Expand All @@ -219,8 +217,7 @@ fun HomeFeedScreen(
contentPadding = contentPadding,
modifier = contentModifier,
state = homeListLazyListState,
searchInput = searchInput,
onSearchInputChanged = onSearchInputChanged,
searchInputState = searchInputState,
)
}
}
Expand Down Expand Up @@ -390,8 +387,7 @@ private fun LoadingContent(
* @param modifier modifier for the root element
* @param contentPadding the padding to apply to the content
* @param state the state object to be used to control or observe the list's state
* @param searchInput (state) the search input
* @param onSearchInputChanged (event) request that the search input changed
* @param searchInputState (state) the search input state
*/
@Composable
private fun PostList(
Expand All @@ -403,8 +399,7 @@ private fun PostList(
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(0.dp),
state: LazyListState = rememberLazyListState(),
searchInput: String = "",
onSearchInputChanged: (String) -> Unit,
searchInputState: TextFieldState = rememberTextFieldState(),
) {
LazyColumn(
modifier = modifier,
Expand All @@ -415,8 +410,7 @@ private fun PostList(
item {
HomeSearch(
Modifier.padding(horizontal = 16.dp),
searchInput = searchInput,
onSearchInputChanged = onSearchInputChanged,
searchInputState = searchInputState,
)
}
}
Expand Down Expand Up @@ -569,43 +563,33 @@ private fun PostListDivider() {
/**
* Expanded search UI - includes support for enter-to-send on the search field
*/
@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
@Composable
private fun HomeSearch(modifier: Modifier = Modifier, searchInput: String = "", onSearchInputChanged: (String) -> Unit) {
private fun HomeSearch(modifier: Modifier = Modifier, searchInputState: TextFieldState = rememberTextFieldState()) {
val context = LocalContext.current
val focusManager = LocalFocusManager.current
val keyboardController = LocalSoftwareKeyboardController.current
OutlinedTextField(
value = searchInput,
onValueChange = onSearchInputChanged,
state = searchInputState,
placeholder = { Text(stringResource(R.string.home_search)) },
leadingIcon = { Icon(painter = painterResource(R.drawable.ic_search), contentDescription = null) },
modifier = modifier
.fillMaxWidth()
.interceptKey(Key.Enter) {
// submit a search query when Enter is pressed
submitSearch(onSearchInputChanged, context)
keyboardController?.hide()
focusManager.clearFocus(force = true)
},
singleLine = true,
modifier = modifier.fillMaxWidth(),
lineLimits = TextFieldLineLimits.SingleLine,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

singleline should handle the enter key press, so the above modifier might be unnecessary. please check

// keyboardOptions change the newline key to a search key on the soft keyboard
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
// keyboardActions submits the search query when the search key is pressed
keyboardActions = KeyboardActions(
onSearch = {
submitSearch(onSearchInputChanged, context)
keyboardController?.hide()
},
),
// onKeyboardAction submits the search query when the search or Enter key is pressed
onKeyboardAction = {
submitSearch(searchInputState, context)
keyboardController?.hide()
focusManager.clearFocus(force = true)
},
)
}

/**
* Stub helper function to submit a user's search query
*/
private fun submitSearch(onSearchInputChanged: (String) -> Unit, context: Context) {
onSearchInputChanged("")
private fun submitSearch(searchInputState: TextFieldState, context: Context) {
searchInputState.clearText()
Toast.makeText(
context,
"Search is not yet implemented",
Expand Down Expand Up @@ -679,7 +663,6 @@ fun PreviewHomeListDrawerScreen() {
favorites = emptySet(),
isLoading = false,
errorMessages = emptyList(),
searchInput = "",
),
showTopAppBar = false,
onToggleFavorite = {},
Expand All @@ -689,7 +672,6 @@ fun PreviewHomeListDrawerScreen() {
openDrawer = {},
homeListLazyListState = rememberLazyListState(),
snackbarHostState = SnackbarHostState(),
onSearchInputChanged = {},
)
}
}
Expand All @@ -713,7 +695,6 @@ fun PreviewHomeListNavRailScreen() {
favorites = emptySet(),
isLoading = false,
errorMessages = emptyList(),
searchInput = "",
),
showTopAppBar = true,
onToggleFavorite = {},
Expand All @@ -723,7 +704,6 @@ fun PreviewHomeListNavRailScreen() {
openDrawer = {},
homeListLazyListState = rememberLazyListState(),
snackbarHostState = SnackbarHostState(),
onSearchInputChanged = {},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.jetnews.ui.home

import androidx.compose.foundation.text.input.TextFieldState
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
Expand All @@ -42,16 +43,14 @@ sealed interface HomeUiState {

val isLoading: Boolean
val errorMessages: List<ErrorMessage>
val searchInput: String

/**
* There are no posts to render.
*
* This could either be because they are still loading or they failed to load, and we are
* waiting to reload them.
*/
data class NoPosts(override val isLoading: Boolean, override val errorMessages: List<ErrorMessage>, override val searchInput: String) :
HomeUiState
data class NoPosts(override val isLoading: Boolean, override val errorMessages: List<ErrorMessage>) : HomeUiState

/**
* There are posts to render, as contained in [postsFeed].
Expand All @@ -61,7 +60,6 @@ sealed interface HomeUiState {
val favorites: Set<String>,
override val isLoading: Boolean,
override val errorMessages: List<ErrorMessage>,
override val searchInput: String,
) : HomeUiState
}

Expand All @@ -73,7 +71,6 @@ private data class HomeViewModelState(
val favorites: Set<String> = emptySet(),
val isLoading: Boolean = false,
val errorMessages: List<ErrorMessage> = emptyList(),
val searchInput: String = "",
) {

/**
Expand All @@ -84,15 +81,13 @@ private data class HomeViewModelState(
HomeUiState.NoPosts(
isLoading = isLoading,
errorMessages = errorMessages,
searchInput = searchInput,
)
} else {
HomeUiState.HasPosts(
postsFeed = postsFeed,
favorites = favorites,
isLoading = isLoading,
errorMessages = errorMessages,
searchInput = searchInput,
)
}
}
Expand All @@ -102,6 +97,8 @@ private data class HomeViewModelState(
*/
class HomeViewModel(private val postsRepository: PostsRepository) : ViewModel() {

val searchInputState = TextFieldState()

private val viewModelState = MutableStateFlow(
HomeViewModelState(
isLoading = true,
Expand Down Expand Up @@ -172,15 +169,6 @@ class HomeViewModel(private val postsRepository: PostsRepository) : ViewModel()
}
}

/**
* Notify that the user updated the search query
*/
fun onSearchInputChanged(searchInput: String) {
viewModelState.update {
it.copy(searchInput = searchInput)
}
}

/**
* Factory for HomeViewModel that takes PostsRepository as a dependency
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ExperimentalMaterial3Api
Expand Down Expand Up @@ -70,13 +71,12 @@ import androidx.compose.material3.adaptive.occludingVerticalHingeBounds
import androidx.compose.material3.adaptive.separatingVerticalHingeBounds
import androidx.compose.material3.carousel.HorizontalMultiBrowseCarousel
import androidx.compose.material3.carousel.rememberCarouselState
import androidx.compose.material3.rememberSearchBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -297,12 +297,10 @@ private fun HomeScreenReady(
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun HomeAppBar(isExpanded: Boolean, modifier: Modifier = Modifier) {
var queryText by remember {
mutableStateOf("")
}
val textFieldState = rememberTextFieldState()
val searchBarState = rememberSearchBarState()
Row(
horizontalArrangement = Arrangement.End,
modifier = modifier
Expand All @@ -311,13 +309,12 @@ private fun HomeAppBar(isExpanded: Boolean, modifier: Modifier = Modifier) {
.padding(horizontal = 16.dp, vertical = 8.dp),
) {
SearchBar(
state = searchBarState,
inputField = {
SearchBarDefaults.InputField(
query = queryText,
onQueryChange = { queryText = it },
textFieldState = textFieldState,
searchBarState = searchBarState,
onSearch = {},
expanded = false,
onExpandedChange = {},
enabled = true,
placeholder = {
Text(stringResource(id = R.string.search_for_a_podcast))
Expand All @@ -338,9 +335,7 @@ private fun HomeAppBar(isExpanded: Boolean, modifier: Modifier = Modifier) {
modifier = if (isExpanded) Modifier.fillMaxWidth() else Modifier,
)
},
expanded = false,
onExpandedChange = {},
) {}
)
}
}

Expand Down
Loading
Loading