Fix 4827 - Use ktx doOnPreDraw() to restore HomeFragment's layout
The previous solution would result in a crash because the passed in viewTreeObserver that would trigger onPreDraw would be invalid. The proposed solution is simpler and ensures we'll always use the right viewTreeObserver. `FragmentPreDrawManager` is general enough that can be used by other Fragments also, so I've added it to the `utils` package.nightly-build-test
parent
115833c78a
commit
ab6e7dc52b
@ -1,52 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.home
|
||||
|
||||
import android.view.ViewTreeObserver
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleObserver
|
||||
import androidx.lifecycle.OnLifecycleEvent
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class TransitionPreDrawListener(
|
||||
private val fragment: Fragment,
|
||||
private val viewTreeObserver: ViewTreeObserver,
|
||||
private val restoreLayoutState: () -> Unit
|
||||
) : ViewTreeObserver.OnPreDrawListener, LifecycleObserver {
|
||||
|
||||
init {
|
||||
fragment.viewLifecycleOwner.lifecycle.addObserver(this)
|
||||
}
|
||||
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
|
||||
fun onCreateView() {
|
||||
viewTreeObserver.addOnPreDrawListener(this)
|
||||
}
|
||||
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
||||
fun onDestroyView() {
|
||||
viewTreeObserver.removeOnPreDrawListener(this)
|
||||
}
|
||||
|
||||
override fun onPreDraw(): Boolean {
|
||||
if (fragment.view != null) {
|
||||
fragment.viewLifecycleOwner.lifecycleScope.launch {
|
||||
delay(ANIM_SCROLL_DELAY)
|
||||
restoreLayoutState()
|
||||
fragment.startPostponedEnterTransition()
|
||||
}.invokeOnCompletion {
|
||||
viewTreeObserver.removeOnPreDrawListener(this)
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ANIM_SCROLL_DELAY = 100L
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.utils
|
||||
|
||||
import androidx.core.view.doOnPreDraw
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* Helper class that allows executing code immediately before [Fragment]s View being drawn.
|
||||
*/
|
||||
class FragmentPreDrawManager(
|
||||
private val fragment: Fragment
|
||||
) {
|
||||
init {
|
||||
fragment.postponeEnterTransition()
|
||||
}
|
||||
|
||||
fun execute(code: () -> Unit) {
|
||||
fragment.view?.doOnPreDraw {
|
||||
fragment.viewLifecycleOwner.lifecycleScope.launch {
|
||||
delay(ANIM_SCROLL_DELAY)
|
||||
code()
|
||||
fragment.startPostponedEnterTransition()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ANIM_SCROLL_DELAY = 100L
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue