Resource Card: Add bookmark icon

Change-Id: I61ae5ca4ba542415165265dfdf0aea4ee0a63f44
pull/2/head
Caren Chang 3 years ago
parent d161bb5dc0
commit 68ba1ba477

@ -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

@ -21,4 +21,8 @@
<string name="topics">Topics</string>
<string name="done">Done</string>
<string name="for_you_loading">Loading for you…</string>
<!-- Resource Card -->
<string name="bookmark">Bookmark</string>
<string name="unbookmark">Unbookmark</string>
</resources>

Loading…
Cancel
Save