diff --git a/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt b/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt index e0b3873a7..2d2e04230 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/MusicRepository.kt @@ -391,7 +391,7 @@ constructor( val storage = Storage(cache, covers, storedPlaylists) val interpretation = Interpretation(nameFactory, separators) - val newLibrary = + val result = Musikr.new(context, storage, interpretation).run(locations, ::emitIndexingProgress) // Music loading completed, update the revision right now so we re-use this work // later. @@ -399,11 +399,9 @@ constructor( // Deliver the library to the rest of the app // This will more or less block until all required item translation and // cleanup finishes. - emitLibrary(newLibrary) - // Old cover revisions may be lying around, even during a normal refresh due - // to really lucky cancellations. Now that it is impossible for the rest of - // the app to possible be using them, clean them up. - covers.cleanup(newLibrary.songs.mapNotNull { it.cover }) + emitLibrary(result.library) + // Clean up old data that is now impossible for the app to be using. + result.cleanup() // Finish up loading. emitIndexingCompletion(null) } diff --git a/musikr/src/main/java/org/oxycblt/musikr/Config.kt b/musikr/src/main/java/org/oxycblt/musikr/Config.kt index cdd5a3f56..6670131f2 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/Config.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/Config.kt @@ -30,4 +30,4 @@ data class Storage( val storedPlaylists: StoredPlaylists ) -data class Interpretation(val naming: Naming, val separators: Separators) +data class Interpretation(val naming: Naming, val separators: Separators) \ No newline at end of file diff --git a/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt b/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt index 55081a442..af40c1102 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt @@ -34,17 +34,24 @@ interface Musikr { suspend fun run( locations: List, onProgress: suspend (IndexingProgress) -> Unit = {} - ): MutableLibrary + ): LibraryResult companion object { fun new(context: Context, storage: Storage, interpretation: Interpretation): Musikr = MusikrImpl( + storage, ExploreStep.from(context, storage), ExtractStep.from(context, storage), EvaluateStep.new(storage, interpretation)) } } +interface LibraryResult { + val library: MutableLibrary + + suspend fun cleanup() +} + /** * Represents the current progress of music loading. * @@ -57,6 +64,7 @@ sealed interface IndexingProgress { } private class MusikrImpl( + private val storage: Storage, private val exploreStep: ExploreStep, private val extractStep: ExtractStep, private val evaluateStep: EvaluateStep @@ -79,6 +87,16 @@ private class MusikrImpl( .buffer(Channel.UNLIMITED) .onEach { onProgress(IndexingProgress.Songs(++extractedCount, exploredCount)) } .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 }) + } +} \ No newline at end of file diff --git a/musikr/src/main/java/org/oxycblt/musikr/tag/interpret/Interpretation.kt b/musikr/src/main/java/org/oxycblt/musikr/tag/interpret/Interpretation.kt new file mode 100644 index 000000000..414d2363a --- /dev/null +++ b/musikr/src/main/java/org/oxycblt/musikr/tag/interpret/Interpretation.kt @@ -0,0 +1 @@ +package org.oxycblt.musikr.tag.interpret