A fully functional Android app built entirely with Kotlin and Jetpack Compose
New empty bookmarks handling (#443)
Mohsen Rznac19b8b93191c
diff --git a/core/designsystem/src/main/java/com/google/samples/apps/nowinandroid/core/designsystem/component/LoadingWheel.kt b/core/designsystem/src/main/java/com/google/samples/apps/nowinandroid/core/designsystem/component/LoadingWheel.ktindex ba3f7c2b0..c32637b69 100644--- a/core/designsystem/src/main/java/com/google/samples/apps/nowinandroid/core/designsystem/component/LoadingWheel.kt+++ b/core/designsystem/src/main/java/com/google/samples/apps/nowinandroid/core/designsystem/component/LoadingWheel.kt@@ -42,6 +42,7 @@ import androidx.compose.ui.geometry.Offsetimport androidx.compose.ui.graphics.StrokeCapimport androidx.compose.ui.graphics.drawscope.rotateimport androidx.compose.ui.graphics.graphicsLayer+import androidx.compose.ui.platform.LocalInspectionModeimport androidx.compose.ui.semantics.contentDescriptionimport androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.unit.dp@@ -56,7 +57,8 @@ fun NiaLoadingWheel(val infiniteTransition = rememberInfiniteTransition()// Specifies the float animation for slowly drawing out the lines on entering- val floatAnimValues = (0 until NUM_OF_LINES).map { remember { Animatable(1F) } }+ val startValue = if (LocalInspectionMode.current) 0F else 1F+ val floatAnimValues = (0 until NUM_OF_LINES).map { remember { Animatable(startValue) } }LaunchedEffect(floatAnimValues) {(0 until NUM_OF_LINES).map { index ->launch {diff --git a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.ktindex 560378370..06a87bc9e 100644--- a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt+++ b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt@@ -16,14 +16,17 @@package com.google.samples.apps.nowinandroid.feature.bookmarks+import androidx.annotation.VisibleForTesting+import androidx.compose.foundation.Imageimport androidx.compose.foundation.layout.Arrangement-import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.PaddingValuesimport androidx.compose.foundation.layout.Spacerimport androidx.compose.foundation.layout.WindowInsets-import androidx.compose.foundation.layout.fillMaxHeightimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.safeDrawingimport androidx.compose.foundation.layout.windowInsetsBottomHeightimport androidx.compose.foundation.layout.wrapContentSize@@ -31,19 +34,29 @@ import androidx.compose.foundation.lazy.grid.GridCells.Adaptiveimport androidx.compose.foundation.lazy.grid.GridItemSpanimport androidx.compose.foundation.lazy.grid.LazyVerticalGridimport androidx.compose.foundation.lazy.grid.rememberLazyGridState+import androidx.compose.material3.MaterialThemeimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.runtime.getValueimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.platform.testTag+import androidx.compose.ui.res.painterResourceimport androidx.compose.ui.res.stringResource+import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.hilt.navigation.compose.hiltViewModelimport androidx.lifecycle.compose.ExperimentalLifecycleComposeApiimport androidx.lifecycle.compose.collectAsStateWithLifecycleimport com.google.samples.apps.nowinandroid.core.designsystem.component.NiaLoadingWheel+import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme+import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource+import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResourcesimport 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.Successimport com.google.samples.apps.nowinandroid.core.ui.TrackScrollJankimport com.google.samples.apps.nowinandroid.core.ui.newsFeed@@ -61,8 +74,39 @@ internal fun BookmarksRoute()}+/**+ * Displays the user's bookmarked articles. Includes support for loading and empty states.+ */+@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)+@Composable+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-fun BookmarksScreen(+private fun LoadingState(modifier: Modifier = Modifier) {+ NiaLoadingWheel(+ modifier = modifier+ .fillMaxWidth()+ .wrapContentSize()+ .testTag("forYou:loading"),+ contentDesc = stringResource(id = R.string.saved_loading),+ )+}++@Composable+private fun BookmarksGrid(feedState: NewsFeedUiState,removeFromBookmarks: (String) -> Unit,modifier: Modifier = Modifier@@ -79,41 +123,80 @@ fun BookmarksScreen(.fillMaxSize().testTag("bookmarks:feed")) {+ newsFeed(+ feedState = feedState,+ onNewsResourcesCheckedChanged = { id, _ -> removeFromBookmarks(id) },+ )+ item(span = { GridItemSpan(maxLineSpan) }) {+ Spacer(Modifier.windowInsetsBottomHeight(WindowInsets.safeDrawing))+ }+ }+}- when (feedState) {- is NewsFeedUiState.Loading -> {- item(span = { GridItemSpan(maxLineSpan) }) {- NiaLoadingWheel(- modifier = Modifier- .fillMaxWidth()- .wrapContentSize()- .testTag("forYou:loading"),- contentDesc = stringResource(id = R.string.saved_loading),- )- }- }+@Composable+private fun EmptyState(modifier: Modifier = Modifier) {+ Column(+ modifier = modifier+ .padding(16.dp)+ .fillMaxSize()+ .testTag("bookmarks:empty"),+ verticalArrangement = Arrangement.Center,+ horizontalAlignment = Alignment.CenterHorizontally+ ) {+ Image(+ modifier = Modifier.fillMaxWidth(),+ painter = painterResource(id = R.drawable.img_empty_bookmarks),+ contentDescription = null+ )++ Spacer(modifier = Modifier.height(16.dp))++ Text(+ text = stringResource(id = R.string.bookmarks_empty_error),+ modifier = Modifier.fillMaxWidth(),+ textAlign = TextAlign.Center,+ style = MaterialTheme.typography.titleMedium,+ fontWeight = FontWeight.Bold+ )- is NewsFeedUiState.Success -> {- if (feedState.feed.isNotEmpty()) {- newsFeed(- feedState = feedState,- onNewsResourcesCheckedChanged = { id, _ -> removeFromBookmarks(id) },- )- item(span = { GridItemSpan(maxLineSpan) }) {- Spacer(Modifier.windowInsetsBottomHeight(WindowInsets.safeDrawing))- }- } else item(span = { GridItemSpan(maxLineSpan) }) {- Box(- modifier = Modifier- .fillMaxHeight()- .wrapContentSize()- .testTag("bookmarks:empty"),- contentAlignment = Alignment.Center- ) {- Text(text = stringResource(id = R.string.bookmarks_empty))- }+ Spacer(modifier = Modifier.height(8.dp))++ Text(+ text = stringResource(id = R.string.bookmarks_empty_description),+ modifier = Modifier.fillMaxWidth(),+ textAlign = TextAlign.Center,+ style = MaterialTheme.typography.bodyMedium+ )+ }+}++@Preview+@Composable+private fun LoadingStatePreview() {+ NiaTheme {+ LoadingState()+ }+}++@Preview+@Composable+private fun BookmarksGridPreview() {+ NiaTheme {+ BookmarksGrid(+ feedState = Success(+ previewNewsResources.map {+ SaveableNewsResource(it, false)}- }- }+ ),+ removeFromBookmarks = {}+ )+ }+}++@Preview+@Composable+fun EmptyStatePreview() {+ NiaTheme {+ EmptyState()}}diff --git a/feature/bookmarks/src/main/res/drawable/img_empty_bookmarks.xml b/feature/bookmarks/src/main/res/drawable/img_empty_bookmarks.xmlnew file mode 100644index 000000000..b9e2f2963--- /dev/null+++ b/feature/bookmarks/src/main/res/drawable/img_empty_bookmarks.xml@@ -0,0 +1,31 @@+<?xml version="1.0" encoding="utf-8"?>+<!--+ Copyright 2022 The Android Open Source Project++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+-->+<vector xmlns:android="http://schemas.android.com/apk/res/android"+ android:width="192dp"+ android:height="192dp"+ android:viewportWidth="64"+ android:viewportHeight="64">+ <path+ android:fillColor="#8B418F"+ android:pathData="M16,1C15.448,1 15,1.448 15,2C15,2.552 15.448,3 16,3V1ZM58,52C58,52.552 58.448,53 59,53C59.552,53 60,52.552 60,52H58ZM16,3H55V1H16V3ZM58,6V52H60V6H58ZM55,3C56.657,3 58,4.343 58,6H60C60,3.239 57.761,1 55,1V3Z" />+ <path+ android:fillColor="#00000000"+ android:pathData="M45,10H9C6.791,10 5,11.791 5,14V55.854C5,59.177 8.817,61.051 11.446,59.019L24.554,48.89C25.995,47.777 28.005,47.777 29.446,48.89L42.554,59.019C45.183,61.051 49,59.177 49,55.854V14C49,11.791 47.209,10 45,10Z"+ android:strokeColor="#8B418F"+ android:strokeLineCap="round"+ android:strokeWidth="2" />+</vector>diff --git a/feature/bookmarks/src/main/res/values/strings.xml b/feature/bookmarks/src/main/res/values/strings.xmlindex 965b78333..61781ad6e 100644--- a/feature/bookmarks/src/main/res/values/strings.xml+++ b/feature/bookmarks/src/main/res/values/strings.xml@@ -20,5 +20,6 @@<string name="top_app_bar_title">Saved</string><string name="top_app_bar_action_search">Search</string><string name="top_app_bar_action_menu">Menu</string>- <string name="bookmarks_empty">No bookmarks yet!</string>+ <string name="bookmarks_empty_error">No saved updates</string>+ <string name="bookmarks_empty_description">Updates you save will be stored here\nto read later</string></resources>\ No newline at end of file