Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,3 @@ internal fun PagingData<ChatListItem.ContentBubble>.withSeparators(
insertSeparators { newer: ChatListItem.ContentBubble?, older: ChatListItem.ContentBubble? ->
separatorBetween(newer, older, boundary, config)
}

/** The count the divider shows: past 99 it reads "99+". */
internal fun unreadCountLabel(count: Int): String = if (count > 99) "99+" else count.toString()
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewWrapper
import com.flipcash.app.messenger.internal.unreadCountLabel
import com.flipcash.app.theme.FlipcashThemeWrapper
import com.flipcash.features.messenger.R
import com.getcode.theme.CodeTheme
import com.getcode.ui.components.unreadCountLabel
import kotlin.time.Clock
import kotlin.time.Instant

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,4 @@ class UnreadDividerPlacementTest {
separatorBetween(newer, older, UnreadBoundary.None, SeparatorConfig.DayOnly),
)
}

@Test
fun `counts over 99 read 99+`() {
assertEquals("1", unreadCountLabel(1))
assertEquals("99", unreadCountLabel(99))
assertEquals("99+", unreadCountLabel(100))
}
}
10 changes: 4 additions & 6 deletions ui/components/src/main/kotlin/com/getcode/ui/components/Badge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ private fun Modifier.squareMinSize(): Modifier = layout { measurable, constraint
}
}

/** An unread count as a badge or divider shows it: past 99 it reads "99+". */
fun unreadCountLabel(count: Int): String = if (count > 99) "99+" else count.toString()

@Composable
fun Badge(
count: Int,
modifier: Modifier = Modifier,
showMoreUnread: Boolean = count > 100,
color: Color = CodeTheme.colors.brand,
contentColor: Color = Color.White,
textStyle: TextStyle = CodeTheme.typography.caption.copy(fontWeight = FontWeight.SemiBold),
Expand All @@ -66,11 +68,7 @@ fun Badge(
enter = enterTransition,
exit = exitTransition
) {
val text = when {
count == 0 -> ""
showMoreUnread -> "$count+"
else -> "$count"
}
val text = if (count == 0) "" else unreadCountLabel(count)

val scaledTextStyle = if (scale == 1f) textStyle else textStyle.copy(
fontSize = textStyle.fontSize * scale
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.getcode.ui.components

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.assertHeightIsEqualTo
import androidx.compose.ui.test.getBoundsInRoot
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.unit.dp
import com.getcode.theme.DesignSystem
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import kotlin.test.assertEquals
import kotlin.test.assertTrue

@RunWith(RobolectricTestRunner::class)
class BadgeTest {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun `counts up to 99 read as the number`() {
assertEquals("1", unreadCountLabel(1))
assertEquals("99", unreadCountLabel(99))
}

@Test
fun `counts over 99 read 99+`() {
assertEquals("99+", unreadCountLabel(100))
assertEquals("99+", unreadCountLabel(150))
}

@Test
fun `badge shows 99 at 99`() = assertBadgeText(count = 99, expected = "99")

@Test
fun `badge caps at 99+ at 100`() = assertBadgeText(count = 100, expected = "99+")

@Test
fun `badge caps at 99+ at 150`() = assertBadgeText(count = 150, expected = "99+")

// The chat row pill (`UnreadBadge`): an 18dp fixed height with 4dp side padding. At the cap it
// must stay 18dp tall and wide enough to hold the whole label.
@Test
fun `99+ keeps the 18dp pill height and fits inside it`() {
composeTestRule.setContent {
DesignSystem {
Badge(
modifier = Modifier.testTag("badge"),
count = 150,
contentPadding = PaddingValues(horizontal = 4.dp),
height = 18.dp,
)
}
}
composeTestRule.waitForIdle()

val pill = composeTestRule.onNodeWithTag("badge").assertHeightIsEqualTo(18.dp).getBoundsInRoot()
val label = composeTestRule.onNodeWithText("99+").getBoundsInRoot()
assertTrue(label.left >= pill.left && label.right <= pill.right, "label $label overflows pill $pill")
assertTrue(label.top >= pill.top && label.bottom <= pill.bottom, "label $label overflows pill $pill")
}

private fun assertBadgeText(count: Int, expected: String) {
composeTestRule.setContent { DesignSystem { Badge(count = count) } }
composeTestRule.waitForIdle()
composeTestRule.onNodeWithText(expected).assertExists()
}
}
Loading