Store references to items in DetailViewModel
Store references to the artist & album inside DetailViewModel to prevent an artist/album search on every re-creation.
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
package org.oxycblt.auxio
|
||||
|
||||
// RecyclerView click listener
|
||||
class ClickListener<T>(val onClick: (T) -> Unit)
|
||||
@@ -23,6 +23,3 @@ class MainActivity : AppCompatActivity() {
|
||||
setContentView(R.layout.activity_main)
|
||||
}
|
||||
}
|
||||
|
||||
// RecyclerView click listener
|
||||
class ClickListener<T>(val onClick: (T) -> Unit)
|
||||
|
||||
@@ -28,17 +28,22 @@ class AlbumDetailFragment : Fragment() {
|
||||
): View? {
|
||||
val binding = FragmentAlbumDetailBinding.inflate(inflater)
|
||||
|
||||
// I honestly don't want to turn of the any data classes into parcelables due to how
|
||||
// many lists they store, so just pick up the artist id and find it from musicModel.
|
||||
val musicModel: MusicViewModel by activityViewModels()
|
||||
val album = musicModel.albums.value?.find { it.id == args.albumId }!!
|
||||
// If DetailViewModel isn't already storing the album, get it from MusicViewModel
|
||||
// using the ID given by the navigation arguments.
|
||||
if (detailModel.currentAlbum == null) {
|
||||
val musicModel: MusicViewModel by activityViewModels()
|
||||
|
||||
detailModel.currentAlbum = musicModel.albums.value!!.find {
|
||||
it.id == args.albumId
|
||||
}!!
|
||||
}
|
||||
|
||||
binding.lifecycleOwner = this
|
||||
binding.detailModel = detailModel
|
||||
binding.album = album
|
||||
binding.album = detailModel.currentAlbum
|
||||
|
||||
binding.songRecycler.adapter = DetailSongAdapter(
|
||||
album.songs,
|
||||
detailModel.currentAlbum!!.songs,
|
||||
ClickListener {
|
||||
Log.d(this::class.simpleName, it.name)
|
||||
}
|
||||
@@ -46,16 +51,17 @@ class AlbumDetailFragment : Fragment() {
|
||||
binding.songRecycler.applyDivider()
|
||||
binding.songRecycler.setHasFixedSize(true)
|
||||
|
||||
// If the album was shown directly from LibraryFragment, then enable the ability
|
||||
// to navigate to the artist from the album. Don't do this if the album was shown
|
||||
// from ArtistDetailFragment, as you can just navigate up to see the parent artist.
|
||||
if (args.isFromLibrary) {
|
||||
// If the album was shown directly from LibraryFragment [No parent artist stored],
|
||||
// then enable the ability to navigate upwards to the album's parent artist.
|
||||
if (detailModel.currentArtist == null) {
|
||||
detailModel.doneWithNavToParent()
|
||||
|
||||
detailModel.navToParentArtist.observe(viewLifecycleOwner) {
|
||||
if (it) {
|
||||
findNavController().navigate(
|
||||
AlbumDetailFragmentDirections.actionShowParentArtist(album.artist.id)
|
||||
AlbumDetailFragmentDirections.actionShowParentArtist(
|
||||
detailModel.currentAlbum!!.artist.id
|
||||
)
|
||||
)
|
||||
|
||||
detailModel.doneWithNavToParent()
|
||||
@@ -69,4 +75,4 @@ class AlbumDetailFragment : Fragment() {
|
||||
|
||||
return binding.root
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,20 +28,26 @@ class ArtistDetailFragment : Fragment() {
|
||||
): View? {
|
||||
val binding = FragmentArtistDetailBinding.inflate(inflater)
|
||||
|
||||
// I honestly don't want to turn of the any data classes into parcelables due to how
|
||||
// many lists they store, so just pick up the artist id and find it from musicModel.
|
||||
val musicModel: MusicViewModel by activityViewModels()
|
||||
val artist = musicModel.artists.value?.find { it.id == args.artistId }!!
|
||||
// If DetailViewModel isn't already storing the artist, get it from MusicViewModel
|
||||
// using the ID given by the navigation arguments
|
||||
if (detailModel.currentArtist == null) {
|
||||
val musicModel: MusicViewModel by activityViewModels()
|
||||
detailModel.currentArtist = musicModel.artists.value!!.find {
|
||||
it.id == args.artistId
|
||||
}!!
|
||||
}
|
||||
|
||||
binding.lifecycleOwner = this
|
||||
binding.artist = artist
|
||||
|
||||
binding.albumRecycler.adapter = DetailAlbumAdapter(
|
||||
artist.albums,
|
||||
val artistAdapter = DetailAlbumAdapter(
|
||||
detailModel.currentArtist!!.albums,
|
||||
ClickListener {
|
||||
navToAlbum(it)
|
||||
}
|
||||
)
|
||||
|
||||
binding.lifecycleOwner = this
|
||||
binding.artist = detailModel.currentArtist!!
|
||||
|
||||
binding.albumRecycler.adapter = artistAdapter
|
||||
binding.albumRecycler.applyDivider()
|
||||
binding.albumRecycler.setHasFixedSize(true)
|
||||
|
||||
@@ -56,14 +62,22 @@ class ArtistDetailFragment : Fragment() {
|
||||
detailModel.isAlreadyNavigating = false
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
|
||||
// Reset the stored artist so that the next instance of ArtistDetailFragment
|
||||
// will not read it.
|
||||
detailModel.currentArtist = null
|
||||
}
|
||||
|
||||
private fun navToAlbum(album: Album) {
|
||||
// Don't navigate if an item already has been selected.
|
||||
if (!detailModel.isAlreadyNavigating) {
|
||||
detailModel.isAlreadyNavigating = true
|
||||
|
||||
findNavController().navigate(
|
||||
ArtistDetailFragmentDirections.actionShowAlbum(album.id, false)
|
||||
ArtistDetailFragmentDirections.actionShowAlbum(album.id)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package org.oxycblt.auxio.detail
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.music.models.Artist
|
||||
|
||||
class DetailViewModel : ViewModel() {
|
||||
var isAlreadyNavigating = false
|
||||
@@ -10,6 +12,9 @@ class DetailViewModel : ViewModel() {
|
||||
private val mNavToParentArtist = MutableLiveData<Boolean>()
|
||||
val navToParentArtist: LiveData<Boolean> get() = mNavToParentArtist
|
||||
|
||||
var currentArtist: Artist? = null
|
||||
var currentAlbum: Album? = null
|
||||
|
||||
fun navToParent() {
|
||||
mNavToParentArtist.value = true
|
||||
}
|
||||
@@ -17,4 +22,4 @@ class DetailViewModel : ViewModel() {
|
||||
fun doneWithNavToParent() {
|
||||
mNavToParentArtist.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,8 +19,7 @@ data class Artist(
|
||||
}
|
||||
|
||||
fun finalizeGenre() {
|
||||
// If the artist has more than one genre, pick the most "prominent" one.
|
||||
// [Really just eliminate duplicates created from my hacky way of getting genres loaded but shhhh]
|
||||
// If the artist has more than one genre, pick the most "Prominent" one.
|
||||
genre = if (genres.size > 1) {
|
||||
val groupGenres = genres.groupBy { it.name }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user