settings: add option to clear state

Add an option to clear the currently saved playback state.

This does not clear playback entirely, but rather remove the saved
state so that it's not restored on the next startup. This is generally
the cleanest solution compared to allowing state restore to be toggled,
which opens up a bunch of race conditions.

Resolves #107.
This commit is contained in:
OxygenCobalt
2022-07-25 09:54:39 -06:00
parent 86a1815b1f
commit 6381815fd9
9 changed files with 36 additions and 10 deletions
@@ -31,6 +31,11 @@ import org.oxycblt.auxio.util.androidActivityViewModels
import org.oxycblt.auxio.util.collectImmediately
import org.oxycblt.auxio.util.formatDuration
/**
* A dialog displayed when "View properties" is selected on a song, showing more information about
* the properties of the audio file itself.
* @author OxygenCobalt
*/
class SongDetailDialog : ViewBindingDialogFragment<DialogSongDetailBinding>() {
private val detailModel: DetailViewModel by androidActivityViewModels()
private val args: SongDetailDialogArgs by navArgs()
@@ -282,6 +282,14 @@ class PlaybackViewModel(application: Application) :
}
}
/** Wipe the saved playback state (if any). */
fun wipePlaybackState(onDone: () -> Unit) {
viewModelScope.launch {
playbackManager.wipeState(PlaybackStateDatabase.getInstance(application))
onDone()
}
}
/**
* Force restore the last [PlaybackStateManager] saved state, regardless of if a library exists
* or not. [onDone] will be called with true if it was successfully done, or false if there was
@@ -186,19 +186,17 @@ class PlaybackStateDatabase private constructor(context: Context) :
}
/** Clear the previously written [SavedState] and write a new one. */
fun write(state: SavedState) {
fun write(state: SavedState?) {
requireBackgroundThread()
val song = state.queue.getOrNull(state.index)
if (song != null) {
if (state != null && state.index in state.queue.indices) {
val rawState =
RawState(
index = state.index,
positionMs = state.positionMs,
repeatMode = state.repeatMode,
isShuffled = state.isShuffled,
songId = song.id,
songId = state.queue[state.index].id,
parentId = state.parent?.id,
playbackMode =
when (state.parent) {
@@ -388,12 +388,15 @@ class PlaybackStateManager private constructor() {
/** Save the current state to the [database]. */
suspend fun saveState(database: PlaybackStateDatabase) {
logD("Saving state to DB")
val state = synchronized(this) { makeStateImpl() }
withContext(Dispatchers.IO) { database.write(state) }
}
suspend fun wipeState(database: PlaybackStateDatabase) {
logD("Wiping state")
withContext(Dispatchers.IO) { database.write(null) }
}
/** Sanitize the state with [newLibrary]. */
@Synchronized
fun sanitize(newLibrary: MusicStore.Library) {
@@ -49,8 +49,6 @@ import org.oxycblt.auxio.util.systemBarInsetsCompat
/**
* The actual fragment containing the settings menu. Inherits [PreferenceFragmentCompat].
* @author OxygenCobalt
*
* TODO: Add option to not restore state
*/
@Suppress("UNUSED")
class SettingsListFragment : PreferenceFragmentCompat() {
@@ -120,6 +118,9 @@ class SettingsListFragment : PreferenceFragmentCompat() {
getString(R.string.set_key_save_state) -> {
playbackModel.savePlaybackState { context?.showToast(R.string.lbl_state_saved) }
}
getString(R.string.set_key_wipe_state) -> {
playbackModel.wipePlaybackState { context?.showToast(R.string.lbl_state_wiped) }
}
getString(R.string.set_key_restore_state) ->
playbackModel.tryRestorePlaybackState { restored ->
if (restored) {