musikr: never reconcile destructively
This commit is contained in:
@@ -22,9 +22,10 @@ import org.oxycblt.musikr.Music
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Reconciles stored tracked playlists against the playlist files discovered in a scan. The file
|
* 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
|
* on disk wins when it speaks, but never destructively: an unchanged file keeps the stored
|
||||||
* changed file replaces it (in-app edits are discarded), and a deleted file deletes it. Playlists
|
* playlist (in-app edits survive), a changed file with at least one resolved entry replaces it
|
||||||
* without a source file are unmanaged and never reach this unit.
|
* (in-app edits are discarded), and a file that is missing or resolves nothing keeps the stored
|
||||||
|
* playlist. Playlists without a source file are unmanaged and never reach this unit.
|
||||||
*
|
*
|
||||||
* @author OxygenCobalt (Fork)
|
* @author OxygenCobalt (Fork)
|
||||||
*/
|
*/
|
||||||
@@ -43,21 +44,19 @@ internal object PlaylistReconciler {
|
|||||||
*
|
*
|
||||||
* @property uri The document URI of the file.
|
* @property uri The document URI of the file.
|
||||||
* @property signature The content signature of the file.
|
* @property signature The content signature of the file.
|
||||||
|
* @property resolvedCount How many entries resolved against the library.
|
||||||
*/
|
*/
|
||||||
data class Discovered(val uri: String, val signature: Long)
|
data class Discovered(val uri: String, val signature: Long, val resolvedCount: Int)
|
||||||
|
|
||||||
/** What to do with a tracked playlist or discovered file after reconciliation. */
|
/** What to do with a tracked playlist or discovered file after reconciliation. */
|
||||||
sealed interface Action {
|
sealed interface Action {
|
||||||
/** The file is unchanged: keep the stored playlist as-is. */
|
/** Nothing changes: keep the stored playlist as-is. */
|
||||||
data class Keep(val tracked: Tracked) : Action
|
data class Keep(val tracked: Tracked) : Action
|
||||||
|
|
||||||
/** The file changed: re-import it, replacing the stored playlist's songs. */
|
/** The file changed and resolves: re-import it, replacing the stored playlist's songs. */
|
||||||
data class Replace(val tracked: Tracked, val file: Discovered) : Action
|
data class Replace(val tracked: Tracked, val file: Discovered) : Action
|
||||||
|
|
||||||
/** The file is gone: delete the stored playlist. */
|
/** The file has no stored playlist and resolves: import it as a new tracked 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
|
data class Create(val file: Discovered) : Action
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,14 +70,17 @@ internal object PlaylistReconciler {
|
|||||||
tracked.map { item ->
|
tracked.map { item ->
|
||||||
val file = filesByUri[item.sourceUri]
|
val file = filesByUri[item.sourceUri]
|
||||||
when {
|
when {
|
||||||
file == null -> Action.Delete(item)
|
file == null -> Action.Keep(item)
|
||||||
file.signature == item.signature -> Action.Keep(item)
|
file.signature == item.signature -> Action.Keep(item)
|
||||||
|
file.resolvedCount == 0 -> Action.Keep(item)
|
||||||
else -> Action.Replace(item, file)
|
else -> Action.Replace(item, file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val trackedUris = tracked.mapTo(mutableSetOf()) { it.sourceUri }
|
val trackedUris = tracked.mapTo(mutableSetOf()) { it.sourceUri }
|
||||||
val createActions =
|
val createActions =
|
||||||
discovered.filter { it.uri !in trackedUris }.map { Action.Create(it) }
|
discovered
|
||||||
|
.filter { it.uri !in trackedUris && it.resolvedCount > 0 }
|
||||||
|
.map { Action.Create(it) }
|
||||||
return trackedActions + createActions
|
return trackedActions + createActions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,13 +25,8 @@ import org.oxycblt.musikr.Music
|
|||||||
class PlaylistReconcilerTest {
|
class PlaylistReconcilerTest {
|
||||||
@Test
|
@Test
|
||||||
fun reconcile_unchangedFile_keepsTheStoredPlaylist() {
|
fun reconcile_unchangedFile_keepsTheStoredPlaylist() {
|
||||||
val tracked =
|
val tracked = tracked(42L)
|
||||||
PlaylistReconciler.Tracked(
|
val discovered = discovered(42L, resolvedCount = 1)
|
||||||
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))
|
val actions = PlaylistReconciler.reconcile(listOf(tracked), listOf(discovered))
|
||||||
|
|
||||||
@@ -40,13 +35,8 @@ class PlaylistReconcilerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun reconcile_changedFile_replacesTheStoredPlaylist() {
|
fun reconcile_changedFile_replacesTheStoredPlaylist() {
|
||||||
val tracked =
|
val tracked = tracked(42L)
|
||||||
PlaylistReconciler.Tracked(
|
val discovered = discovered(43L, resolvedCount = 1)
|
||||||
Music.UID.auxio(Music.UID.Item.PLAYLIST),
|
|
||||||
"content://tree/playlists/x.m3u",
|
|
||||||
42L,
|
|
||||||
)
|
|
||||||
val discovered = PlaylistReconciler.Discovered("content://tree/playlists/x.m3u", 43L)
|
|
||||||
|
|
||||||
val actions = PlaylistReconciler.reconcile(listOf(tracked), listOf(discovered))
|
val actions = PlaylistReconciler.reconcile(listOf(tracked), listOf(discovered))
|
||||||
|
|
||||||
@@ -54,25 +44,49 @@ class PlaylistReconcilerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun reconcile_deletedFile_deletesTheStoredPlaylist() {
|
fun reconcile_deletedFile_keepsTheStoredPlaylist() {
|
||||||
val tracked =
|
val tracked = tracked(42L)
|
||||||
PlaylistReconciler.Tracked(
|
|
||||||
Music.UID.auxio(Music.UID.Item.PLAYLIST),
|
|
||||||
"content://tree/playlists/x.m3u",
|
|
||||||
42L,
|
|
||||||
)
|
|
||||||
|
|
||||||
val actions = PlaylistReconciler.reconcile(listOf(tracked), emptyList())
|
val actions = PlaylistReconciler.reconcile(listOf(tracked), emptyList())
|
||||||
|
|
||||||
assertEquals(listOf(PlaylistReconciler.Action.Delete(tracked)), actions)
|
assertEquals(listOf(PlaylistReconciler.Action.Keep(tracked)), actions)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun reconcile_changedFileWithNothingResolved_keepsTheStoredPlaylist() {
|
||||||
|
val tracked = tracked(42L)
|
||||||
|
val discovered = discovered(43L, resolvedCount = 0)
|
||||||
|
|
||||||
|
val actions = PlaylistReconciler.reconcile(listOf(tracked), listOf(discovered))
|
||||||
|
|
||||||
|
assertEquals(listOf(PlaylistReconciler.Action.Keep(tracked)), actions)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun reconcile_newFile_createsATrackedPlaylist() {
|
fun reconcile_newFile_createsATrackedPlaylist() {
|
||||||
val discovered = PlaylistReconciler.Discovered("content://tree/playlists/x.m3u", 42L)
|
val discovered = discovered(42L, resolvedCount = 1)
|
||||||
|
|
||||||
val actions = PlaylistReconciler.reconcile(emptyList(), listOf(discovered))
|
val actions = PlaylistReconciler.reconcile(emptyList(), listOf(discovered))
|
||||||
|
|
||||||
assertEquals(listOf(PlaylistReconciler.Action.Create(discovered)), actions)
|
assertEquals(listOf(PlaylistReconciler.Action.Create(discovered)), actions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun reconcile_newFileWithNothingResolved_doesNothing() {
|
||||||
|
val discovered = discovered(42L, resolvedCount = 0)
|
||||||
|
|
||||||
|
val actions = PlaylistReconciler.reconcile(emptyList(), listOf(discovered))
|
||||||
|
|
||||||
|
assertEquals(emptyList<PlaylistReconciler.Action>(), actions)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun tracked(signature: Long) =
|
||||||
|
PlaylistReconciler.Tracked(
|
||||||
|
Music.UID.auxio(Music.UID.Item.PLAYLIST),
|
||||||
|
"content://tree/playlists/x.m3u",
|
||||||
|
signature,
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun discovered(signature: Long, resolvedCount: Int) =
|
||||||
|
PlaylistReconciler.Discovered("content://tree/playlists/x.m3u", signature, resolvedCount)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user