From 366945499e138ffc6f47502689d1e0e9ae88d7fc Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 12 Nov 2026 18:20:54 -0500 Subject: [PATCH] musikr: reconcile tracked playlists against discovered files --- .../musikr/playlist/PlaylistReconciler.kt | 79 +++++++++++++++++++ .../musikr/playlist/PlaylistReconcilerTest.kt | 40 ++++++++++ 2 files changed, 119 insertions(+) create mode 100644 musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistReconciler.kt create mode 100644 musikr/src/test/java/org/oxycblt/musikr/playlist/PlaylistReconcilerTest.kt diff --git a/musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistReconciler.kt b/musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistReconciler.kt new file mode 100644 index 000000000..148ad214a --- /dev/null +++ b/musikr/src/main/java/org/oxycblt/musikr/playlist/PlaylistReconciler.kt @@ -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 . + */ + +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, discovered: List): List { + 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) + } + } + } +} diff --git a/musikr/src/test/java/org/oxycblt/musikr/playlist/PlaylistReconcilerTest.kt b/musikr/src/test/java/org/oxycblt/musikr/playlist/PlaylistReconcilerTest.kt new file mode 100644 index 000000000..0d5aa5cd1 --- /dev/null +++ b/musikr/src/test/java/org/oxycblt/musikr/playlist/PlaylistReconcilerTest.kt @@ -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 . + */ + +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) + } +}