musikr.fs: simplify streaming tree impl
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
package org.oxycblt.musikr
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.flow.buffer
|
||||
@@ -119,6 +120,7 @@ private class MusikrImpl(
|
||||
locations: List<MusicLocation>,
|
||||
onProgress: suspend (IndexingProgress) -> Unit
|
||||
) = coroutineScope {
|
||||
val start = System.currentTimeMillis()
|
||||
var exploredCount = 0
|
||||
var extractedCount = 0
|
||||
val explored =
|
||||
@@ -134,6 +136,9 @@ private class MusikrImpl(
|
||||
.onEach { onProgress(IndexingProgress.Songs(++extractedCount, exploredCount)) }
|
||||
.onCompletion { onProgress(IndexingProgress.Indeterminate) }
|
||||
val library = evaluateStep.evaluate(extracted)
|
||||
Log.d(
|
||||
"Musikr",
|
||||
"Loaded ${library.songs.size} songs in ${System.currentTimeMillis() - start}ms")
|
||||
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.
|
||||
val parent = file.parent
|
||||
val bestCover =
|
||||
parent.children.flow()
|
||||
parent.children
|
||||
.filterIsInstance<DeviceFile>()
|
||||
.map { it to coverArtScore(it) }
|
||||
.toList()
|
||||
|
||||
@@ -29,14 +29,19 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.asFlow
|
||||
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.map
|
||||
import kotlinx.coroutines.flow.shareIn
|
||||
import kotlinx.coroutines.flow.takeWhile
|
||||
import org.oxycblt.musikr.fs.MusicLocation
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
import org.oxycblt.musikr.fs.device.FiniteHotFlow.HotObject
|
||||
|
||||
internal interface DeviceFS {
|
||||
fun explore(locations: Flow<MusicLocation>): Flow<DeviceDirectory>
|
||||
@@ -77,8 +82,13 @@ private class DeviceDirectoryImpl(
|
||||
contentResolver: ContentResolver,
|
||||
withHidden: Boolean
|
||||
) : 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 children = FiniteHotFlow<DeviceFSEntry>(coroutineScope) { emit ->
|
||||
override val children = flow {
|
||||
contentResolver.useQuery(
|
||||
DocumentsContract.buildChildDocumentsUriUsingTree(
|
||||
rootUri,
|
||||
@@ -109,17 +119,17 @@ private class DeviceDirectoryImpl(
|
||||
val lastModified = cursor.getLong(lastModifiedIndex)
|
||||
|
||||
if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) {
|
||||
emit(
|
||||
val dir =
|
||||
DeviceDirectoryImpl(
|
||||
rootUri,
|
||||
childId,
|
||||
path = newPath,
|
||||
parent = this,
|
||||
parent = this@DeviceDirectoryImpl,
|
||||
coroutineScope,
|
||||
contentResolver,
|
||||
withHidden
|
||||
)
|
||||
)
|
||||
emit(HotObject.More(dir))
|
||||
} else {
|
||||
val size = cursor.getLong(sizeIndex)
|
||||
val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId)
|
||||
@@ -130,13 +140,16 @@ private class DeviceDirectoryImpl(
|
||||
path = newPath,
|
||||
size = size,
|
||||
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 val PROJECTION =
|
||||
|
||||
@@ -22,7 +22,10 @@ import android.net.Uri
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.asFlow
|
||||
import kotlinx.coroutines.flow.emitAll
|
||||
import kotlinx.coroutines.flow.flatMapMerge
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
|
||||
@@ -33,7 +36,7 @@ sealed interface DeviceFSEntry {
|
||||
|
||||
interface DeviceDirectory : DeviceFSEntry {
|
||||
val parent: DeviceDirectory?
|
||||
val children: FiniteHotFlow<DeviceFSEntry>
|
||||
val children: Flow<DeviceFSEntry>
|
||||
}
|
||||
|
||||
data class DeviceFile(
|
||||
@@ -46,9 +49,13 @@ data class DeviceFile(
|
||||
) : DeviceFSEntry
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
fun DeviceDirectory.flatten(): Flow<DeviceFile> = children.flow().flatMapMerge {
|
||||
when (it) {
|
||||
is DeviceDirectory -> it.flatten()
|
||||
is DeviceFile -> flowOf(it)
|
||||
fun DeviceDirectory.flatten(): Flow<DeviceFile> = flow {
|
||||
val recursive = mutableListOf<Flow<DeviceFile>>()
|
||||
children.collect {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user