A fully functional Android app built entirely with Kotlin and Jetpack Compose
Refactor InterestsItem to Material3 ListItem
Miłosz Moczkowski445c18a80363
diff --git a/feature/interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsItem.kt b/feature/interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsItem.ktindex ec9fd8f10..7456ba92b 100644--- a/feature/interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsItem.kt+++ b/feature/interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/InterestsItem.kt@@ -18,22 +18,20 @@ package com.google.samples.apps.nowinandroid.feature.interestsimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.clickable-import androidx.compose.foundation.layout.Column-import androidx.compose.foundation.layout.Row-import androidx.compose.foundation.layout.Spacerimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.size-import androidx.compose.foundation.layout.widthimport androidx.compose.material3.Icon+import androidx.compose.material3.ListItem+import androidx.compose.material3.ListItemDefaultsimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.Surfaceimport androidx.compose.material3.Textimport androidx.compose.runtime.Composable-import androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.Colorimport androidx.compose.ui.res.stringResource+import androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.tooling.preview.Preview-import androidx.compose.ui.unit.Dpimport androidx.compose.ui.unit.dpimport com.google.samples.apps.nowinandroid.core.designsystem.component.DynamicAsyncImageimport com.google.samples.apps.nowinandroid.core.designsystem.component.NiaIconToggleButton@@ -51,63 +49,46 @@ fun InterestsItem(modifier: Modifier = Modifier,iconModifier: Modifier = Modifier,description: String = "",- itemSeparation: Dp = 16.dp,) {- Row(- verticalAlignment = Alignment.CenterVertically,- modifier = modifier,- ) {- Row(- verticalAlignment = Alignment.CenterVertically,- modifier = Modifier- .weight(1f)- .clickable { onClick() }- .padding(vertical = itemSeparation),- ) {+ ListItem(+ leadingContent = {InterestsIcon(topicImageUrl, iconModifier.size(64.dp))- Spacer(modifier = Modifier.width(24.dp))- InterestContent(name, description)- }- NiaIconToggleButton(- checked = following,- onCheckedChange = onFollowButtonClick,- icon = {- Icon(- imageVector = NiaIcons.Add,- contentDescription = stringResource(- id = string.card_follow_button_content_desc,- ),- )- },- checkedIcon = {- Icon(- imageVector = NiaIcons.Check,- contentDescription = stringResource(- id = string.card_unfollow_button_content_desc,- ),- )- },- )- }-}--@Composable-private fun InterestContent(name: String, description: String, modifier: Modifier = Modifier) {- Column(modifier) {- Text(- text = name,- style = MaterialTheme.typography.headlineSmall,- modifier = Modifier.padding(- vertical = if (description.isEmpty()) 0.dp else 4.dp,- ),- )- if (description.isNotEmpty()) {- Text(- text = description,- style = MaterialTheme.typography.bodyMedium,+ },+ headlineContent = {+ Text(text = name)+ },+ supportingContent = {+ Text(text = description)+ },+ trailingContent = {+ NiaIconToggleButton(+ checked = following,+ onCheckedChange = onFollowButtonClick,+ icon = {+ Icon(+ imageVector = NiaIcons.Add,+ contentDescription = stringResource(+ id = string.card_follow_button_content_desc,+ ),+ )+ },+ checkedIcon = {+ Icon(+ imageVector = NiaIcons.Check,+ contentDescription = stringResource(+ id = string.card_unfollow_button_content_desc,+ ),+ )+ },)- }- }+ },+ colors = ListItemDefaults.colors(+ containerColor = Color.Transparent,+ ),+ modifier = modifier+ .semantics(mergeDescendants = true) { /* no-op */ }+ .clickable(enabled = true, onClick = onClick),+ )}@Composablediff --git a/feature/search/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/search/SearchScreenTest.kt b/feature/search/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/search/SearchScreenTest.ktindex 53f00c0dc..d6c07221e 100644--- a/feature/search/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/search/SearchScreenTest.kt+++ b/feature/search/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/search/SearchScreenTest.kt@@ -20,11 +20,14 @@ import androidx.activity.ComponentActivityimport androidx.compose.ui.test.assertCountEqualsimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.assertIsFocused+import androidx.compose.ui.test.hasScrollToNodeActionimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onAllNodesWithContentDescription+import androidx.compose.ui.test.onFirstimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performScrollToIndeximport com.google.samples.apps.nowinandroid.core.data.model.RecentSearchQueryimport com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig.DARKimport com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand.ANDROID@@ -139,15 +142,18 @@ class SearchScreenTest {composeTestRule.onNodeWithText(topicsString).assertIsDisplayed()- composeTestRule- .onNodeWithText(followableTopicTestData[0].topic.name)- .assertIsDisplayed()- composeTestRule- .onNodeWithText(followableTopicTestData[1].topic.name)- .assertIsDisplayed()- composeTestRule- .onNodeWithText(followableTopicTestData[2].topic.name)- .assertIsDisplayed()++ val scrollableNode = composeTestRule+ .onAllNodes(hasScrollToNodeAction())+ .onFirst()++ followableTopicTestData.forEachIndexed { index, followableTopic ->+ scrollableNode.performScrollToIndex(index)++ composeTestRule+ .onNodeWithText(followableTopic.topic.name)+ .assertIsDisplayed()+ }composeTestRule.onAllNodesWithContentDescription(followButtonContentDesc)