-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ANDROID-14134 Set assets clickable #324
Changes from all commits
f765cc6
435b934
da4e9d6
ba3ca7f
c599cb4
b56750c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,31 +28,36 @@ sealed class ListRowIcon(val contentDescription: String?) { | |
data class NormalIcon( | ||
val painter: Painter? = null, | ||
private val description: String? = null, | ||
val modifier: Modifier = Modifier, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In compose we expose the modifier which is a common used practice which we were not following until now: https://chrisbanes.me/posts/always-provide-a-modifier/ |
||
) : ListRowIcon(description) | ||
|
||
data class CircleIcon( | ||
val painter: Painter? = null, | ||
val backgroundColor: Color = Color.Transparent, | ||
private val description: String? = null, | ||
val modifier: Modifier = Modifier, | ||
) : ListRowIcon(description) | ||
|
||
data class SmallAsset( | ||
val painter: Painter? = null, | ||
private val description: String? = null, | ||
val modifier: Modifier = Modifier, | ||
) : ListRowIcon(description) | ||
|
||
data class LargeAsset( | ||
val painter: Painter? = null, | ||
val aspectRatio: AspectRatio = AspectRatio.RATIO_1_1, | ||
val contentScale: ContentScale = ContentScale.Crop, | ||
private val description: String? = null, | ||
val modifier: Modifier = Modifier, | ||
) : ListRowIcon(description) | ||
|
||
data class ImageAsset( | ||
val painter: Painter? = null, | ||
val dimensions: ImageDimensions? = null, | ||
val contentScale: ContentScale = ContentScale.Crop, | ||
private val description: String? = null, | ||
val modifier: Modifier = Modifier, | ||
) : ListRowIcon(description) | ||
|
||
enum class AspectRatio(val width: Dp, val height: Dp) { | ||
|
@@ -75,9 +80,9 @@ sealed class ListRowIcon(val contentDescription: String?) { | |
@Composable | ||
private fun NormalIcon.DrawNormalIcon() { | ||
Box( | ||
modifier = Modifier | ||
modifier = modifier | ||
.size(40.dp) | ||
.wrapContentSize(align = Alignment.Center) | ||
.wrapContentSize(align = Alignment.Center), | ||
) { | ||
painter?.let { | ||
Icon( | ||
|
@@ -94,6 +99,7 @@ sealed class ListRowIcon(val contentDescription: String?) { | |
private fun CircleIcon.DrawCircleIcon() { | ||
Circle( | ||
color = backgroundColor, | ||
modifier = modifier, | ||
) { | ||
painter?.let { | ||
Icon( | ||
|
@@ -110,7 +116,7 @@ sealed class ListRowIcon(val contentDescription: String?) { | |
Image( | ||
painter = painter, | ||
contentDescription = contentDescription, | ||
modifier = Modifier | ||
modifier = modifier | ||
.size(40.dp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For accessibility reasons, we might consider expanding the clickable area of the asset to 48px. We have updated our specs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I'm fine with that if the specs have also been updated. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just exposing the modifier and not making it clickable with just that. Also doing that includes breaking changes in the lib. In my opinion we should ensure what we want to make clickable or not on the designs and then use this modifier to make it clickable or not depending on those designs. I'm going to revert this change. |
||
.clip(CircleShape), | ||
contentScale = ContentScale.Crop, | ||
|
@@ -124,7 +130,7 @@ sealed class ListRowIcon(val contentDescription: String?) { | |
Image( | ||
painter = painter, | ||
contentDescription = contentDescription, | ||
modifier = Modifier | ||
modifier = modifier | ||
.height(aspectRatio.height) | ||
.width(aspectRatio.width) | ||
.clip(RoundedCornerShape(4.dp)), | ||
|
@@ -139,7 +145,7 @@ sealed class ListRowIcon(val contentDescription: String?) { | |
Image( | ||
painter = painter, | ||
contentDescription = contentDescription, | ||
modifier = Modifier | ||
modifier = modifier | ||
.width(dimensions?.width?.dp ?: dimensionResource(id = R.dimen.asset_default_size)) | ||
.height(dimensions?.height?.dp ?: dimensionResource(id = R.dimen.asset_default_size)) | ||
.clip(RoundedCornerShape(4.dp)), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -309,6 +309,10 @@ class ListRowView @JvmOverloads constructor( | |
} | ||
} | ||
|
||
fun setAssetOnClickListener(clickListener: OnClickListener) { | ||
assetImageLayout.setOnClickListener(clickListener) | ||
} | ||
Comment on lines
+312
to
+314
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the code added for classic views in order to be able to set the click listener. |
||
|
||
private fun updateIconVisibility() { | ||
assetCircularImageView.isVisible = assetType == TYPE_IMAGE | ||
assetRoundedImageView.isVisible = assetType == TYPE_IMAGE_1_1 || assetType == TYPE_IMAGE_7_10 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
package com.telefonica.mistica.compose.list | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.test.hasTestTag | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onRoot | ||
import androidx.compose.ui.test.performClick | ||
import com.telefonica.mistica.compose.shape.Chevron | ||
import com.telefonica.mistica.compose.tag.Tag | ||
import com.telefonica.mistica.compose.theme.MisticaTheme | ||
|
@@ -15,6 +20,9 @@ import org.junit.Test | |
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import com.telefonica.mistica.R | ||
import org.junit.Assert.assertEquals | ||
|
||
private const val LIST_ROW_ITEM_ASSET_TAG = "listRowItemAssetTag" | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
internal class ListRowItemKtTest: ScreenshotsTest() { | ||
|
@@ -35,14 +43,30 @@ internal class ListRowItemKtTest: ScreenshotsTest() { | |
`then screenshot is OK`() | ||
} | ||
|
||
@Test | ||
fun `check ListRowItem with clickable asset`() { | ||
var clicked = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is acting as a boolean, not as an integer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not exactly as I want to know that it has been invoked only once. A boolean would only ensure it has been invoked but it could be multiple times. By doing it this way the condition is stronger. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition is exactly the same, you are checking if it has been clicked 1 time instead of 0. |
||
val onAssetClick: () -> Unit = { | ||
clicked++ | ||
} | ||
`when ListRowItem with asset`( | ||
dimensions = ImageDimensions(width = 44, height = 44), | ||
onAssetClick = onAssetClick, | ||
) | ||
composeTestRule.onNode(hasTestTag(LIST_ROW_ITEM_ASSET_TAG)).performClick() | ||
|
||
assertEquals(1, clicked) | ||
} | ||
Comment on lines
+46
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests that the asset is clickable on compose |
||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
private fun `when ListRowItem with asset`(dimensions: ImageDimensions) { | ||
private fun `when ListRowItem with asset`(dimensions: ImageDimensions, onAssetClick: () -> Unit = {}) { | ||
composeTestRule.setContent { | ||
MisticaTheme(brand = MovistarBrand) { | ||
ListRowItem( | ||
listRowIcon = ListRowIcon.ImageAsset( | ||
painter = painterResource(id = R.drawable.placeholder), | ||
dimensions = ImageDimensions(width = dimensions.width, height = dimensions.height), | ||
modifier = Modifier.testTag(LIST_ROW_ITEM_ASSET_TAG).clickable { onAssetClick() }, | ||
), | ||
headline = Tag("Promo"), | ||
isBadgeVisible = true, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import androidx.test.ext.junit.rules.activityScenarioRule | |
import com.telefonica.mistica.DummyActivity | ||
import com.telefonica.mistica.R | ||
import com.telefonica.mistica.testutils.ScreenshotsTest | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
@@ -26,4 +27,20 @@ internal class ListRowViewTest: ScreenshotsTest() { | |
compareScreenshot(Espresso.onView(ViewMatchers.withId(R.id.dummy_activity_wrapper))) | ||
} | ||
} | ||
|
||
@Test | ||
fun `check ListRowView xml onClick`() { | ||
rule.scenario.onActivity { activity -> | ||
var clicks = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, it is used as a boolean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same answer here 😄 |
||
val wrapper: FrameLayout = activity.findViewById(R.id.dummy_activity_wrapper) | ||
activity.layoutInflater.inflate(R.layout.test_list_row_view, wrapper, true) | ||
|
||
with (activity.findViewById<ListRowView>(R.id.list_row_view_32)) { | ||
setOnClickListener { clicks++ } | ||
performClick() | ||
} | ||
|
||
assertEquals(1, clicks) | ||
} | ||
} | ||
Comment on lines
+31
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests that the asset is clickable on xml |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is used only once, there is no need to declare it as a property
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but can't ve moved to the same line it's used as that would not compile: