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>() private var mSongs = listOf<Song>()
val songs: List<Song> get() = mSongs 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. */ /** Marker for whether the music loading process has successfully completed. */
var loaded = false var loaded = false
private set private set
@ -88,12 +79,13 @@ class MusicStore private constructor() {
* @return The corresponding [Song] for this [uri], null if there isnt one. * @return The corresponding [Song] for this [uri], null if there isnt one.
*/ */
fun getSongForUri(uri: Uri, resolver: ContentResolver): Song? { fun getSongForUri(uri: Uri, resolver: ContentResolver): Song? {
resolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use { cursor -> resolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)
cursor.moveToFirst() ?.use { cursor ->
val fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)) 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 return null
} }

View file

@ -667,14 +667,18 @@ class PlaybackStateManager private constructor() {
* Unpack a [playbackState] into this instance. * Unpack a [playbackState] into this instance.
*/ */
private fun unpackFromPlaybackState(playbackState: PlaybackState) { private fun unpackFromPlaybackState(playbackState: PlaybackState) {
// Turn the simplified information from PlaybackState into values that can be used // Turn the simplified information from PlaybackState into usable data.
mSong = musicStore.songs.find { it.name == playbackState.songName }
mParent = musicStore.parents.find { it.name == playbackState.parentName } // Do queue setup first
mMode = PlaybackMode.fromInt(playbackState.mode) ?: PlaybackMode.ALL_SONGS 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 mLoopMode = LoopMode.fromInt(playbackState.loopMode) ?: LoopMode.NONE
mIsShuffling = playbackState.isShuffling mIsShuffling = playbackState.isShuffling
mIsInUserQueue = playbackState.inUserQueue mIsInUserQueue = playbackState.inUserQueue
mIndex = playbackState.index
seekTo(playbackState.position) seekTo(playbackState.position)
} }
@ -734,6 +738,19 @@ class PlaybackStateManager private constructor() {
forceUserQueueUpdate() 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. * 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.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import org.oxycblt.auxio.logD import org.oxycblt.auxio.logD
import org.oxycblt.auxio.playback.state.PlaybackMode 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) get() = sharedPrefs.getBoolean(Keys.KEY_USE_ALT_NOTIFICATION_ACTION, false)
/** What to display on the library. */ /** What to display on the library. */
val libraryDisplayMode: DisplayMode val libraryDisplayMode: org.oxycblt.auxio.recycler.DisplayMode
get() = DisplayMode.valueOfOrFallback( get() = DisplayMode.valueOfOrFallback(
sharedPrefs.getString( sharedPrefs.getString(
Keys.KEY_LIBRARY_DISPLAY_MODE, Keys.KEY_LIBRARY_DISPLAY_MODE,
@ -264,7 +263,7 @@ class SettingsManager private constructor(context: Context) :
interface Callback { interface Callback {
fun onColorizeNotifUpdate(doColorize: Boolean) {} fun onColorizeNotifUpdate(doColorize: Boolean) {}
fun onNotifActionUpdate(useAltAction: Boolean) {} fun onNotifActionUpdate(useAltAction: Boolean) {}
fun onLibDisplayModeUpdate(displayMode: DisplayMode) {} fun onLibDisplayModeUpdate(displayMode: org.oxycblt.auxio.recycler.DisplayMode) {}
fun onShowCoverUpdate(showCovers: Boolean) {} fun onShowCoverUpdate(showCovers: Boolean) {}
fun onQualityCoverUpdate(doQualityCovers: Boolean) {} fun onQualityCoverUpdate(doQualityCovers: Boolean) {}
} }