Skip to content

Commit

Permalink
Fixes mozilla-mobile#11739: Fix flicker
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakkı Kaan Çalışkan authored and ekager committed Jun 25, 2020
1 parent 29b49b5 commit ad43dac
Showing 1 changed file with 15 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -47,29 +43,15 @@ open class TabCounter @JvmOverloads constructor(
}

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

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

if (this.count == 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.
}

// 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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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())
}
Expand All @@ -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
Expand Down

0 comments on commit ad43dac

Please sign in to comment.