home: fix crash when recreating viewpager
Fix a crash where all HomeListFragments would lose their displayModes when the activity was recreated [such as in settings].
This commit is contained in:
parent
19e2fcbb90
commit
504e8260ac
3 changed files with 26 additions and 17 deletions
|
@ -53,11 +53,7 @@ import org.oxycblt.auxio.recycler.DisplayMode
|
||||||
class HomeFragment : Fragment() {
|
class HomeFragment : Fragment() {
|
||||||
private val playbackModel: PlaybackViewModel by activityViewModels()
|
private val playbackModel: PlaybackViewModel by activityViewModels()
|
||||||
private val detailModel: DetailViewModel by activityViewModels()
|
private val detailModel: DetailViewModel by activityViewModels()
|
||||||
|
private val homeModel: HomeViewModel by activityViewModels()
|
||||||
private val tabs = arrayOf(
|
|
||||||
DisplayMode.SHOW_SONGS, DisplayMode.SHOW_ALBUMS,
|
|
||||||
DisplayMode.SHOW_ARTISTS, DisplayMode.SHOW_GENRES
|
|
||||||
)
|
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater,
|
inflater: LayoutInflater,
|
||||||
|
@ -94,7 +90,7 @@ class HomeFragment : Fragment() {
|
||||||
|
|
||||||
// By default, ViewPager2's sensitivity is high enough to result in vertical
|
// By default, ViewPager2's sensitivity is high enough to result in vertical
|
||||||
// scroll events being registered as horizontal scroll events. Reflect into the
|
// scroll events being registered as horizontal scroll events. Reflect into the
|
||||||
// internal recyclerview and change the touch slope so that touch actions will
|
// internal recyclerview and change the touch slope so that touch actions will
|
||||||
// act more as a scroll than as a swipe.
|
// act more as a scroll than as a swipe.
|
||||||
// Derived from: https://al-e-shevelev.medium.com/how-to-reduce-scroll-sensitivity-of-viewpager2-widget-87797ad02414
|
// Derived from: https://al-e-shevelev.medium.com/how-to-reduce-scroll-sensitivity-of-viewpager2-widget-87797ad02414
|
||||||
|
|
||||||
|
@ -117,7 +113,7 @@ class HomeFragment : Fragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
TabLayoutMediator(binding.homeTabs, binding.homePager) { tab, pos ->
|
TabLayoutMediator(binding.homeTabs, binding.homePager) { tab, pos ->
|
||||||
val labelRes = when (tabs[pos]) {
|
val labelRes = when (requireNotNull(homeModel.tabs.value)[pos]) {
|
||||||
DisplayMode.SHOW_SONGS -> R.string.lbl_songs
|
DisplayMode.SHOW_SONGS -> R.string.lbl_songs
|
||||||
DisplayMode.SHOW_ALBUMS -> R.string.lbl_albums
|
DisplayMode.SHOW_ALBUMS -> R.string.lbl_albums
|
||||||
DisplayMode.SHOW_ARTISTS -> R.string.lbl_artists
|
DisplayMode.SHOW_ARTISTS -> R.string.lbl_artists
|
||||||
|
@ -162,7 +158,7 @@ class HomeFragment : Fragment() {
|
||||||
private inner class HomePagerAdapter :
|
private inner class HomePagerAdapter :
|
||||||
FragmentStateAdapter(childFragmentManager, viewLifecycleOwner.lifecycle) {
|
FragmentStateAdapter(childFragmentManager, viewLifecycleOwner.lifecycle) {
|
||||||
|
|
||||||
override fun getItemCount(): Int = tabs.size
|
override fun getItemCount(): Int = requireNotNull(homeModel.tabs.value).size
|
||||||
override fun createFragment(position: Int): Fragment = HomeListFragment.new(tabs[position])
|
override fun createFragment(position: Int): Fragment = HomeListFragment.new(position)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.viewModels
|
import androidx.fragment.app.viewModels
|
||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
|
import org.oxycblt.auxio.BuildConfig
|
||||||
import org.oxycblt.auxio.databinding.FragmentHomeListBinding
|
import org.oxycblt.auxio.databinding.FragmentHomeListBinding
|
||||||
import org.oxycblt.auxio.logD
|
import org.oxycblt.auxio.logD
|
||||||
import org.oxycblt.auxio.music.Album
|
import org.oxycblt.auxio.music.Album
|
||||||
|
@ -37,15 +38,13 @@ import org.oxycblt.auxio.recycler.DisplayMode
|
||||||
import org.oxycblt.auxio.spans
|
import org.oxycblt.auxio.spans
|
||||||
import org.oxycblt.auxio.ui.newMenu
|
import org.oxycblt.auxio.ui.newMenu
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Fragment that contains a list of items specified by a [DisplayMode].
|
* Fragment that contains a list of items specified by a [DisplayMode]. This fragment
|
||||||
* TODO: Fix crash from not saving the display mode. This is getting really tiring.
|
* should be created using the [new] method with it's position in the ViewPager.
|
||||||
* Just keep the index for the tab we're working with and then just use that w/homeModel.
|
|
||||||
*/
|
*/
|
||||||
class HomeListFragment : Fragment() {
|
class HomeListFragment : Fragment() {
|
||||||
private val homeModel: HomeViewModel by viewModels()
|
private val homeModel: HomeViewModel by viewModels()
|
||||||
private val playbackModel: PlaybackViewModel by viewModels()
|
private val playbackModel: PlaybackViewModel by viewModels()
|
||||||
private lateinit var displayMode: DisplayMode
|
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater,
|
inflater: LayoutInflater,
|
||||||
|
@ -91,7 +90,9 @@ class HomeListFragment : Fragment() {
|
||||||
|
|
||||||
// --- VIEWMODEL SETUP ---
|
// --- VIEWMODEL SETUP ---
|
||||||
|
|
||||||
val toObserve = when (displayMode) {
|
val pos = requireNotNull(arguments).getInt(ARG_POS)
|
||||||
|
|
||||||
|
val toObserve = when (requireNotNull(homeModel.tabs.value)[pos]) {
|
||||||
DisplayMode.SHOW_SONGS -> homeModel.songs
|
DisplayMode.SHOW_SONGS -> homeModel.songs
|
||||||
DisplayMode.SHOW_ALBUMS -> homeModel.albums
|
DisplayMode.SHOW_ALBUMS -> homeModel.albums
|
||||||
DisplayMode.SHOW_ARTISTS -> homeModel.artists
|
DisplayMode.SHOW_ARTISTS -> homeModel.artists
|
||||||
|
@ -111,12 +112,16 @@ class HomeListFragment : Fragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
private const val ARG_POS = BuildConfig.APPLICATION_ID + ".key.POS"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Instantiates this fragment for use in a ViewPager.
|
* Instantiates this fragment for use in a ViewPager.
|
||||||
*/
|
*/
|
||||||
fun new(mode: DisplayMode): HomeListFragment {
|
fun new(pos: Int): HomeListFragment {
|
||||||
val fragment = HomeListFragment()
|
val fragment = HomeListFragment()
|
||||||
fragment.displayMode = mode
|
fragment.arguments = Bundle().apply {
|
||||||
|
putInt(ARG_POS, pos)
|
||||||
|
}
|
||||||
return fragment
|
return fragment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import org.oxycblt.auxio.music.Artist
|
||||||
import org.oxycblt.auxio.music.Genre
|
import org.oxycblt.auxio.music.Genre
|
||||||
import org.oxycblt.auxio.music.MusicStore
|
import org.oxycblt.auxio.music.MusicStore
|
||||||
import org.oxycblt.auxio.music.Song
|
import org.oxycblt.auxio.music.Song
|
||||||
|
import org.oxycblt.auxio.recycler.DisplayMode
|
||||||
|
|
||||||
class HomeViewModel : ViewModel() {
|
class HomeViewModel : ViewModel() {
|
||||||
private val mGenres = MutableLiveData(listOf<Genre>())
|
private val mGenres = MutableLiveData(listOf<Genre>())
|
||||||
|
@ -40,6 +41,9 @@ class HomeViewModel : ViewModel() {
|
||||||
private val mSongs = MutableLiveData(listOf<Song>())
|
private val mSongs = MutableLiveData(listOf<Song>())
|
||||||
val songs: LiveData<List<Song>> get() = mSongs
|
val songs: LiveData<List<Song>> get() = mSongs
|
||||||
|
|
||||||
|
private val mTabs = MutableLiveData(arrayOf<DisplayMode>())
|
||||||
|
val tabs: LiveData<Array<DisplayMode>> = mTabs
|
||||||
|
|
||||||
private val musicStore = MusicStore.getInstance()
|
private val musicStore = MusicStore.getInstance()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
@ -47,5 +51,9 @@ class HomeViewModel : ViewModel() {
|
||||||
mArtists.value = musicStore.artists
|
mArtists.value = musicStore.artists
|
||||||
mAlbums.value = musicStore.albums
|
mAlbums.value = musicStore.albums
|
||||||
mSongs.value = musicStore.songs
|
mSongs.value = musicStore.songs
|
||||||
|
mTabs.value = arrayOf(
|
||||||
|
DisplayMode.SHOW_SONGS, DisplayMode.SHOW_ALBUMS,
|
||||||
|
DisplayMode.SHOW_ARTISTS, DisplayMode.SHOW_GENRES
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue