musikr: refactor musiclocation api into location
Improves the API surface pretty heavily. Assisted by Claude 4 Sonnet/Claude 3.5 Haiku.
This commit is contained in:
@@ -21,10 +21,17 @@ package org.oxycblt.musikr
|
||||
import org.oxycblt.musikr.cache.MutableCache
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.MutableCovers
|
||||
import org.oxycblt.musikr.fs.Location
|
||||
import org.oxycblt.musikr.fs.OpenedLocation
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
import org.oxycblt.musikr.tag.interpret.Naming
|
||||
import org.oxycblt.musikr.tag.interpret.Separators
|
||||
|
||||
data class Query(
|
||||
val source: List<OpenedLocation>,
|
||||
val exclude: List<Location>
|
||||
)
|
||||
|
||||
/** Side-effect laden [Storage] for use during music loading and [MutableLibrary] operation. */
|
||||
data class Storage(
|
||||
/**
|
||||
|
||||
@@ -25,7 +25,6 @@ import kotlinx.coroutines.flow.buffer
|
||||
import kotlinx.coroutines.flow.onCompletion
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import org.oxycblt.musikr.fs.MusicLocation
|
||||
import org.oxycblt.musikr.pipeline.EvaluateStep
|
||||
import org.oxycblt.musikr.pipeline.ExploreStep
|
||||
import org.oxycblt.musikr.pipeline.ExtractStep
|
||||
@@ -47,13 +46,13 @@ interface Musikr {
|
||||
/**
|
||||
* Start loading music from the given [locations] and the configuration provided earlier.
|
||||
*
|
||||
* @param locations The [MusicLocation]s to search for music in.
|
||||
* @param query The [Query] to load music from.
|
||||
* @param onProgress Optional callback to receive progress on the current status of the music
|
||||
* pipeline. Warning: These events will be rapid-fire.
|
||||
* @return A handle to the newly created library alongside further cleanup.
|
||||
*/
|
||||
suspend fun run(
|
||||
locations: List<MusicLocation>,
|
||||
query: Query,
|
||||
onProgress: suspend (IndexingProgress) -> Unit = {}
|
||||
): LibraryResult
|
||||
|
||||
@@ -70,6 +69,7 @@ interface Musikr {
|
||||
*/
|
||||
fun new(context: Context, storage: Storage, interpretation: Interpretation): Musikr =
|
||||
MusikrImpl(
|
||||
context,
|
||||
storage,
|
||||
ExploreStep.from(context, storage, interpretation),
|
||||
ExtractStep.from(context, storage),
|
||||
@@ -96,7 +96,7 @@ sealed interface IndexingProgress {
|
||||
/**
|
||||
* Currently indexing and extracting tags from device music.
|
||||
*
|
||||
* @param explored The amount of music currently found from the given [MusicLocation]s.
|
||||
* @param explored The amount of music currently found from the given [Query].
|
||||
* @param loaded The amount of music that has had metadata extracted and parsed.
|
||||
*/
|
||||
data class Songs(val loaded: Int, val explored: Int) : IndexingProgress
|
||||
@@ -110,20 +110,21 @@ sealed interface IndexingProgress {
|
||||
}
|
||||
|
||||
private class MusikrImpl(
|
||||
private val context: Context,
|
||||
private val storage: Storage,
|
||||
private val exploreStep: ExploreStep,
|
||||
private val extractStep: ExtractStep,
|
||||
private val evaluateStep: EvaluateStep
|
||||
) : Musikr {
|
||||
override suspend fun run(
|
||||
locations: List<MusicLocation>,
|
||||
query: Query,
|
||||
onProgress: suspend (IndexingProgress) -> Unit
|
||||
) = coroutineScope {
|
||||
var exploredCount = 0
|
||||
var extractedCount = 0
|
||||
val explored =
|
||||
exploreStep
|
||||
.explore(locations)
|
||||
.explore(query)
|
||||
.buffer(Channel.UNLIMITED)
|
||||
.onStart { onProgress(IndexingProgress.Songs(0, 0)) }
|
||||
.onEach { onProgress(IndexingProgress.Songs(extractedCount, ++exploredCount)) }
|
||||
|
||||
+33
-31
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Auxio Project
|
||||
* MusicLocation.kt is part of Auxio.
|
||||
* Location.kt is part of Auxio.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -24,49 +24,51 @@ import android.net.Uri
|
||||
import android.provider.DocumentsContract
|
||||
import org.oxycblt.musikr.fs.device.contentResolverSafe
|
||||
import org.oxycblt.musikr.fs.path.DocumentPathFactory
|
||||
import org.oxycblt.musikr.util.splitEscaped
|
||||
|
||||
class MusicLocation private constructor(val uri: Uri, val path: Path) {
|
||||
override fun equals(other: Any?) = other is MusicLocation && uri == other.uri
|
||||
class Location private constructor(val uri: Uri, val path: Path) {
|
||||
override fun equals(other: Any?) = other is Location && uri == other.uri
|
||||
|
||||
override fun hashCode() = 31 * uri.hashCode()
|
||||
|
||||
override fun toString(): String = uri.toString()
|
||||
|
||||
companion object {
|
||||
fun new(context: Context, uri: Uri): MusicLocation? {
|
||||
if (!DocumentsContract.isTreeUri(uri)) return null
|
||||
val documentPathFactory = DocumentPathFactory.from(context)
|
||||
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
|
||||
val notPersisted =
|
||||
context.contentResolverSafe.persistedUriPermissions.none {
|
||||
it.uri == uri && it.isReadPermission && it.isWritePermission
|
||||
}
|
||||
if (notPersisted) {
|
||||
fun open(context: Context): OpenedLocation? {
|
||||
if (isUnopened(context, uri)) {
|
||||
try {
|
||||
context.contentResolverSafe.takePersistableUriPermission(
|
||||
uri,
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
// Not fully sure what happens if I'm disallowed to take the permission,
|
||||
// check for both circumstances (error or no-op)
|
||||
return null
|
||||
}
|
||||
return MusicLocation(uri, path)
|
||||
|
||||
if (isUnopened(context, uri)) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
return OpenedLocation(uri, path)
|
||||
}
|
||||
|
||||
private fun isUnopened(context: Context, uri: Uri) =
|
||||
context.contentResolverSafe.persistedUriPermissions.none {
|
||||
it.uri == uri && it.isReadPermission && it.isWritePermission
|
||||
}
|
||||
|
||||
fun existing(context: Context, uri: Uri): MusicLocation? {
|
||||
val documentPathFactory = DocumentPathFactory.from(context)
|
||||
companion object {
|
||||
fun from(context: Context, uri: Uri): Location? {
|
||||
if (!DocumentsContract.isTreeUri(uri)) return null
|
||||
val notPersisted =
|
||||
context.contentResolverSafe.persistedUriPermissions.none {
|
||||
it.uri == uri && it.isReadPermission && it.isWritePermission
|
||||
}
|
||||
if (notPersisted) return null
|
||||
val documentPathFactory = DocumentPathFactory.from(context)
|
||||
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
|
||||
return MusicLocation(uri, path)
|
||||
}
|
||||
|
||||
fun toString(list: List<MusicLocation>) =
|
||||
list.joinToString(";") { it.uri.toString().replace(";", "\\;") }
|
||||
|
||||
fun existing(context: Context, string: String): List<MusicLocation> {
|
||||
return string.splitEscaped { it == ';' }.mapNotNull { existing(context, Uri.parse(it)) }
|
||||
return Location(uri, path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class OpenedLocation internal constructor(val uri: Uri, val path: Path) {
|
||||
override fun equals(other: Any?) = other is OpenedLocation && uri == other.uri && path == other.path
|
||||
override fun hashCode() = 31 * uri.hashCode() + path.hashCode()
|
||||
override fun toString(): String = uri.toString()
|
||||
}
|
||||
@@ -28,18 +28,20 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.asFlow
|
||||
import kotlinx.coroutines.flow.emitAll
|
||||
import org.oxycblt.musikr.Query
|
||||
import kotlinx.coroutines.flow.flatMapMerge
|
||||
import kotlinx.coroutines.flow.flattenMerge
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import org.oxycblt.musikr.fs.MusicLocation
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
|
||||
internal interface DeviceFS {
|
||||
fun explore(locations: Flow<MusicLocation>): Flow<DeviceFile>
|
||||
fun explore(query: Query): Flow<DeviceFile>
|
||||
|
||||
companion object {
|
||||
fun from(context: Context, withHidden: Boolean): DeviceFS =
|
||||
DeviceFSImpl(context.contentResolverSafe, withHidden)
|
||||
fun from(
|
||||
context: Context,
|
||||
withHidden: Boolean
|
||||
): DeviceFS = DeviceFSImpl(context.contentResolverSafe, withHidden)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,8 +50,8 @@ private class DeviceFSImpl(
|
||||
private val contentResolver: ContentResolver,
|
||||
private val withHidden: Boolean
|
||||
) : DeviceFS {
|
||||
override fun explore(locations: Flow<MusicLocation>): Flow<DeviceFile> =
|
||||
locations.flatMapMerge { location ->
|
||||
override fun explore(query: Query): Flow<DeviceFile> =
|
||||
query.source.asFlow().flatMapMerge { location ->
|
||||
exploreDirectoryImpl(
|
||||
location.uri,
|
||||
DocumentsContract.getTreeDocumentId(location.uri),
|
||||
@@ -57,6 +59,7 @@ private class DeviceFSImpl(
|
||||
null)
|
||||
}
|
||||
|
||||
|
||||
private fun exploreDirectoryImpl(
|
||||
rootUri: Uri,
|
||||
treeDocumentId: String,
|
||||
@@ -94,7 +97,8 @@ private class DeviceFSImpl(
|
||||
|
||||
if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) {
|
||||
recursive.add(
|
||||
exploreDirectoryImpl(rootUri, childId, newPath, directoryDeferred))
|
||||
exploreDirectoryImpl(rootUri, childId, newPath, directoryDeferred)
|
||||
)
|
||||
} else {
|
||||
val size = cursor.getLong(sizeIndex)
|
||||
val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId)
|
||||
@@ -105,7 +109,8 @@ private class DeviceFSImpl(
|
||||
path = newPath,
|
||||
size = size,
|
||||
modifiedMs = lastModified,
|
||||
parent = directoryDeferred)
|
||||
parent = directoryDeferred
|
||||
)
|
||||
children.add(file)
|
||||
emit(file)
|
||||
}
|
||||
|
||||
@@ -26,11 +26,13 @@ import kotlinx.coroutines.flow.asFlow
|
||||
import kotlinx.coroutines.flow.buffer
|
||||
import kotlinx.coroutines.flow.emitAll
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.flatMapMerge
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.merge
|
||||
import org.oxycblt.musikr.Interpretation
|
||||
import org.oxycblt.musikr.Query
|
||||
import org.oxycblt.musikr.Storage
|
||||
import org.oxycblt.musikr.cache.Cache
|
||||
import org.oxycblt.musikr.cache.CacheResult
|
||||
@@ -38,18 +40,20 @@ import org.oxycblt.musikr.cache.CachedSong
|
||||
import org.oxycblt.musikr.covers.Cover
|
||||
import org.oxycblt.musikr.covers.CoverResult
|
||||
import org.oxycblt.musikr.covers.Covers
|
||||
import org.oxycblt.musikr.fs.MusicLocation
|
||||
import org.oxycblt.musikr.fs.OpenedLocation
|
||||
import org.oxycblt.musikr.fs.device.DeviceFS
|
||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||
import org.oxycblt.musikr.playlist.m3u.M3U
|
||||
|
||||
internal interface ExploreStep {
|
||||
fun explore(locations: List<MusicLocation>): Flow<Explored>
|
||||
fun explore(query: Query): Flow<Explored>
|
||||
|
||||
companion object {
|
||||
fun from(context: Context, storage: Storage, interpretation: Interpretation): ExploreStep =
|
||||
ExploreStepImpl(
|
||||
DeviceFS.from(context, interpretation.withHidden),
|
||||
DeviceFS.from(
|
||||
context = context,
|
||||
withHidden = interpretation.withHidden),
|
||||
storage.cache,
|
||||
storage.covers,
|
||||
storage.storedPlaylists)
|
||||
@@ -62,13 +66,11 @@ private class ExploreStepImpl(
|
||||
private val covers: Covers<out Cover>,
|
||||
private val storedPlaylists: StoredPlaylists
|
||||
) : ExploreStep {
|
||||
override fun explore(locations: List<MusicLocation>): Flow<Explored> {
|
||||
override fun explore(query: Query): Flow<Explored> {
|
||||
val addingMs = System.currentTimeMillis()
|
||||
return merge(
|
||||
deviceFS
|
||||
.explore(
|
||||
locations.asFlow(),
|
||||
)
|
||||
.explore(query)
|
||||
.filter { it.mimeType.startsWith("audio/") || it.mimeType == M3U.MIME_TYPE }
|
||||
.distributedMap(n = 8, on = Dispatchers.IO, buffer = Channel.UNLIMITED) { file ->
|
||||
when (val cacheResult = cache.read(file)) {
|
||||
|
||||
@@ -22,11 +22,11 @@ import android.content.Context
|
||||
import android.database.ContentObserver
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import org.oxycblt.musikr.fs.MusicLocation
|
||||
import org.oxycblt.musikr.fs.OpenedLocation
|
||||
|
||||
internal class LocationObserver(
|
||||
private val context: Context,
|
||||
private val location: MusicLocation,
|
||||
private val location: OpenedLocation,
|
||||
private val listener: UpdateTracker.Callback
|
||||
) : ContentObserver(Handler(Looper.getMainLooper())), Runnable {
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
@@ -19,15 +19,15 @@
|
||||
package org.oxycblt.musikr.track
|
||||
|
||||
import android.content.Context
|
||||
import org.oxycblt.musikr.fs.MusicLocation
|
||||
import org.oxycblt.musikr.fs.OpenedLocation
|
||||
|
||||
interface UpdateTracker {
|
||||
fun track(locations: List<MusicLocation>)
|
||||
fun track(locations: List<OpenedLocation>)
|
||||
|
||||
fun release()
|
||||
|
||||
interface Callback {
|
||||
fun onUpdate(location: MusicLocation)
|
||||
fun onUpdate(location: OpenedLocation)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@@ -42,7 +42,7 @@ private class UpdateTrackerImpl(
|
||||
) : UpdateTracker {
|
||||
private val observers = mutableListOf<LocationObserver>()
|
||||
|
||||
override fun track(locations: List<MusicLocation>) {
|
||||
override fun track(locations: List<OpenedLocation>) {
|
||||
release()
|
||||
observers.addAll(locations.map { LocationObserver(context, it, callback) })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user