musikr.cover: refactor cover

Instead of using a weird sealed class, instead go for a
Cover/CoverCollection system instead that removes some implicit
design dependence in musikr.
This commit is contained in:
Alexander Capehart
2024-12-24 14:43:48 -05:00
parent a24d102a00
commit 7768d98632
15 changed files with 205 additions and 76 deletions
@@ -26,6 +26,7 @@ import java.util.UUID
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import org.oxycblt.musikr.cover.Cover
import org.oxycblt.musikr.cover.CoverCollection
import org.oxycblt.musikr.fs.Format
import org.oxycblt.musikr.fs.Path
import org.oxycblt.musikr.tag.Date
@@ -276,7 +277,7 @@ interface Song : Music {
/** The date the audio file was added to the device, as a unix epoch timestamp. */
val dateAdded: Long
/** Useful information to quickly obtain the album cover. */
val cover: Cover.Single?
val cover: Cover?
/**
* The parent [Album]. If the metadata did not specify an album, it's parent directory is used
* instead.
@@ -310,7 +311,7 @@ interface Album : MusicParent {
*/
val releaseType: ReleaseType
/** Cover information from album's songs. */
val cover: Cover
val covers: CoverCollection
/** The duration of all songs in the album, in milliseconds. */
val durationMs: Long
/** The earliest date a song in this album was added, as a unix epoch timestamp. */
@@ -340,7 +341,7 @@ interface Artist : MusicParent {
*/
val durationMs: Long?
/** Useful information to quickly obtain a (single) cover for a Genre. */
val cover: Cover
val covers: CoverCollection
/** The [Genre]s of this artist. */
val genres: List<Genre>
}
@@ -356,7 +357,7 @@ interface Genre : MusicParent {
/** The total duration of the songs in this genre, in milliseconds. */
val durationMs: Long
/** Useful information to quickly obtain a (single) cover for a Genre. */
val cover: Cover
val covers: CoverCollection
}
/**
@@ -370,5 +371,5 @@ interface Playlist : MusicParent {
/** The total duration of the songs in this genre, in milliseconds. */
val durationMs: Long
/** Useful information to quickly obtain a (single) cover for a Genre. */
val cover: Cover
val covers: CoverCollection
}
@@ -19,29 +19,22 @@
package org.oxycblt.musikr.cover
import java.io.InputStream
import org.oxycblt.musikr.Song
sealed interface Cover {
interface Cover {
val id: String
interface Single : Cover {
suspend fun open(): InputStream?
}
class Multi(val all: List<Single>) : Cover {
override val id = "multi@${all.hashCode()}"
}
suspend fun open(): InputStream?
}
class CoverCollection private constructor(val covers: List<Cover>) {
companion object {
fun multi(songs: Collection<Song>) = order(songs).run { Multi(this) }
private fun order(songs: Collection<Song>) =
songs
.mapNotNull { it.cover }
.groupBy { it.id }
.entries
.sortedByDescending { it.key }
.sortedByDescending { it.value.size }
.map { it.value.first() }
fun from(covers: Collection<Cover>) =
CoverCollection(
covers
.groupBy { it.id }
.entries
.sortedByDescending { it.key }
.sortedByDescending { it.value.size }
.map { it.value.first() })
}
}
@@ -21,7 +21,7 @@ package org.oxycblt.musikr.cover
import android.content.Context
interface StoredCovers {
suspend fun obtain(id: String): Cover.Single?
suspend fun obtain(id: String): Cover?
companion object {
fun from(context: Context, path: String, format: CoverFormat): MutableStoredCovers =
@@ -30,7 +30,7 @@ interface StoredCovers {
}
interface MutableStoredCovers : StoredCovers {
suspend fun write(data: ByteArray): Cover.Single?
suspend fun write(data: ByteArray): Cover?
}
private class FileStoredCovers(
@@ -45,6 +45,6 @@ private class FileStoredCovers(
}
}
private class FileCover(override val id: String, private val coverFile: CoverFile) : Cover.Single {
private class FileCover(override val id: String, private val coverFile: CoverFile) : Cover {
override suspend fun open() = coverFile.open()
}
@@ -22,7 +22,7 @@ import org.oxycblt.musikr.Album
import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.Song
import org.oxycblt.musikr.cover.Cover
import org.oxycblt.musikr.cover.CoverCollection
import org.oxycblt.musikr.tag.Date
import org.oxycblt.musikr.tag.interpret.PreAlbum
import org.oxycblt.musikr.util.update
@@ -56,7 +56,7 @@ internal class AlbumImpl(private val core: AlbumCore) : Album {
override val releaseType = preAlbum.releaseType
override val durationMs = core.songs.sumOf { it.durationMs }
override val dateAdded = core.songs.minOf { it.dateAdded }
override val cover = Cover.multi(core.songs)
override val covers = CoverCollection.from(core.songs.mapNotNull { it.cover })
override val dates: Date.Range? =
core.songs.mapNotNull { it.date }.ifEmpty { null }?.run { Date.Range(min(), max()) }
@@ -23,7 +23,7 @@ import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Genre
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.Song
import org.oxycblt.musikr.cover.Cover
import org.oxycblt.musikr.cover.CoverCollection
import org.oxycblt.musikr.tag.interpret.PreArtist
import org.oxycblt.musikr.util.update
@@ -55,7 +55,7 @@ internal class ArtistImpl(private val core: ArtistCore) : Artist {
get() = core.resolveGenres().toList()
override val durationMs = core.songs.sumOf { it.durationMs }
override val cover = Cover.multi(core.songs)
override val covers = CoverCollection.from(core.songs.mapNotNull { it.cover })
private val hashCode =
31 * (31 * uid.hashCode() + core.preArtist.hashCode()) * core.songs.hashCode()
@@ -22,7 +22,7 @@ import org.oxycblt.musikr.Artist
import org.oxycblt.musikr.Genre
import org.oxycblt.musikr.Music
import org.oxycblt.musikr.Song
import org.oxycblt.musikr.cover.Cover
import org.oxycblt.musikr.cover.CoverCollection
import org.oxycblt.musikr.tag.interpret.PreGenre
import org.oxycblt.musikr.util.update
@@ -44,7 +44,7 @@ internal class GenreImpl(private val core: GenreCore) : Genre {
override val songs = core.songs
override val artists = core.artists
override val durationMs = core.songs.sumOf { it.durationMs }
override val cover = Cover.multi(core.songs)
override val covers = CoverCollection.from(core.songs.mapNotNull { it.cover })
private val hashCode = 31 * (31 * uid.hashCode() + core.preGenre.hashCode()) + songs.hashCode()
@@ -20,7 +20,7 @@ package org.oxycblt.musikr.model
import org.oxycblt.musikr.Playlist
import org.oxycblt.musikr.Song
import org.oxycblt.musikr.cover.Cover
import org.oxycblt.musikr.cover.CoverCollection
import org.oxycblt.musikr.playlist.interpret.PrePlaylistInfo
import org.oxycblt.musikr.tag.Name
@@ -33,7 +33,7 @@ internal class PlaylistImpl(val core: PlaylistCore) : Playlist {
override val uid = core.prePlaylist.handle.uid
override val name: Name.Known = core.prePlaylist.name
override val durationMs = core.songs.sumOf { it.durationMs }
override val cover = Cover.multi(core.songs)
override val covers = CoverCollection.from(core.songs.mapNotNull { it.cover })
override val songs = core.songs
private var hashCode =
@@ -163,7 +163,7 @@ internal data class RawSong(
val file: DeviceFile,
val properties: Properties,
val tags: ParsedTags,
val cover: Cover.Single?
val cover: Cover?
)
internal sealed interface ExtractedMusic {
@@ -48,7 +48,7 @@ internal data class PreSong(
val replayGainAdjustment: ReplayGainAdjustment,
val lastModified: Long,
val dateAdded: Long,
val cover: Cover.Single?,
val cover: Cover?,
val preAlbum: PreAlbum,
val preArtists: List<PreArtist>,
val preGenres: List<PreGenre>