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:
OxygenCobalt 2021-08-22 18:14:15 -06:00
parent 19e2fcbb90
commit 504e8260ac
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
3 changed files with 26 additions and 17 deletions

View file

@ -53,11 +53,7 @@ import org.oxycblt.auxio.recycler.DisplayMode
class HomeFragment : Fragment() {
private val playbackModel: PlaybackViewModel by activityViewModels()
private val detailModel: DetailViewModel by activityViewModels()
private val tabs = arrayOf(
DisplayMode.SHOW_SONGS, DisplayMode.SHOW_ALBUMS,
DisplayMode.SHOW_ARTISTS, DisplayMode.SHOW_GENRES
)
private val homeModel: HomeViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater,
@ -117,7 +113,7 @@ class HomeFragment : Fragment() {
}
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_ALBUMS -> R.string.lbl_albums
DisplayMode.SHOW_ARTISTS -> R.string.lbl_artists
@ -162,7 +158,7 @@ class HomeFragment : Fragment() {
private inner class HomePagerAdapter :
FragmentStateAdapter(childFragmentManager, viewLifecycleOwner.lifecycle) {
override fun getItemCount(): Int = tabs.size
override fun createFragment(position: Int): Fragment = HomeListFragment.new(tabs[position])
override fun getItemCount(): Int = requireNotNull(homeModel.tabs.value).size
override fun createFragment(position: Int): Fragment = HomeListFragment.new(position)
}
}

View file

@ -26,6 +26,7 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import org.oxycblt.auxio.BuildConfig
import org.oxycblt.auxio.databinding.FragmentHomeListBinding
import org.oxycblt.auxio.logD
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.ui.newMenu
/*
* Fragment that contains a list of items specified by a [DisplayMode].
* TODO: Fix crash from not saving the display mode. This is getting really tiring.
* Just keep the index for the tab we're working with and then just use that w/homeModel.
/**
* Fragment that contains a list of items specified by a [DisplayMode]. This fragment
* should be created using the [new] method with it's position in the ViewPager.
*/
class HomeListFragment : Fragment() {
private val homeModel: HomeViewModel by viewModels()
private val playbackModel: PlaybackViewModel by viewModels()
private lateinit var displayMode: DisplayMode
override fun onCreateView(
inflater: LayoutInflater,
@ -91,7 +90,9 @@ class HomeListFragment : Fragment() {
// --- 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_ALBUMS -> homeModel.albums
DisplayMode.SHOW_ARTISTS -> homeModel.artists
@ -111,12 +112,16 @@ class HomeListFragment : Fragment() {
}
companion object {
private const val ARG_POS = BuildConfig.APPLICATION_ID + ".key.POS"
/*
* Instantiates this fragment for use in a ViewPager.
*/
fun new(mode: DisplayMode): HomeListFragment {
fun new(pos: Int): HomeListFragment {
val fragment = HomeListFragment()
fragment.displayMode = mode
fragment.arguments = Bundle().apply {
putInt(ARG_POS, pos)
}
return fragment
}
}

View file

@ -26,6 +26,7 @@ import org.oxycblt.auxio.music.Artist
import org.oxycblt.auxio.music.Genre
import org.oxycblt.auxio.music.MusicStore
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.recycler.DisplayMode
class HomeViewModel : ViewModel() {
private val mGenres = MutableLiveData(listOf<Genre>())
@ -40,6 +41,9 @@ class HomeViewModel : ViewModel() {
private val mSongs = MutableLiveData(listOf<Song>())
val songs: LiveData<List<Song>> get() = mSongs
private val mTabs = MutableLiveData(arrayOf<DisplayMode>())
val tabs: LiveData<Array<DisplayMode>> = mTabs
private val musicStore = MusicStore.getInstance()
init {
@ -47,5 +51,9 @@ class HomeViewModel : ViewModel() {
mArtists.value = musicStore.artists
mAlbums.value = musicStore.albums
mSongs.value = musicStore.songs
mTabs.value = arrayOf(
DisplayMode.SHOW_SONGS, DisplayMode.SHOW_ALBUMS,
DisplayMode.SHOW_ARTISTS, DisplayMode.SHOW_GENRES
)
}
}