From 68ba1ba47721e8ef6af782b59fcc82a3ca767d75 Mon Sep 17 00:00:00 2001 From: Caren Chang Date: Wed, 26 Jan 2022 13:13:25 -0800 Subject: [PATCH] Resource Card: Add bookmark icon Change-Id: I61ae5ca4ba542415165265dfdf0aea4ee0a63f44 --- .../apps/nowinandroid/ui/ResourceCard.kt | 57 ++++++++++++++++++- app/src/main/res/values/strings.xml | 4 ++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/ResourceCard.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/ResourceCard.kt index 2db7eb367..5c30e6a41 100644 --- a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/ResourceCard.kt +++ b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/ResourceCard.kt @@ -17,9 +17,19 @@ package com.google.samples.apps.nowinandroid.ui import android.content.res.Configuration -import androidx.compose.material.Surface +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Bookmark +import androidx.compose.material.icons.filled.BookmarkBorder +import androidx.compose.material3.Icon +import androidx.compose.material3.IconToggleButton +import androidx.compose.material3.Surface import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.onClick +import androidx.compose.ui.semantics.semantics import androidx.compose.ui.tooling.preview.Preview +import com.google.samples.apps.nowinandroid.R import com.google.samples.apps.nowinandroid.data.news.NewsResource import com.google.samples.apps.nowinandroid.ui.theme.NiaTheme @@ -28,6 +38,31 @@ import com.google.samples.apps.nowinandroid.ui.theme.NiaTheme * For You, Episodes, Saved */ +@Composable +fun BookmarkButton( + isBookmarked: Boolean, + onClick: () -> Unit, + modifier: Modifier = Modifier +) { + val clickActionLabel = stringResource( + if (isBookmarked) R.string.unbookmark else R.string.bookmark + ) + IconToggleButton( + checked = isBookmarked, + onCheckedChange = { onClick() }, + modifier = modifier.semantics { + // Use custom label for accessibility services to communicate button's action to user. + // Pass null for action to only override the label and not the actual action. + this.onClick(label = clickActionLabel, action = null) + } + ) { + Icon( + imageVector = if (isBookmarked) Icons.Filled.Bookmark else Icons.Filled.BookmarkBorder, + contentDescription = null // handled by click label of parent + ) + } +} + @Composable fun ResourceAuthors( newsResource: NewsResource @@ -82,6 +117,26 @@ fun ResourceCardExpanded() { TODO() } +@Preview("Bookmark Button") +@Composable +fun BookmarkButtonPreview() { + NiaTheme { + Surface { + BookmarkButton(isBookmarked = false, onClick = { }) + } + } +} + +@Preview("Bookmark Button Bookmarked") +@Composable +fun BookmarkButtonBookmarkedPreview() { + NiaTheme { + Surface { + BookmarkButton(isBookmarked = true, onClick = { }) + } + } +} + @Preview("Expanded resource card") @Preview("Expanded resource card (dark)", uiMode = Configuration.UI_MODE_NIGHT_YES) @Composable diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 18d8565b2..d4de7d616 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -21,4 +21,8 @@ Topics Done Loading for you… + + + Bookmark + Unbookmark