Simplify logic in Bookmarks screen

pull/443/head
Jolanda Verhoef 3 years ago
parent 03ab5c63df
commit 01529f567e

@ -16,6 +16,7 @@
package com.google.samples.apps.nowinandroid.feature.bookmarks
import androidx.annotation.VisibleForTesting
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
@ -50,6 +51,8 @@ import androidx.lifecycle.compose.ExperimentalLifecycleComposeApi
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaLoadingWheel
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState.Loading
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState.Success
import com.google.samples.apps.nowinandroid.core.ui.TrackScrollJank
import com.google.samples.apps.nowinandroid.core.ui.newsFeed
@ -67,19 +70,35 @@ internal fun BookmarksRoute(
)
}
/**
* Displays the user's bookmarked articles. Includes support for loading and empty states.
*/
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
@Composable
fun BookmarksScreen(
internal fun BookmarksScreen(
feedState: NewsFeedUiState,
removeFromBookmarks: (String) -> Unit,
modifier: Modifier = Modifier
) {
when (feedState) {
Loading -> LoadingState(modifier)
is Success ->
if (feedState.feed.isNotEmpty()) {
BookmarksGrid(feedState, removeFromBookmarks, modifier)
} else {
EmptyState(modifier)
}
}
}
@Composable
private fun BookmarksGrid(
feedState: NewsFeedUiState,
removeFromBookmarks: (String) -> Unit,
modifier: Modifier = Modifier
) {
val scrollableState = rememberLazyGridState()
TrackScrollJank(scrollableState = scrollableState, stateName = "bookmarks:grid")
/**
** The [LazyVerticalGrid] is handling the Loading and Success states when the feed is not empty.
**/
LazyVerticalGrid(
columns = Adaptive(300.dp),
contentPadding = PaddingValues(16.dp),
@ -90,18 +109,6 @@ fun BookmarksScreen(
.fillMaxSize()
.testTag("bookmarks:feed")
) {
when (feedState) {
is NewsFeedUiState.Loading -> item(span = { GridItemSpan(maxLineSpan) }) {
NiaLoadingWheel(
modifier = Modifier
.fillMaxWidth()
.wrapContentSize()
.testTag("forYou:loading"),
contentDesc = stringResource(id = R.string.saved_loading),
)
}
is NewsFeedUiState.Success -> {
if (feedState.feed.isNotEmpty()) {
newsFeed(
feedState = feedState,
onNewsResourcesCheckedChanged = { id, _ -> removeFromBookmarks(id) },
@ -111,21 +118,11 @@ fun BookmarksScreen(
}
}
}
}
}
/**
** The [Column] is handling the Empty state when the feed is empty.
**/
if (feedState is NewsFeedUiState.Success && feedState.feed.isEmpty()) {
EmptyState()
}
}
@Composable
private fun EmptyState() {
private fun EmptyState(modifier: Modifier = Modifier) {
Column(
modifier = Modifier
modifier = modifier
.padding(16.dp)
.fillMaxSize()
.testTag("bookmarks:empty"),
@ -158,3 +155,14 @@ private fun EmptyState() {
)
}
}
@Composable
private fun LoadingState(modifier: Modifier = Modifier) {
NiaLoadingWheel(
modifier = modifier
.fillMaxWidth()
.wrapContentSize()
.testTag("forYou:loading"),
contentDesc = stringResource(id = R.string.saved_loading),
)
}

Loading…
Cancel
Save