A fully functional Android app built entirely with Kotlin and Jetpack Compose
Improve AuthorItem behavior
Jolanda Verhoef7e04447cffbc
diff --git a/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/AuthorsCarousel.kt b/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/AuthorsCarousel.ktindex d6d686d4b..1f37f576d 100644--- a/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/AuthorsCarousel.kt+++ b/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/AuthorsCarousel.kt@@ -43,10 +43,12 @@ import androidx.compose.runtime.rememberimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.clip+import androidx.compose.ui.draw.drawBehindimport androidx.compose.ui.layout.ContentScaleimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.res.stringResourceimport androidx.compose.ui.semantics.Role+import androidx.compose.ui.semantics.onClickimport androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.semantics.stateDescriptionimport androidx.compose.ui.text.font.FontWeight@@ -54,7 +56,6 @@ import androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport coil.compose.AsyncImage-import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaToggleButtonimport com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIconsimport com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaThemeimport com.google.samples.apps.nowinandroid.core.domain.model.FollowableAuthor@@ -73,8 +74,8 @@ fun AuthorsCarousel(LazyRow(modifier = modifier.testTag(tag),- contentPadding = PaddingValues(24.dp),- horizontalArrangement = Arrangement.spacedBy(24.dp),+ contentPadding = PaddingValues(16.dp),+ horizontalArrangement = Arrangement.spacedBy(8.dp),state = lazyListState) {items(items = authors, key = { item -> item.author.id }) { followableAuthor ->@@ -97,24 +98,32 @@ fun AuthorItem(modifier: Modifier = Modifier,) {val followDescription = if (following) {- stringResource(id = R.string.following)+ stringResource(R.string.following)} else {- stringResource(id = R.string.not_following)+ stringResource(R.string.not_following)+ }+ val followActionLabel = if (following) {+ stringResource(R.string.unfollow)+ } else {+ stringResource(R.string.follow)}Column(modifier = modifier.toggleable(value = following,- enabled = true,role = Role.Button,interactionSource = remember { MutableInteractionSource() },indication = rememberRipple(bounded = false),onValueChange = { newFollowing -> onAuthorClick(newFollowing) },)+ .padding(8.dp).sizeIn(maxWidth = 48.dp).semantics(mergeDescendants = true) {+ // Add information for A11y services, explaining what each state means and+ // what will happen when the user interacts with the author item.stateDescription = "$followDescription ${author.name}"+ onClick(label = followActionLabel, action = null)}) {Box(@@ -140,24 +149,23 @@ fun AuthorItem(contentDescription = null)}- NiaToggleButton(- checked = following,- onCheckedChange = onAuthorClick,- modifier = Modifier.align(Alignment.BottomEnd),- icon = {- Icon(- imageVector = NiaIcons.Add,- contentDescription = null- )- },- checkedIcon = {- Icon(- imageVector = NiaIcons.Check,- contentDescription = null- )- },- size = 24.dp,- backgroundColor = MaterialTheme.colorScheme.surface+ val backgroundColor =+ if (following)+ MaterialTheme.colorScheme.primaryContainer+ else+ MaterialTheme.colorScheme.surface+ Icon(+ imageVector = if (following) NiaIcons.Check else NiaIcons.Add,+ contentDescription = null,+ modifier = Modifier+ .align(Alignment.BottomEnd)+ .size(18.dp)+ .drawBehind {+ drawCircle(+ color = backgroundColor,+ radius = 12.dp.toPx()+ )+ })}Spacer(modifier = Modifier.height(4.dp))diff --git a/feature/foryou/src/main/res/values/strings.xml b/feature/foryou/src/main/res/values/strings.xmlindex 8f109ef69..1880ab953 100644--- a/feature/foryou/src/main/res/values/strings.xml+++ b/feature/foryou/src/main/res/values/strings.xml@@ -27,5 +27,7 @@<!-- Authors--><string name="following">You are following</string><string name="not_following">You are not following</string>+ <string name="follow">Follow</string>+ <string name="unfollow">Unfollow</string></resources>