Restore bottom nav position earlier after being recreated (#6648)

This commit is contained in:
Ivan Iskandar 2022-02-18 10:08:36 +07:00 committed by GitHub
parent 57c07250fd
commit a8ad19a89d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,7 +10,6 @@ import android.util.AttributeSet
import android.view.ViewPropertyAnimator import android.view.ViewPropertyAnimator
import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.doOnLayout import androidx.core.view.doOnLayout
import androidx.core.view.doOnNextLayout
import androidx.core.view.updateLayoutParams import androidx.core.view.updateLayoutParams
import androidx.customview.view.AbsSavedState import androidx.customview.view.AbsSavedState
import androidx.interpolator.view.animation.FastOutLinearInInterpolator import androidx.interpolator.view.animation.FastOutLinearInInterpolator
@ -60,19 +59,15 @@ class TachiyomiBottomNavigationView @JvmOverloads constructor(
val superState = super.onSaveInstanceState() val superState = super.onSaveInstanceState()
return SavedState(superState).also { return SavedState(superState).also {
it.currentState = currentState it.currentState = currentState
it.translationY = translationY
} }
} }
override fun onRestoreInstanceState(state: Parcelable?) { override fun onRestoreInstanceState(state: Parcelable?) {
if (state is SavedState) { if (state is SavedState) {
super.onRestoreInstanceState(state.superState) super.onRestoreInstanceState(state.superState)
doOnNextLayout { super.setTranslationY(state.translationY)
if (state.currentState == STATE_UP) { currentState = state.currentState
slideUp(animate = false)
} else if (state.currentState == STATE_DOWN) {
slideDown(animate = false)
}
}
} else { } else {
super.onRestoreInstanceState(state) super.onRestoreInstanceState(state)
} }
@ -86,34 +81,30 @@ class TachiyomiBottomNavigationView @JvmOverloads constructor(
/** /**
* Shows this view up. * Shows this view up.
*
* @param animate True if slide up should be animated
*/ */
fun slideUp(animate: Boolean = true) = post { fun slideUp() = post {
currentAnimator?.cancel() currentAnimator?.cancel()
clearAnimation() clearAnimation()
currentState = STATE_UP currentState = STATE_UP
animateTranslation( animateTranslation(
0F, 0F,
if (animate) SLIDE_UP_ANIMATION_DURATION else 0, SLIDE_UP_ANIMATION_DURATION,
LinearOutSlowInInterpolator() LinearOutSlowInInterpolator()
) )
} }
/** /**
* Hides this view down. [setTranslationY] won't work until [slideUp] is called. * Hides this view down. [setTranslationY] won't work until [slideUp] is called.
*
* @param animate True if slide down should be animated
*/ */
fun slideDown(animate: Boolean = true) = post { fun slideDown() = post {
currentAnimator?.cancel() currentAnimator?.cancel()
clearAnimation() clearAnimation()
currentState = STATE_DOWN currentState = STATE_DOWN
animateTranslation( animateTranslation(
height.toFloat(), height.toFloat(),
if (animate) SLIDE_DOWN_ANIMATION_DURATION else 0, SLIDE_DOWN_ANIMATION_DURATION,
FastOutLinearInInterpolator() FastOutLinearInInterpolator()
) )
} }
@ -134,16 +125,19 @@ class TachiyomiBottomNavigationView @JvmOverloads constructor(
internal class SavedState : AbsSavedState { internal class SavedState : AbsSavedState {
var currentState = STATE_UP var currentState = STATE_UP
var translationY = 0F
constructor(superState: Parcelable) : super(superState) constructor(superState: Parcelable) : super(superState)
constructor(source: Parcel, loader: ClassLoader?) : super(source, loader) { constructor(source: Parcel, loader: ClassLoader?) : super(source, loader) {
currentState = source.readByte().toInt() currentState = source.readInt()
translationY = source.readFloat()
} }
override fun writeToParcel(out: Parcel, flags: Int) { override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags) super.writeToParcel(out, flags)
out.writeByte(currentState.toByte()) out.writeInt(currentState)
out.writeFloat(translationY)
} }
companion object { companion object {