musikr.fs: simplify streaming tree impl

This commit is contained in:
Alexander Capehart
2025-05-13 19:58:38 -06:00
parent 6905b51e66
commit 3aa56ad5b6
5 changed files with 38 additions and 44 deletions
@@ -19,6 +19,7 @@
package org.oxycblt.musikr package org.oxycblt.musikr
import android.content.Context import android.content.Context
import android.util.Log
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.buffer import kotlinx.coroutines.flow.buffer
@@ -119,6 +120,7 @@ private class MusikrImpl(
locations: List<MusicLocation>, locations: List<MusicLocation>,
onProgress: suspend (IndexingProgress) -> Unit onProgress: suspend (IndexingProgress) -> Unit
) = coroutineScope { ) = coroutineScope {
val start = System.currentTimeMillis()
var exploredCount = 0 var exploredCount = 0
var extractedCount = 0 var extractedCount = 0
val explored = val explored =
@@ -134,6 +136,9 @@ private class MusikrImpl(
.onEach { onProgress(IndexingProgress.Songs(++extractedCount, exploredCount)) } .onEach { onProgress(IndexingProgress.Songs(++extractedCount, exploredCount)) }
.onCompletion { onProgress(IndexingProgress.Indeterminate) } .onCompletion { onProgress(IndexingProgress.Indeterminate) }
val library = evaluateStep.evaluate(extracted) val library = evaluateStep.evaluate(extracted)
Log.d(
"Musikr",
"Loaded ${library.songs.size} songs in ${System.currentTimeMillis() - start}ms")
LibraryResultImpl(storage, library) LibraryResultImpl(storage, library)
} }
} }
@@ -101,7 +101,7 @@ class MutableFSCovers(private val context: Context) : MutableCovers<FDCover> {
// query to finally finish to be able to have a complete list of siblings to search for. // query to finally finish to be able to have a complete list of siblings to search for.
val parent = file.parent val parent = file.parent
val bestCover = val bestCover =
parent.children.flow() parent.children
.filterIsInstance<DeviceFile>() .filterIsInstance<DeviceFile>()
.map { it to coverArtScore(it) } .map { it to coverArtScore(it) }
.toList() .toList()
@@ -29,14 +29,19 @@ 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.asFlow import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.emitAll import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flatMapMerge import kotlinx.coroutines.flow.flatMapMerge
import kotlinx.coroutines.flow.flattenMerge 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.shareIn
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>
@@ -77,8 +82,13 @@ private class DeviceDirectoryImpl(
contentResolver: ContentResolver, contentResolver: ContentResolver,
withHidden: Boolean withHidden: Boolean
) : DeviceDirectory { ) : DeviceDirectory {
private sealed interface HotObject {
data class More(val value: DeviceFSEntry) : HotObject
data object Done : HotObject
}
override val uri: Uri = DocumentsContract.buildDocumentUriUsingTree(rootUri, treeDocumentId) override val uri: Uri = DocumentsContract.buildDocumentUriUsingTree(rootUri, treeDocumentId)
override val children = FiniteHotFlow<DeviceFSEntry>(coroutineScope) { emit -> override val children = flow {
contentResolver.useQuery( contentResolver.useQuery(
DocumentsContract.buildChildDocumentsUriUsingTree( DocumentsContract.buildChildDocumentsUriUsingTree(
rootUri, rootUri,
@@ -109,17 +119,17 @@ 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) {
emit( val dir =
DeviceDirectoryImpl( DeviceDirectoryImpl(
rootUri, rootUri,
childId, childId,
path = newPath, path = newPath,
parent = this, parent = this@DeviceDirectoryImpl,
coroutineScope, coroutineScope,
contentResolver, contentResolver,
withHidden withHidden
) )
) emit(HotObject.More(dir))
} else { } else {
val size = cursor.getLong(sizeIndex) val size = cursor.getLong(sizeIndex)
val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId) val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId)
@@ -130,13 +140,16 @@ private class DeviceDirectoryImpl(
path = newPath, path = newPath,
size = size, size = size,
modifiedMs = lastModified, modifiedMs = lastModified,
parent = this parent = this@DeviceDirectoryImpl
) )
emit(file) emit(HotObject.More(file))
} }
} }
emit(HotObject.Done)
} }
} }.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 =
@@ -22,7 +22,10 @@ import android.net.Uri
import kotlinx.coroutines.Deferred import kotlinx.coroutines.Deferred
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flatMapMerge import kotlinx.coroutines.flow.flatMapMerge
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
import org.oxycblt.musikr.fs.Path import org.oxycblt.musikr.fs.Path
@@ -33,7 +36,7 @@ sealed interface DeviceFSEntry {
interface DeviceDirectory : DeviceFSEntry { interface DeviceDirectory : DeviceFSEntry {
val parent: DeviceDirectory? val parent: DeviceDirectory?
val children: FiniteHotFlow<DeviceFSEntry> val children: Flow<DeviceFSEntry>
} }
data class DeviceFile( data class DeviceFile(
@@ -46,9 +49,13 @@ data class DeviceFile(
) : DeviceFSEntry ) : DeviceFSEntry
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
fun DeviceDirectory.flatten(): Flow<DeviceFile> = children.flow().flatMapMerge { fun DeviceDirectory.flatten(): Flow<DeviceFile> = flow {
when (it) { val recursive = mutableListOf<Flow<DeviceFile>>()
is DeviceDirectory -> it.flatten() children.collect {
is DeviceFile -> flowOf(it) when (it) {
is DeviceDirectory -> recursive.add(it.flatten())
is DeviceFile -> emit(it)
}
} }
emitAll(recursive.asFlow().flatMapMerge { it })
} }
@@ -1,31 +0,0 @@
package org.oxycblt.musikr.fs.device
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.takeWhile
class FiniteHotFlow<T>(scope: CoroutineScope, emitter: suspend (emit: suspend (T) -> Unit) -> Unit) {
private sealed interface HotObject<T> {
data class More<T>(val value: T) : HotObject<T>
data object Done : HotObject<Nothing>
}
private val sharedFlow = flow {
emitter {
emit(HotObject.More(it))
}
emit(HotObject.Done)
}.shareIn(scope, SharingStarted.Lazily, replay = Int.MAX_VALUE)
@Suppress("UNCHECKED_CAST")
fun flow() = flow {
sharedFlow.takeWhile { it is HotObject.More<*> }.collect {
emit((it as HotObject.More<*>).value as T)
}
}
}