Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Fixes #11672, #11727: Delete default tab text and add padding to infinite character #11738

Closed
wants to merge 3 commits into from
Closed
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 @@ -16,25 +16,20 @@ import kotlinx.android.synthetic.main.mozac_ui_tabcounter_layout.view.*
import org.mozilla.fenix.R
import java.text.NumberFormat

open class TabCounter @JvmOverloads constructor(
class TabCounter @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : RelativeLayout(context, attrs, defStyle) {

private val animationSet: AnimatorSet
private var count: Int = 0

init {
val inflater = LayoutInflater.from(context)
inflater.inflate(R.layout.mozac_ui_tabcounter_layout, this)

counter_text.text = DEFAULT_TABS_COUNTER_TEXT
val shiftThreeDp = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, TWO_DIGIT_PADDING, context.resources.displayMetrics
).toInt()
counter_text.setPadding(0, shiftThreeDp, shiftThreeDp, 0)
updateContentDescription(0)
// This is needed because without this counter box will be empty.
setCount(INTERNAL_COUNT)

animationSet = createAnimatorSet()
}
Expand All @@ -48,28 +43,19 @@ open class TabCounter @JvmOverloads constructor(
}

fun setCountWithAnimation(count: Int) {
updateContentDescription(count)

// Don't animate from initial state.
if (this.count == 0) {
setCount(count)
return
}

if (this.count == count) {
return
// No need to animate on these cases.
when {
count == INTERNAL_COUNT -> return // There isn't any tab added or removed.
INTERNAL_COUNT == 0 -> {
setCount(count)
return
} // Initial state.
count > MAX_VISIBLE_TABS && INTERNAL_COUNT > MAX_VISIBLE_TABS -> {
INTERNAL_COUNT = count
return
} // There are still over MAX_VISIBLE_TABS tabs open.
}

// Don't animate if there are still over MAX_VISIBLE_TABS tabs open.
if (this.count > MAX_VISIBLE_TABS && count > MAX_VISIBLE_TABS) {
this.count = count
return
}

adjustTextSize(count)

counter_text.text = formatForDisplay(count)
this.count = count
setCount(count)

// Cancel previous animations if necessary.
if (animationSet.isRunning) {
Expand All @@ -82,9 +68,8 @@ open class TabCounter @JvmOverloads constructor(
fun setCount(count: Int) {
updateContentDescription(count)
adjustTextSize(count)

counter_text.text = formatForDisplay(count)
this.count = count
INTERNAL_COUNT = count
}

private fun createAnimatorSet(): AnimatorSet {
Expand Down Expand Up @@ -198,6 +183,7 @@ open class TabCounter @JvmOverloads constructor(

private fun formatForDisplay(count: Int): String {
return if (count > MAX_VISIBLE_TABS) {
counter_text.setPadding(0, 0, 0, INFINITE_CHAR_PADDING_BOTTOM)
SO_MANY_TABS_OPEN
} else NumberFormat.getInstance().format(count.toLong())
}
Expand All @@ -217,14 +203,16 @@ open class TabCounter @JvmOverloads constructor(
}

companion object {
internal var INTERNAL_COUNT = 0

internal const val MAX_VISIBLE_TABS = 99

internal const val SO_MANY_TABS_OPEN = "∞"
internal const val DEFAULT_TABS_COUNTER_TEXT = ":)"

internal const val INFINITE_CHAR_PADDING_BOTTOM = 6

internal const val ONE_DIGIT_SIZE_RATIO = 0.5f
internal const val TWO_DIGITS_SIZE_RATIO = 0.4f
internal const val TWO_DIGIT_PADDING = 3F
internal const val TWO_DIGITS_TAB_COUNT_THRESHOLD = 10

// createBoxAnimatorSet
Expand Down