Move LiveData away from MusicRepository
Change the LiveData in MusicRepository to just be a plain list, as MusicRepository isnt Lifecycle-Aware.
This commit is contained in:
parent
c04a90c3fa
commit
acef9550fa
6 changed files with 46 additions and 48 deletions
|
@ -12,7 +12,6 @@ import androidx.lifecycle.ViewModelProvider
|
|||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||
import org.oxycblt.auxio.library.recycler.AlbumDataAdapter
|
||||
import org.oxycblt.auxio.music.MusicRepository
|
||||
|
||||
class LibraryFragment : Fragment() {
|
||||
|
||||
|
@ -32,8 +31,7 @@ class LibraryFragment : Fragment() {
|
|||
val adapter = AlbumDataAdapter()
|
||||
binding.libraryRecycler.adapter = adapter
|
||||
|
||||
val repo = MusicRepository.getInstance()
|
||||
repo.albums.observe(
|
||||
libraryModel.albums.observe(
|
||||
viewLifecycleOwner,
|
||||
Observer {
|
||||
adapter.data = it
|
||||
|
|
|
@ -1,11 +1,27 @@
|
|||
package org.oxycblt.auxio.library
|
||||
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import org.oxycblt.auxio.music.MusicRepository
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.music.models.Artist
|
||||
|
||||
class LibraryViewModel() : ViewModel() {
|
||||
|
||||
private val mArtists = MutableLiveData<List<Artist>>()
|
||||
private var mAlbums = MutableLiveData<List<Album>>()
|
||||
|
||||
val artists: LiveData<List<Artist>> get() = mArtists
|
||||
val albums: LiveData<List<Album>> get() = mAlbums
|
||||
|
||||
init {
|
||||
val repo = MusicRepository.getInstance()
|
||||
|
||||
mArtists.value = repo.artists
|
||||
mAlbums.value = repo.albums
|
||||
|
||||
Log.d(this::class.simpleName, "ViewModel created.")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,10 +4,10 @@ import android.view.LayoutInflater
|
|||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import org.oxycblt.auxio.databinding.LibraryItemBinding
|
||||
import org.oxycblt.auxio.databinding.AlbumItemBinding
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
|
||||
class AlbumDataAdapter : ListAdapter<Album, LibraryViewHolder>(DiffCallback) {
|
||||
class AlbumDataAdapter : ListAdapter<Album, AlbumViewHolder>(DiffCallback) {
|
||||
|
||||
var data = listOf<Album>()
|
||||
set(newData) {
|
||||
|
@ -15,16 +15,16 @@ class AlbumDataAdapter : ListAdapter<Album, LibraryViewHolder>(DiffCallback) {
|
|||
submitList(data)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LibraryViewHolder {
|
||||
return LibraryViewHolder(
|
||||
LibraryItemBinding.inflate(LayoutInflater.from(parent.context))
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlbumViewHolder {
|
||||
return AlbumViewHolder(
|
||||
AlbumItemBinding.inflate(LayoutInflater.from(parent.context))
|
||||
)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: LibraryViewHolder, position: Int) {
|
||||
override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
|
||||
val album = getItem(position)
|
||||
|
||||
holder.bindAlbum(album)
|
||||
holder.bind(album)
|
||||
}
|
||||
|
||||
companion object DiffCallback : DiffUtil.ItemCallback<Album>() {
|
||||
|
|
|
@ -1,21 +1,16 @@
|
|||
package org.oxycblt.auxio.library.recycler
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.LibraryItemBinding
|
||||
import org.oxycblt.auxio.databinding.AlbumItemBinding
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.music.models.Artist
|
||||
|
||||
class LibraryViewHolder(
|
||||
private var binding: LibraryItemBinding
|
||||
class AlbumViewHolder(
|
||||
private var binding: AlbumItemBinding
|
||||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
// Bind the view w/new data
|
||||
fun bindAlbum(album: Album) {
|
||||
fun bind(album: Album) {
|
||||
binding.album = album
|
||||
binding.executePendingBindings()
|
||||
}
|
||||
|
||||
fun bindArtist(artist: Artist) {
|
||||
// TODO: Not implemented.
|
||||
}
|
||||
}
|
|
@ -2,10 +2,6 @@ package org.oxycblt.auxio.music
|
|||
|
||||
import android.app.Application
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.music.models.Artist
|
||||
import org.oxycblt.auxio.music.models.Song
|
||||
|
@ -16,15 +12,11 @@ import org.oxycblt.auxio.music.processing.MusicSorter
|
|||
// Storage for music data.
|
||||
class MusicRepository {
|
||||
|
||||
private val mArtists = MutableLiveData<List<Artist>>()
|
||||
private var mAlbums = MutableLiveData<List<Album>>()
|
||||
private var mSongs = MutableLiveData<List<Song>>()
|
||||
lateinit var artists: List<Artist>
|
||||
lateinit var albums: List<Album>
|
||||
lateinit var songs: List<Song>
|
||||
|
||||
val artists: LiveData<List<Artist>> get() = mArtists
|
||||
val albums: LiveData<List<Album>> get() = mAlbums
|
||||
val songs: LiveData<List<Song>> get() = mSongs
|
||||
|
||||
suspend fun init(app: Application): MusicLoaderResponse {
|
||||
fun init(app: Application): MusicLoaderResponse {
|
||||
Log.i(this::class.simpleName, "Starting initial music load...")
|
||||
|
||||
val start = System.currentTimeMillis()
|
||||
|
@ -32,26 +24,23 @@ class MusicRepository {
|
|||
val loader = MusicLoader(app)
|
||||
|
||||
if (loader.response == MusicLoaderResponse.DONE) {
|
||||
// If the loading succeeds, then process the songs and set them
|
||||
// as the values on the main thread.
|
||||
withContext(Dispatchers.Main) {
|
||||
val sorter = MusicSorter(
|
||||
loader.artists,
|
||||
loader.albums,
|
||||
loader.songs
|
||||
)
|
||||
// If the loading succeeds, then process the songs and set them.
|
||||
val sorter = MusicSorter(
|
||||
loader.artists,
|
||||
loader.albums,
|
||||
loader.songs
|
||||
)
|
||||
|
||||
mSongs.value = sorter.songs
|
||||
mAlbums.value = sorter.albums
|
||||
mArtists.value = sorter.artists
|
||||
songs = sorter.songs.toList()
|
||||
albums = sorter.albums.toList()
|
||||
artists = sorter.artists.toList()
|
||||
|
||||
val elapsed = System.currentTimeMillis() - start
|
||||
val elapsed = System.currentTimeMillis() - start
|
||||
|
||||
Log.i(
|
||||
this::class.simpleName,
|
||||
"Music load completed successfully in ${elapsed}ms."
|
||||
)
|
||||
}
|
||||
Log.i(
|
||||
this::class.simpleName,
|
||||
"Music load completed successfully in ${elapsed}ms."
|
||||
)
|
||||
}
|
||||
|
||||
return loader.response
|
||||
|
|
Loading…
Reference in a new issue