Modern Apps git

← Modern-Apps.git

music-wear: reflected playback ops leave the pending queue instead of replaying on reconnect

Commit
6f94882f11a18ea145c677f530244f53a5fca738
Author
Vayun Mathur <[email protected]>
When
2026-12-01 10:38 UTC (5 days ago)
Parents
410f7d1
The pending-op queue was append-only: setPlayback documented
'drop ops it reflects' but never touched pendingOps, and
WearLinkBridge resends the whole queue on every reconnect.
Trace: tap Pause (op queued + sent, persisted), phone pauses
and reflects isPlaying=false, watch shows paused โ€” yet the
op survives, even across process death. Any reconnect
(Bluetooth bounce, relaunch) resends it and toggles playback
back on: music resumes un-paused by itself; SKIP_NEXT skips
a second track, a stale SEEK drags position back, PLAY_TRACKS
restarts the queue, and settings showed 'Waiting for phone'
forever.

setPlayback now drops every non-library op when a reflected
playback state arrives; setPage drops a library op only on
the snapshot that proves it (playlist detail containing the
track, playlists list containing the created name) โ€” the
sibling wear modules' reflected-op pattern. Playback ops are
the priority because the phone's add-to-playlist is
idempotent but transport ops are not. Seven new tests pin
the drops and the negatives. Wire untouched; architecture
unchanged. Verified: :music:wear:testDebugUnitTest BUILD
SUCCESSFUL; lint warnings are pre-existing.

Diff

music-wear/src/main/java/com/vayunmathur/music/wear/data/WearMusicStore.kt
diff --git a/music-wear/src/main/java/com/vayunmathur/music/wear/data/WearMusicStore.kt b/music-wear/src/main/java/com/vayunmathur/music/wear/data/WearMusicStore.ktindex 0feb9bfaae..4bfc1ec582 100644--- a/music-wear/src/main/java/com/vayunmathur/music/wear/data/WearMusicStore.kt+++ b/music-wear/src/main/java/com/vayunmathur/music/wear/data/WearMusicStore.kt@@ -4,7 +4,9 @@ import android.content.Context import com.vayunmathur.library.log.Log import com.vayunmathur.music.wear.domain.WearMusicOp import com.vayunmathur.music.wear.domain.WearMusicProtocol+import com.vayunmathur.music.wear.domain.WearOpKind import com.vayunmathur.music.wear.domain.WearPageId+import com.vayunmathur.music.wear.domain.WearPageKey import com.vayunmathur.music.wear.domain.WearPageSnapshot import com.vayunmathur.music.wear.domain.WearPlaybackState import com.vayunmathur.music.wear.domain.WearWatchState@@ -74,6 +76,7 @@ class WearMusicStore private constructor(private val context: Context) {         }         _pages.value = current         persist(pagesFile, WearMusicProtocol.json.encodeToString(current.values.toList()))+        dropPending(pendingOpsAfterPage(_pendingOps.value, snapshot))         publishWatchState()     } @@ -81,6 +84,15 @@ class WearMusicStore private constructor(private val context: Context) {     fun setPlayback(state: WearPlaybackState) {         _playback.value = state         persist(playbackFile, WearMusicProtocol.json.encodeToString(state))+        dropPending(pendingOpsAfterPlayback(_pendingOps.value))+        publishWatchState()+    }++    /** Replace the pending queue with [kept] when it changed, persisting the smaller queue. */+    private fun dropPending(kept: List<WearMusicOp>) {+        if (kept == _pendingOps.value) return+        _pendingOps.value = kept+        persist(opsFile, WearMusicProtocol.json.encodeToString(kept))     }      /** Queue an op for the phone; persisted so an offline tap is not lost, resent on reconnect. */@@ -120,3 +132,39 @@ class WearMusicStore private constructor(private val context: Context) {         WearMusicProtocol.json.decodeFromString<List<WearMusicOp>>(opsFile.readText())     }.getOrDefault(emptyList()) }++/** Op kinds the phone reports back only through page snapshots, never through playback state. */+private val wearLibraryOpKinds = setOf(WearOpKind.ADD_TO_PLAYLIST, WearOpKind.CREATE_PLAYLIST)++/**+ * Pending ops left after a phone playback state arrives. Every non-library op is+ * executed by the phone player and reported back only as a new+ * [WearPlaybackState], so a state that arrives after the op was queued is its+ * reflection: keeping the op would resend it on the next reconnect (a second+ * TOGGLE_PLAY_PAUSE un-pauses, a second SKIP_NEXT skips another track, a stale+ * SEEK drags playback back). Library ops survive: playback says nothing about+ * them, and dropping them here could lose an op the phone has not applied yet.+ */+internal fun pendingOpsAfterPlayback(pending: List<WearMusicOp>): List<WearMusicOp> =+    pending.filter { it.kind in wearLibraryOpKinds }++/**+ * Pending ops left after a page snapshot arrives: a library op is reflected+ * only by the snapshot that proves it (the playlist detail now containing the+ * track, or the playlist list now containing the created name); every other op+ * is untouched.+ */+internal fun pendingOpsAfterPage(pending: List<WearMusicOp>, snapshot: WearPageSnapshot): List<WearMusicOp> =+    pending.filterNot { op ->+        when (op.kind) {+            WearOpKind.ADD_TO_PLAYLIST ->+                snapshot.page.key == WearPageKey.PLAYLIST_DETAIL &&+                    snapshot.page.entityId == op.entityId &&+                    snapshot.tracks.map { it.id }.containsAll(op.trackIds)+            WearOpKind.CREATE_PLAYLIST ->+                snapshot.page.key == WearPageKey.PLAYLISTS &&+                    op.text.isNotBlank() &&+                    snapshot.playlists.any { it.name == op.text }+            else -> false+        }+    }
music-wear/src/test/java/com/vayunmathur/music/wear/data/WearPendingOpsTest.kt
diff --git a/music-wear/src/test/java/com/vayunmathur/music/wear/data/WearPendingOpsTest.kt b/music-wear/src/test/java/com/vayunmathur/music/wear/data/WearPendingOpsTest.ktnew file mode 100644index 0000000000..e53d226dbd--- /dev/null+++ b/music-wear/src/test/java/com/vayunmathur/music/wear/data/WearPendingOpsTest.kt@@ -0,0 +1,97 @@+package com.vayunmathur.music.wear.data++import com.vayunmathur.music.wear.domain.WearMusicOp+import com.vayunmathur.music.wear.domain.WearOpKind+import com.vayunmathur.music.wear.domain.WearPageId+import com.vayunmathur.music.wear.domain.WearPageKey+import com.vayunmathur.music.wear.domain.WearPageSnapshot+import com.vayunmathur.music.wear.domain.WearPlaylist+import com.vayunmathur.music.wear.domain.WearTrack+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Regression: pending ops were never dropped, so [WearLinkBridge.flushPending]+ * resent every op ever queued on each reconnect โ€” a reflected+ * TOGGLE_PLAY_PAUSE un-paused, SKIP_NEXT skipped again, SEEK jumped back.+ */+class WearPendingOpsTest {++    private val toggle = WearMusicOp(WearOpKind.TOGGLE_PLAY_PAUSE)+    private val skip = WearMusicOp(WearOpKind.SKIP_NEXT)+    private val seek = WearMusicOp(WearOpKind.SEEK, positionMs = 30_000L)+    private val play = WearMusicOp(WearOpKind.PLAY_TRACKS, trackIds = listOf(1L, 2L), startIndex = 1)+    private val addToPlaylist =+        WearMusicOp(WearOpKind.ADD_TO_PLAYLIST, trackIds = listOf(7L), entityId = 42L)+    private val createPlaylist = WearMusicOp(WearOpKind.CREATE_PLAYLIST, text = "Road")++    @Test+    fun playbackReflectionDropsTransportOps() {+        assertEquals(+            emptyList(),+            pendingOpsAfterPlayback(listOf(toggle, skip, seek, play)),+        )+    }++    @Test+    fun playbackReflectionKeepsLibraryOpsInOrder() {+        assertEquals(+            listOf(addToPlaylist, createPlaylist),+            pendingOpsAfterPlayback(listOf(toggle, addToPlaylist, skip, createPlaylist)),+        )+    }++    @Test+    fun playbackReflectionOnEmptyStaysEmpty() {+        assertEquals(emptyList(), pendingOpsAfterPlayback(emptyList()))+    }++    @Test+    fun playlistDetailContainingTrackDropsAddToPlaylist() {+        val snapshot = WearPageSnapshot(+            page = WearPageId(WearPageKey.PLAYLIST_DETAIL, entityId = 42L),+            tracks = listOf(WearTrack(5L, "Other"), WearTrack(7L, "Added")),+        )+        assertEquals(emptyList(), pendingOpsAfterPage(listOf(addToPlaylist), snapshot))+    }++    @Test+    fun playlistDetailMissingTrackOrWrongPlaylistKeepsAddToPlaylist() {+        val missing = WearPageSnapshot(+            page = WearPageId(WearPageKey.PLAYLIST_DETAIL, entityId = 42L),+            tracks = listOf(WearTrack(5L, "Other")),+        )+        val wrongPlaylist = WearPageSnapshot(+            page = WearPageId(WearPageKey.PLAYLIST_DETAIL, entityId = 9L),+            tracks = listOf(WearTrack(7L, "Added")),+        )+        assertEquals(listOf(addToPlaylist), pendingOpsAfterPage(listOf(addToPlaylist), missing))+        assertEquals(listOf(addToPlaylist), pendingOpsAfterPage(listOf(addToPlaylist), wrongPlaylist))+    }++    @Test+    fun playlistListContainingNameDropsCreatePlaylistOnly() {+        val snapshot = WearPageSnapshot(+            page = WearPageId(WearPageKey.PLAYLISTS),+            playlists = listOf(WearPlaylist(3L, "Road")),+        )+        assertEquals(+            listOf(addToPlaylist),+            pendingOpsAfterPage(listOf(createPlaylist, addToPlaylist), snapshot),+        )+        val other = snapshot.copy(playlists = listOf(WearPlaylist(3L, "Sea")))+        assertEquals(listOf(createPlaylist), pendingOpsAfterPage(listOf(createPlaylist), other))+    }++    @Test+    fun unrelatedPageDropsNothing() {+        val snapshot = WearPageSnapshot(+            page = WearPageId(WearPageKey.HOME),+            tracks = listOf(WearTrack(7L, "Added")),+        )+        assertEquals(+            listOf(addToPlaylist, createPlaylist, toggle),+            pendingOpsAfterPage(listOf(addToPlaylist, createPlaylist, toggle), snapshot),+        )+    }+}