musikr: bundle cleanup into api

Prevents as much footguns.
This commit is contained in:
Alexander Capehart 2025-01-06 13:16:31 -07:00
parent 6587d2259b
commit 5e168860e7
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
4 changed files with 26 additions and 9 deletions

View file

@ -391,7 +391,7 @@ constructor(
val storage = Storage(cache, covers, storedPlaylists) val storage = Storage(cache, covers, storedPlaylists)
val interpretation = Interpretation(nameFactory, separators) val interpretation = Interpretation(nameFactory, separators)
val newLibrary = val result =
Musikr.new(context, storage, interpretation).run(locations, ::emitIndexingProgress) Musikr.new(context, storage, interpretation).run(locations, ::emitIndexingProgress)
// Music loading completed, update the revision right now so we re-use this work // Music loading completed, update the revision right now so we re-use this work
// later. // later.
@ -399,11 +399,9 @@ constructor(
// Deliver the library to the rest of the app // Deliver the library to the rest of the app
// This will more or less block until all required item translation and // This will more or less block until all required item translation and
// cleanup finishes. // cleanup finishes.
emitLibrary(newLibrary) emitLibrary(result.library)
// Old cover revisions may be lying around, even during a normal refresh due // Clean up old data that is now impossible for the app to be using.
// to really lucky cancellations. Now that it is impossible for the rest of result.cleanup()
// the app to possible be using them, clean them up.
covers.cleanup(newLibrary.songs.mapNotNull { it.cover })
// Finish up loading. // Finish up loading.
emitIndexingCompletion(null) emitIndexingCompletion(null)
} }

View file

@ -30,4 +30,4 @@ data class Storage(
val storedPlaylists: StoredPlaylists val storedPlaylists: StoredPlaylists
) )
data class Interpretation(val naming: Naming, val separators: Separators) data class Interpretation(val naming: Naming, val separators: Separators)

View file

@ -34,17 +34,24 @@ interface Musikr {
suspend fun run( suspend fun run(
locations: List<MusicLocation>, locations: List<MusicLocation>,
onProgress: suspend (IndexingProgress) -> Unit = {} onProgress: suspend (IndexingProgress) -> Unit = {}
): MutableLibrary ): LibraryResult
companion object { companion object {
fun new(context: Context, storage: Storage, interpretation: Interpretation): Musikr = fun new(context: Context, storage: Storage, interpretation: Interpretation): Musikr =
MusikrImpl( MusikrImpl(
storage,
ExploreStep.from(context, storage), ExploreStep.from(context, storage),
ExtractStep.from(context, storage), ExtractStep.from(context, storage),
EvaluateStep.new(storage, interpretation)) EvaluateStep.new(storage, interpretation))
} }
} }
interface LibraryResult {
val library: MutableLibrary
suspend fun cleanup()
}
/** /**
* Represents the current progress of music loading. * Represents the current progress of music loading.
* *
@ -57,6 +64,7 @@ sealed interface IndexingProgress {
} }
private class MusikrImpl( private class MusikrImpl(
private val storage: Storage,
private val exploreStep: ExploreStep, private val exploreStep: ExploreStep,
private val extractStep: ExtractStep, private val extractStep: ExtractStep,
private val evaluateStep: EvaluateStep private val evaluateStep: EvaluateStep
@ -79,6 +87,16 @@ private class MusikrImpl(
.buffer(Channel.UNLIMITED) .buffer(Channel.UNLIMITED)
.onEach { onProgress(IndexingProgress.Songs(++extractedCount, exploredCount)) } .onEach { onProgress(IndexingProgress.Songs(++extractedCount, exploredCount)) }
.onCompletion { onProgress(IndexingProgress.Indeterminate) } .onCompletion { onProgress(IndexingProgress.Indeterminate) }
evaluateStep.evaluate(extracted) val library = evaluateStep.evaluate(extracted)
LibraryResultImpl(storage, library)
} }
} }
private class LibraryResultImpl(
private val storage: Storage,
override val library: MutableLibrary) : LibraryResult {
override suspend fun cleanup() {
storage.storedCovers.cleanup(library.songs.mapNotNull { it.cover })
}
}

View file

@ -0,0 +1 @@
package org.oxycblt.musikr.tag.interpret