Skip to content

Commit

Permalink
ANDROID-14752 Fix Badge (#368)
Browse files Browse the repository at this point in the history
* ANDROID-14752 Add a button to reproduce a bug

* ANDROID-14752 Clear old badge before add another one
  • Loading branch information
dagonco authored Jun 25, 2024
1 parent 38f8064 commit b50e2eb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,18 @@ class BadgesCatalogFragment : Fragment() {
badgeVisible = !badgeVisible
}
}
findViewById<Button>(R.id.button_add_numeric_badge).apply {
setOnClickListener {
if (!badgeVisible) {
Badge.showNumericBadgeIn(imageView, imageContainer, Random(System.currentTimeMillis()).nextInt(1, 11))
} else {
Badge.removeBadge(imageView)
}
badgeVisible = !badgeVisible
}
findViewById<Button>(R.id.button_add_one_digit_numeric_badge).setOnClickListener {
Badge.showNumericBadgeIn(imageView, imageContainer, Random(System.currentTimeMillis()).nextInt(1, 9))
badgeVisible = true
}
findViewById<Button>(R.id.button_remove_badge_if_numeric_with_count_zero).apply {
setOnClickListener {
Badge.showNumericBadgeIn(imageView, imageContainer, 0)
badgeVisible = false
}

findViewById<Button>(R.id.button_add_two_digit_numeric_badge).setOnClickListener {
Badge.showNumericBadgeIn(imageView, imageContainer, Random(System.currentTimeMillis()).nextInt(10, 20))
badgeVisible = true
}
findViewById<Button>(R.id.button_remove_badge_if_numeric_with_count_zero).setOnClickListener {
Badge.showNumericBadgeIn(imageView, imageContainer, 0)
badgeVisible = false
}
}
}
11 changes: 9 additions & 2 deletions catalog/src/main/res/layout/screen_badges_catalog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@
android:text="Toggle" />

<Button
android:id="@+id/button_add_numeric_badge"
android:id="@+id/button_add_one_digit_numeric_badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Toggle With Number > 0" />
android:text="Show with one digit number" />

<Button
android:id="@+id/button_add_two_digit_numeric_badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Show with two digit number " />

<Button
android:id="@+id/button_remove_badge_if_numeric_with_count_zero"
Expand Down
5 changes: 3 additions & 2 deletions library/src/main/java/com/telefonica/mistica/badge/Badge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ object Badge {
private fun buildBadgeContentDescription(
anchor: View,
count: Int,
badgeDescription: String?
badgeDescription: String?,
): String {
if (!contentDescriptions.containsKey(anchor.hashCode())) {
contentDescriptions[anchor.hashCode()] = anchor.contentDescription
Expand All @@ -90,7 +90,7 @@ object Badge {

private fun getDefaultBadgeDescription(
anchor: View,
count: Int
count: Int,
) = if (count == 0) {
anchor.context.getString(R.string.badge_notification_description)
} else {
Expand Down Expand Up @@ -119,6 +119,7 @@ object Badge {
parent.post {
this.setBoundsFor(anchor, parent)
val overlay = parent.overlay
overlay.clear()
overlay.add(this)
anchor.contentDescription = contentDescription
}
Expand Down

0 comments on commit b50e2eb

Please sign in to comment.