music: split off music location into musikr

This commit is contained in:
Alexander Capehart
2024-12-07 11:46:38 -07:00
parent abeabcb8df
commit 3eaa96ffda
6 changed files with 57 additions and 32 deletions
@@ -26,6 +26,7 @@ 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.model.MutableLibrary
import org.oxycblt.musikr.pipeline.EvaluateStep
import org.oxycblt.musikr.pipeline.ExploreStep
@@ -34,7 +35,7 @@ import org.oxycblt.musikr.tag.Interpretation
interface Indexer {
suspend fun run(
uris: List<Uri>,
locations: MusicLocation,
interpretation: Interpretation,
onProgress: suspend (IndexingProgress) -> Unit = {}
): MutableLibrary
@@ -59,7 +60,7 @@ constructor(
private val evaluateStep: EvaluateStep
) : Indexer {
override suspend fun run(
uris: List<Uri>,
locations: MusicLocation,
interpretation: Interpretation,
onProgress: suspend (IndexingProgress) -> Unit
) = coroutineScope {
@@ -35,7 +35,7 @@ import org.oxycblt.musikr.fs.path.DocumentPathFactory
import timber.log.Timber
interface DeviceFiles {
fun explore(uris: Flow<Uri>): Flow<DeviceFile>
fun explore(locations: Flow<MusicLocation>): Flow<DeviceFile>
}
data class DeviceFile(
@@ -50,19 +50,17 @@ data class DeviceFile(
class DeviceFilesImpl
@Inject
constructor(
@ApplicationContext private val context: Context,
private val documentPathFactory: DocumentPathFactory
@ApplicationContext private val context: Context
) : DeviceFiles {
private val contentResolver = context.contentResolverSafe
override fun explore(uris: Flow<Uri>): Flow<DeviceFile> =
uris.flatMapMerge { rootUri ->
Timber.d("$rootUri")
override fun explore(locations: Flow<MusicLocation>): Flow<DeviceFile> =
locations.flatMapMerge { location ->
exploreImpl(
contentResolver,
rootUri,
DocumentsContract.getTreeDocumentId(rootUri),
requireNotNull(documentPathFactory.unpackDocumentTreeUri(rootUri)))
location.uri,
DocumentsContract.getTreeDocumentId(location.uri),
location.path)
}
private fun exploreImpl(
@@ -56,4 +56,6 @@ interface FsBindsModule {
fun documentPathFactory(documentTreePathFactory: DocumentPathFactoryImpl): DocumentPathFactory
@Binds fun deviceFiles(deviceFilesImpl: DeviceFilesImpl): DeviceFiles
@Binds fun musicLocationFactory(musicLocationFactoryImpl: MusicLocationFactoryImpl): MusicLocation.Factory
}
@@ -0,0 +1,36 @@
package org.oxycblt.musikr.fs
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.DocumentsContract
import dagger.hilt.android.qualifiers.ApplicationContext
import org.oxycblt.musikr.fs.path.DocumentPathFactory
import javax.inject.Inject
class MusicLocation internal constructor(val uri: Uri, val path: Path) {
override fun equals(other: Any?) =
other is MusicLocation && uri == other.uri && path == other.path
override fun hashCode() = 31 * uri.hashCode() + path.hashCode()
override fun toString() = "src:$uri=$path"
interface Factory {
fun create(uri: Uri): MusicLocation?
}
}
class MusicLocationFactoryImpl @Inject constructor(
@ApplicationContext private val context: Context,
private val documentPathFactory: DocumentPathFactory
) : MusicLocation.Factory {
override fun create(uri: Uri): MusicLocation? {
check(DocumentsContract.isTreeUri(uri)) { "URI $uri is not a document tree URI" }
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
context.contentResolverSafe.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
return MusicLocation(uri, path)
}
}