1
0
Fork 0

Fixes #11739: Fix flicker

master
Hakkı Kaan Çalışkan 2020-06-19 15:19:54 +03:00 committed by Emily Kager
parent b95804c33b
commit 3dc6c438f4
1 changed files with 15 additions and 31 deletions

View File

@ -16,24 +16,20 @@ import kotlinx.android.synthetic.main.mozac_ui_tabcounter_layout.view.*
import org.mozilla.fenix.R import org.mozilla.fenix.R
import java.text.NumberFormat import java.text.NumberFormat
open class TabCounter @JvmOverloads constructor( class TabCounter @JvmOverloads constructor(
context: Context, context: Context,
attrs: AttributeSet? = null, attrs: AttributeSet? = null,
defStyle: Int = 0 defStyle: Int = 0
) : RelativeLayout(context, attrs, defStyle) { ) : RelativeLayout(context, attrs, defStyle) {
private val animationSet: AnimatorSet private val animationSet: AnimatorSet
private var count: Int = 0
init { init {
val inflater = LayoutInflater.from(context) val inflater = LayoutInflater.from(context)
inflater.inflate(R.layout.mozac_ui_tabcounter_layout, this) inflater.inflate(R.layout.mozac_ui_tabcounter_layout, this)
val shiftThreeDp = TypedValue.applyDimension( // This is needed because without this counter box will be empty.
TypedValue.COMPLEX_UNIT_DIP, TWO_DIGIT_PADDING, context.resources.displayMetrics setCount(INTERNAL_COUNT)
).toInt()
counter_text.setPadding(0, shiftThreeDp, shiftThreeDp, 0)
updateContentDescription(0)
animationSet = createAnimatorSet() animationSet = createAnimatorSet()
} }
@ -47,29 +43,15 @@ open class TabCounter @JvmOverloads constructor(
} }
fun setCountWithAnimation(count: Int) { fun setCountWithAnimation(count: Int) {
updateContentDescription(count)
// Don't animate from initial state.
if (this.count == 0) {
setCount(count) 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. // Cancel previous animations if necessary.
if (animationSet.isRunning) { if (animationSet.isRunning) {
animationSet.cancel() animationSet.cancel()
@ -81,9 +63,8 @@ open class TabCounter @JvmOverloads constructor(
fun setCount(count: Int) { fun setCount(count: Int) {
updateContentDescription(count) updateContentDescription(count)
adjustTextSize(count) adjustTextSize(count)
counter_text.text = formatForDisplay(count) counter_text.text = formatForDisplay(count)
this.count = count INTERNAL_COUNT = count
} }
private fun createAnimatorSet(): AnimatorSet { private fun createAnimatorSet(): AnimatorSet {
@ -197,7 +178,7 @@ open class TabCounter @JvmOverloads constructor(
private fun formatForDisplay(count: Int): String { private fun formatForDisplay(count: Int): String {
return if (count > MAX_VISIBLE_TABS) { 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 SO_MANY_TABS_OPEN
} else NumberFormat.getInstance().format(count.toLong()) } else NumberFormat.getInstance().format(count.toLong())
} }
@ -217,13 +198,16 @@ open class TabCounter @JvmOverloads constructor(
} }
companion object { companion object {
internal var INTERNAL_COUNT = 0
internal const val MAX_VISIBLE_TABS = 99 internal const val MAX_VISIBLE_TABS = 99
internal const val SO_MANY_TABS_OPEN = "" 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 ONE_DIGIT_SIZE_RATIO = 0.5f
internal const val TWO_DIGITS_SIZE_RATIO = 0.4f 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 internal const val TWO_DIGITS_TAB_COUNT_THRESHOLD = 10
// createBoxAnimatorSet // createBoxAnimatorSet