musikr: introduce null covers

Will be used once covers are made configurable.
This commit is contained in:
Alexander Capehart 2024-12-28 09:51:46 -05:00
parent d3f4ed5dd4
commit c6e83d1e18
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
4 changed files with 55 additions and 12 deletions

View file

@ -15,7 +15,7 @@
* 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
@ -40,6 +40,7 @@ import org.oxycblt.musikr.Playlist
import org.oxycblt.musikr.Song
import org.oxycblt.musikr.Storage
import org.oxycblt.musikr.cache.StoredCache
import org.oxycblt.musikr.cover.CoverIdentifier
import org.oxycblt.musikr.cover.CoverParams
import org.oxycblt.musikr.playlist.db.StoredPlaylists
import org.oxycblt.musikr.tag.interpret.Naming
@ -244,11 +245,15 @@ constructor(
) : MusicRepository {
private val updateListeners = mutableListOf<MusicRepository.UpdateListener>()
private val indexingListeners = mutableListOf<MusicRepository.IndexingListener>()
@Volatile private var indexingWorker: MusicRepository.IndexingWorker? = null
@Volatile
private var indexingWorker: MusicRepository.IndexingWorker? = null
@Volatile override var library: MutableLibrary? = null
@Volatile private var previousCompletedState: IndexingState.Completed? = null
@Volatile private var currentIndexingState: IndexingState? = null
@Volatile
override var library: MutableLibrary? = null
@Volatile
private var previousCompletedState: IndexingState.Completed? = null
@Volatile
private var currentIndexingState: IndexingState? = null
override val indexingState: IndexingState?
get() = currentIndexingState ?: previousCompletedState
@ -388,7 +393,11 @@ constructor(
val currentRevision = musicSettings.revision
val newRevision = currentRevision?.takeIf { withCache } ?: UUID.randomUUID()
val cache = if (withCache) storedCache.visible() else storedCache.invisible()
val covers = SiloedCovers.at(context, CoverSilo(newRevision, CoverParams.of(750, 80)))
val covers = SiloedCovers.from(
context,
CoverSilo(newRevision, CoverParams.of(750, 80)),
CoverIdentifier.md5()
)
val storage = Storage(cache, covers, storedPlaylists)
val interpretation = Interpretation(nameFactory, separators)
@ -414,9 +423,9 @@ constructor(
// TODO: Remove this once you start work on kindred.
deviceLibraryChanged =
this.library?.songs != newLibrary.songs ||
this.library?.albums != newLibrary.albums ||
this.library?.artists != newLibrary.artists ||
this.library?.genres != newLibrary.genres
this.library?.albums != newLibrary.albums ||
this.library?.artists != newLibrary.artists ||
this.library?.genres != newLibrary.genres
userLibraryChanged = this.library?.playlists != newLibrary.playlists
if (!deviceLibraryChanged && !userLibraryChanged) {
L.d("Library has not changed, skipping update")

View file

@ -0,0 +1,9 @@
package org.oxycblt.auxio.music.covers
import android.content.Context
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
suspend fun Context.coversDir() = withContext(Dispatchers.IO) {
filesDir.resolve("covers").apply { mkdirs() }
}

View file

@ -0,0 +1,24 @@
package org.oxycblt.auxio.music.covers
import android.content.Context
import org.oxycblt.musikr.cover.Cover
import org.oxycblt.musikr.cover.CoverIdentifier
import org.oxycblt.musikr.cover.Covers
import org.oxycblt.musikr.cover.MutableCovers
import org.oxycblt.musikr.cover.ObtainResult
import java.io.InputStream
class NullCovers(private val context: Context, private val identifier: CoverIdentifier) : MutableCovers {
override suspend fun obtain(id: String) = ObtainResult.Hit(NullCover(id))
override suspend fun write(data: ByteArray): Cover =
NullCover(identifier.identify(data))
override suspend fun cleanup(excluding: Collection<Cover>) {
context.coversDir().listFiles()?.forEach { it.deleteRecursively() }
}
}
private class NullCover(override val id: String) : Cover {
override suspend fun open() = null
}

View file

@ -25,6 +25,7 @@ import kotlinx.coroutines.withContext
import org.oxycblt.musikr.cover.Cover
import org.oxycblt.musikr.cover.CoverFiles
import org.oxycblt.musikr.cover.CoverFormat
import org.oxycblt.musikr.cover.CoverIdentifier
import org.oxycblt.musikr.cover.Covers
import org.oxycblt.musikr.cover.MutableCovers
import org.oxycblt.musikr.cover.ObtainResult
@ -56,16 +57,16 @@ class SiloedCovers(
}
companion object {
suspend fun at(context: Context, silo: CoverSilo): SiloedCovers {
suspend fun from(context: Context, silo: CoverSilo, identifier: CoverIdentifier): SiloedCovers {
val rootDir: File
val revisionDir: File
withContext(Dispatchers.IO) {
rootDir = context.filesDir.resolve("covers").apply { mkdirs() }
rootDir = context.coversDir()
revisionDir = rootDir.resolve(silo.toString()).apply { mkdirs() }
}
val files = CoverFiles.at(revisionDir)
val format = CoverFormat.jpeg(silo.params)
return SiloedCovers(rootDir, silo, Covers.from(files, format))
return SiloedCovers(rootDir, silo, Covers.from(files, format, identifier))
}
}
}