musikr: further streamline tree query

This commit is contained in:
Alexander Capehart
2025-05-13 20:45:51 -06:00
parent 3aa56ad5b6
commit 84cf0bf29f
2 changed files with 116 additions and 96 deletions
@@ -22,26 +22,21 @@ import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.provider.DocumentsContract import android.provider.DocumentsContract
import kotlinx.coroutines.CompletableDeferred import android.util.Log
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flatMapMerge
import kotlinx.coroutines.flow.flattenMerge
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.flow.takeWhile
import org.oxycblt.musikr.fs.MusicLocation import org.oxycblt.musikr.fs.MusicLocation
import org.oxycblt.musikr.fs.Path import org.oxycblt.musikr.fs.Path
import org.oxycblt.musikr.fs.device.FiniteHotFlow.HotObject
internal interface DeviceFS { internal interface DeviceFS {
fun explore(locations: Flow<MusicLocation>): Flow<DeviceDirectory> fun explore(locations: Flow<MusicLocation>): Flow<DeviceDirectory>
@@ -58,37 +53,47 @@ private class DeviceFSImpl(
private val withHidden: Boolean private val withHidden: Boolean
) : DeviceFS { ) : DeviceFS {
override fun explore(locations: Flow<MusicLocation>): Flow<DeviceDirectory> = override fun explore(locations: Flow<MusicLocation>): Flow<DeviceDirectory> =
locations.map { location -> locations.mapNotNull { location ->
coroutineScope { val treeDocumentId =
DeviceDirectoryImpl( DocumentsContract.getTreeDocumentId(location.uri)
val uri = DocumentsContract.buildDocumentUriUsingTree(location.uri, treeDocumentId)
val modifiedMs = contentResolver.useQuery(
uri,
arrayOf(DocumentsContract.Document.COLUMN_LAST_MODIFIED)
) { cursor ->
if (!cursor.moveToFirst()) return@useQuery null
val lastModifiedIndex =
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_LAST_MODIFIED)
cursor.getLong(lastModifiedIndex)
}
if (modifiedMs == null) {
return@mapNotNull null
}
query(
location.uri, location.uri,
DocumentsContract.getTreeDocumentId(location.uri), treeDocumentId,
location.path, location.path,
null, 0,
coroutineScope = CoroutineScope(Dispatchers.IO), null
contentResolver = contentResolver,
withHidden = withHidden
) )
} }
}
}
private class DeviceDirectoryImpl( private suspend fun query(
rootUri: Uri, rootUri: Uri,
treeDocumentId: String, treeDocumentId: String,
override val path: Path, path: Path,
override val parent: DeviceDirectoryImpl?, modifiedMs: Long,
coroutineScope: CoroutineScope, parent: DeviceDirectory?,
contentResolver: ContentResolver, ): DeviceDirectory = coroutineScope {
withHidden: Boolean val dir = DeviceDirectoryImpl(
) : DeviceDirectory { uri = DocumentsContract.buildDocumentUriUsingTree(rootUri, treeDocumentId),
private sealed interface HotObject { path = path,
data class More(val value: DeviceFSEntry) : HotObject modifiedMs = modifiedMs,
data object Done : HotObject parent = parent,
} children = emptyFlow()
)
override val uri: Uri = DocumentsContract.buildDocumentUriUsingTree(rootUri, treeDocumentId) dir.children = flow {
override val children = flow { Log.d("DeviceFS", "Finished querying $path")
contentResolver.useQuery( contentResolver.useQuery(
DocumentsContract.buildChildDocumentsUriUsingTree( DocumentsContract.buildChildDocumentsUriUsingTree(
rootUri, rootUri,
@@ -119,17 +124,15 @@ private class DeviceDirectoryImpl(
val lastModified = cursor.getLong(lastModifiedIndex) val lastModified = cursor.getLong(lastModifiedIndex)
if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) { if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) {
val dir = val subdir =
DeviceDirectoryImpl( query(
rootUri, rootUri = rootUri,
childId, treeDocumentId = childId,
path = newPath, path = newPath,
parent = this@DeviceDirectoryImpl, modifiedMs = modifiedMs,
coroutineScope, parent = dir
contentResolver,
withHidden
) )
emit(HotObject.More(dir)) emit(StreamedFile.More(subdir))
} else { } else {
val size = cursor.getLong(sizeIndex) val size = cursor.getLong(sizeIndex)
val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId) val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId)
@@ -140,16 +143,23 @@ private class DeviceDirectoryImpl(
path = newPath, path = newPath,
size = size, size = size,
modifiedMs = lastModified, modifiedMs = lastModified,
parent = this@DeviceDirectoryImpl parent = dir
) )
emit(HotObject.More(file)) emit(StreamedFile.More(file))
} }
} }
emit(HotObject.Done) emit(StreamedFile.Done)
}
}.shareIn(this, SharingStarted.Eagerly, replay = Int.MAX_VALUE)
.takeWhile { it is StreamedFile.More }
.map { (it as StreamedFile.More).value }
dir
}
private sealed interface StreamedFile {
data class More(val value: DeviceFSEntry) : StreamedFile
data object Done : StreamedFile
} }
}.shareIn(coroutineScope, SharingStarted.Lazily, replay = Int.MAX_VALUE)
.takeWhile { it is HotObject.More }
.map { (it as HotObject.More).value }
private companion object { private companion object {
private val PROJECTION = private val PROJECTION =
@@ -163,4 +173,13 @@ private class DeviceDirectoryImpl(
} }
} }
private class DeviceDirectoryImpl(
override val uri: Uri,
override val path: Path,
override val modifiedMs: Long,
override val parent: DeviceDirectory?,
override var children: Flow<DeviceFSEntry>,
) : DeviceDirectory
@@ -32,6 +32,7 @@ import org.oxycblt.musikr.fs.Path
sealed interface DeviceFSEntry { sealed interface DeviceFSEntry {
val uri: Uri val uri: Uri
val path: Path val path: Path
val modifiedMs: Long
} }
interface DeviceDirectory : DeviceFSEntry { interface DeviceDirectory : DeviceFSEntry {
@@ -42,7 +43,7 @@ interface DeviceDirectory : DeviceFSEntry {
data class DeviceFile( data class DeviceFile(
override val uri: Uri, override val uri: Uri,
override val path: Path, override val path: Path,
val modifiedMs: Long, override val modifiedMs: Long,
val mimeType: String, val mimeType: String,
val size: Long, val size: Long,
val parent: DeviceDirectory val parent: DeviceDirectory