Skip to content

Commit

Permalink
Implement CompactChip Component google#1396
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitMandalia committed Aug 27, 2023
1 parent 87fc4cc commit 3029cdd
Show file tree
Hide file tree
Showing 4 changed files with 468 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.compose.material

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.runtime.Composable
import com.google.android.horologist.compose.tools.WearPreview

@WearPreview
@Composable
fun CompactChipPreview() {
CompactChip(
onClick = { },
label = "Primary label"
)
}

@WearPreview
@Composable
fun CompactChipPreviewWithIcon() {
CompactChip(
onClick = { },
label = "Primary label",
icon = Icons.Filled.Add
)
}

@WearPreview
@Composable
fun CompactChipPreviewIconOnly() {
CompactChip(
onClick = { },
icon = Icons.Filled.Add
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.compose.material

import androidx.annotation.StringRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.wear.compose.material.ChipBorder
import androidx.wear.compose.material.ChipColors
import androidx.wear.compose.material.ChipDefaults
import androidx.wear.compose.material.CompactChip
import androidx.wear.compose.material.LocalContentAlpha
import androidx.wear.compose.material.Text
import coil.compose.rememberAsyncImagePainter
import com.google.android.horologist.annotations.ExperimentalHorologistApi
import com.google.android.horologist.compose.material.util.DECORATIVE_ELEMENT_CONTENT_DESCRIPTION

@ExperimentalHorologistApi
@Composable
public fun CompactChip(
onClick: () -> Unit,
modifier: Modifier = Modifier,
label: String? = null,
icon: Any? = null,
iconRtlMode: IconRtlMode = IconRtlMode.Default,
placeholder: Painter? = null,
colors: ChipColors = ChipDefaults.primaryChipColors(),
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
border: ChipBorder = ChipDefaults.chipBorder()
) {
val iconParam: (@Composable BoxScope.() -> Unit)? =
icon?.let {
{
Row {
val iconModifier = Modifier
.size(ChipDefaults.SmallIconSize)
when (icon) {
is ImageVector ->
Icon(
imageVector = icon,
contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION,
modifier = iconModifier,
rtlMode = iconRtlMode
)

is Int ->
Icon(
id = icon,
contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION,
modifier = iconModifier
)

else ->
Image(
painter = rememberAsyncImagePainter(
model = icon,
placeholder = placeholder
),
contentDescription = DECORATIVE_ELEMENT_CONTENT_DESCRIPTION,
modifier = iconModifier,
contentScale = ContentScale.Crop,
alpha = LocalContentAlpha.current
)
}
}
}
}
val hasIcon = icon != null

val labelParam: (@Composable RowScope.() -> Unit)? =
label?.let {
{
Text(
modifier = Modifier.fillMaxWidth(),
text = label,
textAlign = if (hasIcon) TextAlign.Start else TextAlign.Center,
overflow = TextOverflow.Ellipsis,
maxLines = 1
)
}
}

CompactChip(
modifier = modifier,
onClick = onClick,
label = labelParam,
icon = iconParam,
colors = colors,
enabled = enabled,
interactionSource = interactionSource,
border = border
)
}

/**
* This component is an alternative to [CompactChip], providing the following:
* - a convenient way of providing a string resource label;
* - a convenient way of providing an icon and a placeholder, and choosing their size based on the
* sizes recommended by the Wear guidelines;
*/
@ExperimentalHorologistApi
@Composable
public fun CompactChip(
@StringRes labelId: Int,
onClick: () -> Unit,
modifier: Modifier = Modifier,
icon: Any? = null,
iconRtlMode: IconRtlMode = IconRtlMode.Default,
placeholder: Painter? = null,
colors: ChipColors = ChipDefaults.primaryChipColors(),
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
border: ChipBorder = ChipDefaults.chipBorder()
) {
CompactChip(
onClick = onClick,
modifier = modifier,
label = stringResource(id = labelId),
icon = icon,
iconRtlMode = iconRtlMode,
placeholder = placeholder,
colors = colors,
enabled = enabled,
interactionSource = interactionSource,
border = border
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.compose.material

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.google.android.horologist.screenshots.ScreenshotBaseTest
import com.google.android.horologist.screenshots.ScreenshotTestRule
import org.junit.Test

class CompactChipA11yTest : ScreenshotBaseTest(
ScreenshotTestRule.screenshotTestRuleParams {
enableA11y = true
screenTimeText = {}
}
) {
@Test
fun withIcon() {
screenshotTestRule.setContent(takeScreenshot = true) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
CompactChip(
onClick = { },
label = "Primary label",
icon = Icons.Filled.Add
)
}
}
}

@Test
fun disabled() {
screenshotTestRule.setContent(takeScreenshot = true) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
CompactChip(
onClick = { },
label = "Primary label",
icon = Icons.Filled.Add,
enabled = false
)
}
}
}
}
Loading

0 comments on commit 3029cdd

Please sign in to comment.