home: fix settings anim

Fix mismatched navigation animations when going to settings and back.
This commit is contained in:
Alexander Capehart 2023-07-03 10:45:32 -06:00
parent a1efb0c34a
commit 6dc2892eb9
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
2 changed files with 26 additions and 20 deletions

View file

@ -34,7 +34,6 @@ import androidx.navigation.findNavController
import com.google.android.material.R as MR import com.google.android.material.R as MR
import com.google.android.material.bottomsheet.BackportBottomSheetBehavior import com.google.android.material.bottomsheet.BackportBottomSheetBehavior
import com.google.android.material.shape.MaterialShapeDrawable import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.transition.MaterialFadeThrough
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
@ -79,12 +78,6 @@ class MainFragment :
private var elevationNormal = 0f private var elevationNormal = 0f
private var initialNavDestinationChange = true private var initialNavDestinationChange = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enterTransition = MaterialFadeThrough()
exitTransition = MaterialFadeThrough()
}
override fun onCreateBinding(inflater: LayoutInflater) = FragmentMainBinding.inflate(inflater) override fun onCreateBinding(inflater: LayoutInflater) = FragmentMainBinding.inflate(inflater)
override fun onBindingCreated(binding: FragmentMainBinding, savedInstanceState: Bundle?) { override fun onBindingCreated(binding: FragmentMainBinding, savedInstanceState: Bundle?) {

View file

@ -38,6 +38,7 @@ import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2 import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.appbar.AppBarLayout import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.tabs.TabLayoutMediator import com.google.android.material.tabs.TabLayoutMediator
import com.google.android.material.transition.MaterialFadeThrough
import com.google.android.material.transition.MaterialSharedAxis import com.google.android.material.transition.MaterialSharedAxis
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import java.lang.reflect.Field import java.lang.reflect.Field
@ -100,9 +101,11 @@ class HomeFragment :
// Orientation change will wipe whatever transition we were using prior, which will // Orientation change will wipe whatever transition we were using prior, which will
// result in no transition when the user navigates back. Make sure we re-initialize // result in no transition when the user navigates back. Make sure we re-initialize
// our transitions. // our transitions.
val axis = savedInstanceState.getInt(KEY_LAST_TRANSITION_AXIS, -1) val id = savedInstanceState.getInt(KEY_LAST_TRANSITION_ID, -2)
if (axis > -1) { when (id) {
setupAxisTransitions(axis) -2 -> {}
-1 -> applyFadeTransition()
else -> applyAxisTransition(id)
} }
} }
} }
@ -179,9 +182,9 @@ class HomeFragment :
} }
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {
val enter = enterTransition when (val transition = enterTransition) {
if (enter is MaterialSharedAxis) { is MaterialFadeThrough -> outState.putInt(KEY_LAST_TRANSITION_ID, -1)
outState.putInt(KEY_LAST_TRANSITION_AXIS, enter.axis) is MaterialSharedAxis -> outState.putInt(KEY_LAST_TRANSITION_ID, transition.axis)
} }
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
@ -214,17 +217,19 @@ class HomeFragment :
// Handle main actions (Search, Settings, About) // Handle main actions (Search, Settings, About)
R.id.action_search -> { R.id.action_search -> {
logD("Navigating to search") logD("Navigating to search")
setupAxisTransitions(MaterialSharedAxis.Z) applyAxisTransition(MaterialSharedAxis.Z)
findNavController().navigateSafe(HomeFragmentDirections.search()) findNavController().navigateSafe(HomeFragmentDirections.search())
true true
} }
R.id.action_settings -> { R.id.action_settings -> {
logD("Navigating to preferences") logD("Navigating to preferences")
applyFadeTransition()
findNavController().navigateSafe(HomeFragmentDirections.preferences()) findNavController().navigateSafe(HomeFragmentDirections.preferences())
true true
} }
R.id.action_about -> { R.id.action_about -> {
logD("Navigating to about") logD("Navigating to about")
applyFadeTransition()
findNavController().navigateSafe(HomeFragmentDirections.about()) findNavController().navigateSafe(HomeFragmentDirections.about())
true true
} }
@ -537,7 +542,7 @@ class HomeFragment :
// fragment should be launched otherwise. // fragment should be launched otherwise.
is Show.SongAlbumDetails -> { is Show.SongAlbumDetails -> {
logD("Navigating to the album of ${show.song}") logD("Navigating to the album of ${show.song}")
setupAxisTransitions(MaterialSharedAxis.X) applyAxisTransition(MaterialSharedAxis.X)
findNavController() findNavController()
.navigateSafe(HomeFragmentDirections.showAlbum(show.song.album.uid)) .navigateSafe(HomeFragmentDirections.showAlbum(show.song.album.uid))
} }
@ -546,14 +551,14 @@ class HomeFragment :
// detail fragment. // detail fragment.
is Show.AlbumDetails -> { is Show.AlbumDetails -> {
logD("Navigating to ${show.album}") logD("Navigating to ${show.album}")
setupAxisTransitions(MaterialSharedAxis.X) applyAxisTransition(MaterialSharedAxis.X)
findNavController().navigateSafe(HomeFragmentDirections.showAlbum(show.album.uid)) findNavController().navigateSafe(HomeFragmentDirections.showAlbum(show.album.uid))
} }
// Always launch a new ArtistDetailFragment. // Always launch a new ArtistDetailFragment.
is Show.ArtistDetails -> { is Show.ArtistDetails -> {
logD("Navigating to ${show.artist}") logD("Navigating to ${show.artist}")
setupAxisTransitions(MaterialSharedAxis.X) applyAxisTransition(MaterialSharedAxis.X)
findNavController().navigateSafe(HomeFragmentDirections.showArtist(show.artist.uid)) findNavController().navigateSafe(HomeFragmentDirections.showArtist(show.artist.uid))
} }
is Show.SongArtistDetails -> { is Show.SongArtistDetails -> {
@ -566,10 +571,12 @@ class HomeFragment :
} }
is Show.GenreDetails -> { is Show.GenreDetails -> {
logD("Navigating to ${show.genre}") logD("Navigating to ${show.genre}")
applyAxisTransition(MaterialSharedAxis.X)
findNavController().navigateSafe(HomeFragmentDirections.showGenre(show.genre.uid)) findNavController().navigateSafe(HomeFragmentDirections.showGenre(show.genre.uid))
} }
is Show.PlaylistDetails -> { is Show.PlaylistDetails -> {
logD("Navigating to ${show.playlist}") logD("Navigating to ${show.playlist}")
applyAxisTransition(MaterialSharedAxis.X)
findNavController() findNavController()
.navigateSafe(HomeFragmentDirections.showPlaylist(show.playlist.uid)) .navigateSafe(HomeFragmentDirections.showPlaylist(show.playlist.uid))
} }
@ -591,7 +598,7 @@ class HomeFragment :
} }
} }
private fun setupAxisTransitions(axis: Int) { private fun applyAxisTransition(axis: Int) {
// Sanity check to avoid in-correct axis transitions // Sanity check to avoid in-correct axis transitions
check(axis == MaterialSharedAxis.X || axis == MaterialSharedAxis.Z) { check(axis == MaterialSharedAxis.X || axis == MaterialSharedAxis.Z) {
"Not expecting Y axis transition" "Not expecting Y axis transition"
@ -603,6 +610,13 @@ class HomeFragment :
reenterTransition = MaterialSharedAxis(axis, false) reenterTransition = MaterialSharedAxis(axis, false)
} }
private fun applyFadeTransition() {
enterTransition = MaterialFadeThrough()
returnTransition = MaterialFadeThrough()
exitTransition = MaterialFadeThrough()
reenterTransition = MaterialFadeThrough()
}
/** /**
* [FragmentStateAdapter] implementation for the [HomeFragment]'s [ViewPager2] instance. * [FragmentStateAdapter] implementation for the [HomeFragment]'s [ViewPager2] instance.
* *
@ -630,7 +644,6 @@ class HomeFragment :
private companion object { private companion object {
val VP_RECYCLER_FIELD: Field by lazyReflectedField(ViewPager2::class, "mRecyclerView") val VP_RECYCLER_FIELD: Field by lazyReflectedField(ViewPager2::class, "mRecyclerView")
val RV_TOUCH_SLOP_FIELD: Field by lazyReflectedField(RecyclerView::class, "mTouchSlop") val RV_TOUCH_SLOP_FIELD: Field by lazyReflectedField(RecyclerView::class, "mTouchSlop")
const val KEY_LAST_TRANSITION_AXIS = const val KEY_LAST_TRANSITION_ID = BuildConfig.APPLICATION_ID + ".key.LAST_TRANSITION_AXIS"
BuildConfig.APPLICATION_ID + ".key.LAST_TRANSITION_AXIS"
} }
} }