Fix parent restore bug

Fix a bug where PlaybackStateManager would try to restore the parent from a full list of items, which raises the possibility for the incorrect parent to be chosen.
This commit is contained in:
OxygenCobalt 2021-03-09 09:04:48 -07:00
parent 5d72bfa09b
commit 72877f77ee
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
3 changed files with 29 additions and 21 deletions

View file

@ -27,15 +27,6 @@ class MusicStore private constructor() {
private var mSongs = listOf<Song>()
val songs: List<Song> get() = mSongs
/** All parent models (ex Albums, Artists) loaded by Auxio */
val parents: List<Parent> by lazy {
mutableListOf<Parent>().apply {
addAll(mGenres)
addAll(mArtists)
addAll(mAlbums)
}
}
/** Marker for whether the music loading process has successfully completed. */
var loaded = false
private set
@ -88,12 +79,13 @@ class MusicStore private constructor() {
* @return The corresponding [Song] for this [uri], null if there isnt one.
*/
fun getSongForUri(uri: Uri, resolver: ContentResolver): Song? {
resolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use { cursor ->
cursor.moveToFirst()
val fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
resolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)
?.use { cursor ->
cursor.moveToFirst()
val fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
return songs.find { it.fileName == fileName }
}
return songs.find { it.fileName == fileName }
}
return null
}

View file

@ -667,14 +667,18 @@ class PlaybackStateManager private constructor() {
* Unpack a [playbackState] into this instance.
*/
private fun unpackFromPlaybackState(playbackState: PlaybackState) {
// Turn the simplified information from PlaybackState into values that can be used
mSong = musicStore.songs.find { it.name == playbackState.songName }
mParent = musicStore.parents.find { it.name == playbackState.parentName }
// Turn the simplified information from PlaybackState into usable data.
// Do queue setup first
mMode = PlaybackMode.fromInt(playbackState.mode) ?: PlaybackMode.ALL_SONGS
mParent = findParent(playbackState.parentName, mMode)
mIndex = playbackState.index
// Then set up the current state
mSong = musicStore.songs.find { it.name == playbackState.songName }
mLoopMode = LoopMode.fromInt(playbackState.loopMode) ?: LoopMode.NONE
mIsShuffling = playbackState.isShuffling
mIsInUserQueue = playbackState.inUserQueue
mIndex = playbackState.index
seekTo(playbackState.position)
}
@ -734,6 +738,19 @@ class PlaybackStateManager private constructor() {
forceUserQueueUpdate()
}
/**
* Get a [Parent] from music store given a [name] and playback [mode].
*/
private fun findParent(name: String, mode: PlaybackMode): Parent? {
return when (mode) {
PlaybackMode.IN_GENRE -> musicStore.genres.find { it.name == name }
PlaybackMode.IN_ARTIST -> musicStore.artists.find { it.name == name }
PlaybackMode.IN_ALBUM -> musicStore.albums.find { it.name == name }
else -> null
}
}
/**
* Do the sanity check to make sure the parent was not lost in the restore process.
*/

View file

@ -2,7 +2,6 @@ package org.oxycblt.auxio.settings
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import org.oxycblt.auxio.logD
import org.oxycblt.auxio.playback.state.PlaybackMode
@ -71,7 +70,7 @@ class SettingsManager private constructor(context: Context) :
get() = sharedPrefs.getBoolean(Keys.KEY_USE_ALT_NOTIFICATION_ACTION, false)
/** What to display on the library. */
val libraryDisplayMode: DisplayMode
val libraryDisplayMode: org.oxycblt.auxio.recycler.DisplayMode
get() = DisplayMode.valueOfOrFallback(
sharedPrefs.getString(
Keys.KEY_LIBRARY_DISPLAY_MODE,
@ -264,7 +263,7 @@ class SettingsManager private constructor(context: Context) :
interface Callback {
fun onColorizeNotifUpdate(doColorize: Boolean) {}
fun onNotifActionUpdate(useAltAction: Boolean) {}
fun onLibDisplayModeUpdate(displayMode: DisplayMode) {}
fun onLibDisplayModeUpdate(displayMode: org.oxycblt.auxio.recycler.DisplayMode) {}
fun onShowCoverUpdate(showCovers: Boolean) {}
fun onQualityCoverUpdate(doQualityCovers: Boolean) {}
}