Fix empty genre bug

Bandaid MusicSorter to remove any genres that dont have anything in them, because that happens now. Also prevent a crash just in case an empty music model does get played.
This commit is contained in:
OxygenCobalt 2020-10-18 15:37:00 -06:00
parent c4c115274b
commit 3337747cb7
5 changed files with 35 additions and 9 deletions

View file

@ -21,7 +21,7 @@ class LibraryViewModel : ViewModel() {
val searchHasFocus: Boolean get() = mSearchHasFocus
// TODO: Move these to prefs when they're added
private val mShowMode = MutableLiveData(ShowMode.SHOW_GENRES)
private val mShowMode = MutableLiveData(ShowMode.SHOW_ARTISTS)
val showMode: LiveData<ShowMode> get() = mShowMode
private val mSortMode = MutableLiveData(SortMode.ALPHA_DOWN)

View file

@ -3,7 +3,7 @@ package org.oxycblt.auxio.music
import android.net.Uri
// --- MUSIC MODELS ---
// TODO: Remove parent/child references so that they can be parcelable [Would require genre rework]
// FIXME: Remove parent/child references so that they can be parcelable?
// The base model for all music
// This is used in a lot of general functions in order to have generic utilities
@ -94,6 +94,13 @@ data class Genre(
}
num
}
val songs: MutableList<Song> by lazy {
val songs = mutableListOf<Song>()
artists.forEach {
songs.addAll(it.songs)
}
songs
}
}
// Header [Used for search, nothing else]

View file

@ -129,8 +129,6 @@ class MusicLoader(
name = artistPlaceholder
}
Log.d(this::class.simpleName, id.toString())
artists.add(
Artist(
id, name
@ -141,6 +139,10 @@ class MusicLoader(
cursor.close()
}
artists = artists.distinctBy {
it.name to it.genres
}.toMutableList()
// Then try to associate any genres with their respective artists.
for (genre in genres) {
val artistGenreCursor = resolver.query(
@ -166,10 +168,6 @@ class MusicLoader(
}
}
artists = artists.distinctBy {
it.name to it.genres
}.toMutableList()
Log.d(
this::class.simpleName,
"Artist search finished with ${artists.size} artists found."

View file

@ -26,6 +26,9 @@ class MusicSorter(
genres = genres.distinctBy {
it.name
}.toMutableList()
// Also elimate any genres that dont have artists, which also happens sometimes.
genres.removeAll { it.artists.isEmpty() }
}
private fun sortSongsIntoAlbums() {

View file

@ -18,7 +18,7 @@ import kotlin.random.Random.Default.nextLong
// TODO: Queue
// TODO: Add the playback service itself
// TODO: Add loop control [From playback]
// TODO: Implement persistence through Bundles and sanity checks [I want to keep my shuffles, okay?]
// TODO: Implement persistence through Bundles [I want to keep my shuffles, okay?]
// A ViewModel that acts as an intermediary between PlaybackService and the Playback Fragments.
class PlaybackViewModel : ViewModel() {
private val mCurrentSong = MutableLiveData<Song>()
@ -108,6 +108,12 @@ class PlaybackViewModel : ViewModel() {
fun play(album: Album, isShuffled: Boolean) {
Log.d(this::class.simpleName, "Playing album ${album.name}")
if (album.songs.isEmpty()) {
Log.e(this::class.simpleName, "Album is empty, not playing.")
return
}
val songs = orderSongsInAlbum(album)
updatePlayback(songs[0])
@ -128,6 +134,12 @@ class PlaybackViewModel : ViewModel() {
fun play(artist: Artist, isShuffled: Boolean) {
Log.d(this::class.simpleName, "Playing artist ${artist.name}")
if (artist.songs.isEmpty()) {
Log.e(this::class.simpleName, "Artist is empty, not playing.")
return
}
val songs = orderSongsInArtist(artist)
updatePlayback(songs[0])
@ -148,6 +160,12 @@ class PlaybackViewModel : ViewModel() {
fun play(genre: Genre, isShuffled: Boolean) {
Log.d(this::class.simpleName, "Playing genre ${genre.name}")
if (genre.songs.isEmpty()) {
Log.e(this::class.simpleName, "Genre is empty, not playing.")
return
}
val songs = orderSongsInGenre(genre)
updatePlayback(songs[0])