music: implement revisioned covers
This commit is contained in:
@@ -47,7 +47,7 @@ import org.oxycblt.musikr.cover.Cover
|
||||
import org.oxycblt.musikr.cover.StoredCovers
|
||||
|
||||
class CoverKeyer @Inject constructor() : Keyer<Cover> {
|
||||
override fun key(data: Cover, options: Options) = "${data.key}&${options.size}"
|
||||
override fun key(data: Cover, options: Options) = "${data.id}&${options.size}"
|
||||
}
|
||||
|
||||
class CoverFetcher
|
||||
@@ -56,16 +56,14 @@ private constructor(
|
||||
private val cover: Cover,
|
||||
private val size: Size,
|
||||
) : Fetcher {
|
||||
private val storedCovers = StoredCovers.from(context, "covers")
|
||||
|
||||
override suspend fun fetch(): FetchResult? {
|
||||
val streams =
|
||||
when (val cover = cover) {
|
||||
is Cover.Single -> listOfNotNull(storedCovers.read(cover))
|
||||
is Cover.Single -> listOfNotNull(cover.open())
|
||||
is Cover.Multi ->
|
||||
buildList {
|
||||
for (single in cover.all) {
|
||||
storedCovers.read(single)?.let { add(it) }
|
||||
single.open()?.let { add(it) }
|
||||
if (size == 4) {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -30,8 +30,10 @@ import kotlinx.coroutines.yield
|
||||
import org.oxycblt.auxio.music.MusicRepository.IndexingWorker
|
||||
import org.oxycblt.musikr.IndexingProgress
|
||||
import org.oxycblt.musikr.Interpretation
|
||||
import org.oxycblt.musikr.Library
|
||||
import org.oxycblt.musikr.Music
|
||||
import org.oxycblt.musikr.Musikr
|
||||
import org.oxycblt.musikr.MutableLibrary
|
||||
import org.oxycblt.musikr.Playlist
|
||||
import org.oxycblt.musikr.Song
|
||||
import org.oxycblt.musikr.Storage
|
||||
@@ -57,7 +59,7 @@ import timber.log.Timber as L
|
||||
*/
|
||||
interface MusicRepository {
|
||||
/** The current library */
|
||||
val library: RevisionedLibrary?
|
||||
val library: Library?
|
||||
|
||||
/** The current state of music loading. Null if no load has occurred yet. */
|
||||
val indexingState: IndexingState?
|
||||
@@ -222,7 +224,7 @@ constructor(
|
||||
private val indexingListeners = mutableListOf<MusicRepository.IndexingListener>()
|
||||
@Volatile private var indexingWorker: MusicRepository.IndexingWorker? = null
|
||||
|
||||
@Volatile override var library: MutableRevisionedLibrary? = null
|
||||
@Volatile override var library: MutableLibrary? = null
|
||||
@Volatile private var previousCompletedState: IndexingState.Completed? = null
|
||||
@Volatile private var currentIndexingState: IndexingState? = null
|
||||
override val indexingState: IndexingState?
|
||||
@@ -365,18 +367,18 @@ constructor(
|
||||
val revision: UUID
|
||||
val storage: Storage
|
||||
if (withCache) {
|
||||
revision = this.library?.revision ?: musicSettings.revision
|
||||
revision = musicSettings.revision
|
||||
storage =
|
||||
Storage(
|
||||
Cache.full(cacheDatabase),
|
||||
StoredCovers.from(context, "covers_$revision"),
|
||||
MutableRevisionedStoredCovers(context, revision),
|
||||
StoredPlaylists.from(playlistDatabase))
|
||||
} else {
|
||||
revision = UUID.randomUUID()
|
||||
storage =
|
||||
Storage(
|
||||
Cache.writeOnly(cacheDatabase),
|
||||
StoredCovers.from(context, "covers_$revision"),
|
||||
MutableRevisionedStoredCovers(context, revision),
|
||||
StoredPlaylists.from(playlistDatabase))
|
||||
}
|
||||
|
||||
@@ -385,8 +387,6 @@ constructor(
|
||||
val newLibrary =
|
||||
Musikr.new(context, storage, interpretation).run(locations, ::emitIndexingProgress)
|
||||
|
||||
val revisionedLibrary = MutableRevisionedLibrary(revision, newLibrary)
|
||||
|
||||
emitIndexingCompletion(null)
|
||||
|
||||
// We want to make sure that all reads and writes are synchronized due to the sheer
|
||||
@@ -408,7 +408,7 @@ constructor(
|
||||
return
|
||||
}
|
||||
|
||||
this.library = revisionedLibrary
|
||||
this.library = newLibrary
|
||||
}
|
||||
|
||||
// Consumers expect their updates to be on the main thread (notably PlaybackService),
|
||||
@@ -418,9 +418,7 @@ constructor(
|
||||
}
|
||||
|
||||
// Quietly update the revision if needed (this way we don't disrupt any new loads)
|
||||
if (!withCache) {
|
||||
musicSettings.revision = revisionedLibrary.revision
|
||||
}
|
||||
musicSettings.revision = revision
|
||||
}
|
||||
|
||||
private suspend fun emitIndexingProgress(progress: IndexingProgress) {
|
||||
|
||||
@@ -15,33 +15,59 @@
|
||||
* 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.auxio.music
|
||||
|
||||
import android.content.Context
|
||||
import androidx.media3.common.util.Log
|
||||
import org.oxycblt.auxio.util.unlikelyToBeNull
|
||||
import java.util.UUID
|
||||
import org.oxycblt.musikr.Library
|
||||
import org.oxycblt.musikr.MutableLibrary
|
||||
import org.oxycblt.musikr.Playlist
|
||||
import org.oxycblt.musikr.Song
|
||||
import org.oxycblt.musikr.cover.Cover
|
||||
import org.oxycblt.musikr.cover.MutableStoredCovers
|
||||
import org.oxycblt.musikr.cover.StoredCovers
|
||||
|
||||
interface RevisionedLibrary : Library {
|
||||
val revision: UUID
|
||||
open class RevisionedStoredCovers(
|
||||
private val context: Context,
|
||||
private val revision: UUID?
|
||||
) : StoredCovers {
|
||||
protected val inner = revision?.let { StoredCovers.at(context, "covers_$it") }
|
||||
|
||||
override suspend fun obtain(id: String): RevisionedCover? {
|
||||
val split = id.split('@', limit = 2)
|
||||
if (split.size != 2) return null
|
||||
val (coverId, coverRevisionStr) = split
|
||||
val coverRevision = coverRevisionStr.toUuidOrNull() ?: return null
|
||||
Log.d("RevisionedStoredCovers", "$coverId $coverRevision $revision")
|
||||
if (revision != null) {
|
||||
if (coverRevision != revision) {
|
||||
return null
|
||||
}
|
||||
val storedCovers = unlikelyToBeNull(inner)
|
||||
return storedCovers.obtain(coverId)?.let { RevisionedCover(revision, it) }
|
||||
} else {
|
||||
val storedCovers = StoredCovers.at(context, "covers_$coverRevision")
|
||||
return storedCovers.obtain(coverId)?.let { RevisionedCover(coverRevision, it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MutableRevisionedLibrary(override val revision: UUID, private val inner: MutableLibrary) :
|
||||
RevisionedLibrary, Library by inner, MutableLibrary {
|
||||
override suspend fun createPlaylist(name: String, songs: List<Song>) =
|
||||
MutableRevisionedLibrary(revision, inner.createPlaylist(name, songs))
|
||||
|
||||
override suspend fun renamePlaylist(playlist: Playlist, name: String) =
|
||||
MutableRevisionedLibrary(revision, inner.renamePlaylist(playlist, name))
|
||||
|
||||
override suspend fun addToPlaylist(playlist: Playlist, songs: List<Song>) =
|
||||
MutableRevisionedLibrary(revision, inner.addToPlaylist(playlist, songs))
|
||||
|
||||
override suspend fun rewritePlaylist(playlist: Playlist, songs: List<Song>) =
|
||||
MutableRevisionedLibrary(revision, inner.rewritePlaylist(playlist, songs))
|
||||
|
||||
override suspend fun deletePlaylist(playlist: Playlist) =
|
||||
MutableRevisionedLibrary(revision, inner.deletePlaylist(playlist))
|
||||
class MutableRevisionedStoredCovers(
|
||||
context: Context,
|
||||
private val revision: UUID
|
||||
) : RevisionedStoredCovers(context, revision), MutableStoredCovers {
|
||||
override suspend fun write(data: ByteArray): RevisionedCover? {
|
||||
return unlikelyToBeNull(inner).write(data)?.let { RevisionedCover(revision, it) }
|
||||
}
|
||||
}
|
||||
|
||||
class RevisionedCover(private val revision: UUID, val inner: Cover.Single) : Cover.Single by inner {
|
||||
override val id: String
|
||||
get() = "${inner.id}@${revision}"
|
||||
}
|
||||
|
||||
internal fun String.toUuidOrNull(): UUID? =
|
||||
try {
|
||||
UUID.fromString(this)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user