diff --git a/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounter.kt b/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounter.kt index ca6c4cf40..3677d6cde 100644 --- a/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounter.kt +++ b/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounter.kt @@ -16,24 +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) - 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() } @@ -47,29 +43,15 @@ open class TabCounter @JvmOverloads constructor( } fun setCountWithAnimation(count: Int) { - updateContentDescription(count) + setCount(count) - // Don't animate from initial state. - if (this.count == 0) { - setCount(count) - return + // No need to animate on these cases. + when { + INTERNAL_COUNT == 0 -> return // Initial state. + INTERNAL_COUNT == count -> return // There isn't any tab added or removed. + INTERNAL_COUNT > MAX_VISIBLE_TABS -> return // There are still over MAX_VISIBLE_TABS tabs open. } - if (this.count == count) { - return - } - - // 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 - // Cancel previous animations if necessary. if (animationSet.isRunning) { animationSet.cancel() @@ -81,9 +63,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 { @@ -197,7 +178,7 @@ open class TabCounter @JvmOverloads constructor( private fun formatForDisplay(count: Int): String { return if (count > MAX_VISIBLE_TABS) { - counter_text.setPadding(0,0,0,6) + counter_text.setPadding(0, 0, 0, INFINITE_CHAR_PADDING_BOTTOM) SO_MANY_TABS_OPEN } else NumberFormat.getInstance().format(count.toLong()) } @@ -217,13 +198,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 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