musikr: reconcile tracked playlists against discovered files

This commit is contained in:
jay
2026-11-12 18:20:54 -05:00
parent 843bf55fd1
commit 366945499e
2 changed files with 119 additions and 0 deletions
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2026 Auxio Project
* PlaylistReconciler.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.musikr.playlist
import org.oxycblt.musikr.Music
/**
* Reconciles stored tracked playlists against the playlist files discovered in a scan. The file
* on disk is authoritative: an unchanged file keeps the stored playlist (in-app edits survive), a
* changed file replaces it (in-app edits are discarded), and a deleted file deletes it. Playlists
* without a source file are unmanaged and never reach this unit.
*
* @author OxygenCobalt (Fork)
*/
internal object PlaylistReconciler {
/**
* A stored playlist's tracking identity.
*
* @property uid The playlist's UID.
* @property sourceUri The document URI of the file the playlist was imported from.
* @property signature The content signature of that file at import time.
*/
data class Tracked(val uid: Music.UID, val sourceUri: String, val signature: Long)
/**
* A playlist file discovered in a scan.
*
* @property uri The document URI of the file.
* @property signature The content signature of the file.
*/
data class Discovered(val uri: String, val signature: Long)
/** What to do with a tracked playlist or discovered file after reconciliation. */
sealed interface Action {
/** The file is unchanged: keep the stored playlist as-is. */
data class Keep(val tracked: Tracked) : Action
/** The file changed: re-import it, replacing the stored playlist's songs. */
data class Replace(val tracked: Tracked, val file: Discovered) : Action
/** The file is gone: delete the stored playlist. */
data class Delete(val tracked: Tracked) : Action
/** The file has no stored playlist: import it as a new tracked playlist. */
data class Create(val file: Discovered) : Action
}
/**
* Reconcile [tracked] stored playlists against [discovered] files, emitting one [Action] per
* input.
*/
fun reconcile(tracked: List<Tracked>, discovered: List<Discovered>): List<Action> {
val filesByUri = discovered.associateBy { it.uri }
return tracked.map { item ->
val file = filesByUri[item.sourceUri]
when {
file == null -> Action.Delete(item)
file.signature == item.signature -> Action.Keep(item)
else -> Action.Replace(item, file)
}
}
}
}
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2026 Auxio Project
* PlaylistReconcilerTest.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.musikr.playlist
import org.junit.Assert.assertEquals
import org.junit.Test
import org.oxycblt.musikr.Music
class PlaylistReconcilerTest {
@Test
fun reconcile_unchangedFile_keepsTheStoredPlaylist() {
val tracked =
PlaylistReconciler.Tracked(
Music.UID.auxio(Music.UID.Item.PLAYLIST),
"content://tree/playlists/x.m3u",
42L,
)
val discovered = PlaylistReconciler.Discovered("content://tree/playlists/x.m3u", 42L)
val actions = PlaylistReconciler.reconcile(listOf(tracked), listOf(discovered))
assertEquals(listOf(PlaylistReconciler.Action.Keep(tracked)), actions)
}
}