all: make callbacks initialize impls

Make addCallback calls (or similar) initalize the caller by calling the
specific callback methods.

This is primarily intended as a fix to an issue where the music library
would actually reload during a warm start. This is extremely bad, but I
hope it doesn't make the app too unusable until the next version.
This commit is contained in:
OxygenCobalt 2022-05-28 19:57:26 -06:00
parent 47726c3e02
commit 48289ef006
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
6 changed files with 11 additions and 27 deletions

View file

@ -54,6 +54,7 @@ class MusicStore private constructor() {
private val callbacks = mutableListOf<Callback>() private val callbacks = mutableListOf<Callback>()
fun addCallback(callback: Callback) { fun addCallback(callback: Callback) {
response?.let(callback::onMusicUpdate)
callbacks.add(callback) callbacks.add(callback)
} }

View file

@ -43,7 +43,7 @@ class MusicViewModel : ViewModel(), MusicStore.Callback {
* navigated to and because SnackBars will have the best UX here. * navigated to and because SnackBars will have the best UX here.
*/ */
fun loadMusic(context: Context) { fun loadMusic(context: Context) {
if (musicStore.library != null || isBusy) { if (_loaderResponse.value != null || isBusy) {
logD("Loader is busy/already completed, not reloading") logD("Loader is busy/already completed, not reloading")
return return
} }

View file

@ -83,18 +83,6 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback, MusicStore
init { init {
musicStore.addCallback(this) musicStore.addCallback(this)
playbackManager.addCallback(this) playbackManager.addCallback(this)
// If the PlaybackViewModel was cleared [Signified by PlaybackStateManager still being
// around & the fact that we are in the init function], then attempt to restore the
// ViewModel state. If it isn't, then wait for MainFragment to give the command to restore
// PlaybackStateManager.
if (playbackManager.isInitialized) {
onNewPlayback(playbackManager.index, playbackManager.queue, playbackManager.parent)
onPositionChanged(playbackManager.positionMs)
onPlayingChanged(playbackManager.isPlaying)
onShuffledChanged(playbackManager.isShuffled)
onRepeatChanged(playbackManager.repeatMode)
}
} }
// --- PLAYING FUNCTIONS --- // --- PLAYING FUNCTIONS ---

View file

@ -96,6 +96,15 @@ class PlaybackStateManager private constructor() {
* [removeCallback] when done. * [removeCallback] when done.
*/ */
fun addCallback(callback: Callback) { fun addCallback(callback: Callback) {
if (isInitialized) {
callback.onNewPlayback(index, queue, parent)
callback.onSeek(positionMs)
callback.onPositionChanged(positionMs)
callback.onRepeatChanged(repeatMode)
callback.onShuffledChanged(isShuffled)
callback.onPlayingChanged(isPlaying)
}
callbacks.add(callback) callbacks.add(callback)
} }

View file

@ -55,13 +55,6 @@ class MediaSessionComponent(private val context: Context, private val player: Pl
playbackManager.addCallback(this) playbackManager.addCallback(this)
settingsManager.addCallback(this) settingsManager.addCallback(this)
mediaSession.setCallback(this) mediaSession.setCallback(this)
if (playbackManager.isInitialized) {
updateMediaMetadata(playbackManager.song)
invalidateSessionState()
onRepeatChanged(playbackManager.repeatMode)
onShuffledChanged(playbackManager.isShuffled)
}
} }
fun release() { fun release() {

View file

@ -139,13 +139,6 @@ class PlaybackService :
// --- PLAYBACKSTATEMANAGER SETUP --- // --- PLAYBACKSTATEMANAGER SETUP ---
playbackManager.addCallback(this) playbackManager.addCallback(this)
if (playbackManager.isInitialized) {
loadSong(playbackManager.song)
onSeek(playbackManager.positionMs)
onPlayingChanged(playbackManager.isPlaying)
onShuffledChanged(playbackManager.isShuffled)
onRepeatChanged(playbackManager.repeatMode)
}
// --- SETTINGSMANAGER SETUP --- // --- SETTINGSMANAGER SETUP ---