musikr: cleanup
This commit is contained in:
parent
f5483b5bc5
commit
9a70ae1c4e
3 changed files with 35 additions and 90 deletions
|
@ -75,7 +75,8 @@ private class DeviceFSImpl(
|
|||
location.uri,
|
||||
DocumentsContract.getTreeDocumentId(location.uri),
|
||||
location.path,
|
||||
null)
|
||||
null
|
||||
)
|
||||
}
|
||||
|
||||
private fun exploreDirectoryImpl(
|
||||
|
@ -90,19 +91,6 @@ private class DeviceFSImpl(
|
|||
val recursive = mutableListOf<Flow<DeviceFile>>()
|
||||
val children = mutableListOf<DeviceNode>()
|
||||
contentResolver.useQuery(uri, PROJECTION) { cursor ->
|
||||
val childUriIndex =
|
||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
||||
val displayNameIndex =
|
||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DISPLAY_NAME)
|
||||
val mimeTypeIndex =
|
||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_MIME_TYPE)
|
||||
val sizeIndex = cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_SIZE)
|
||||
val lastModifiedIndex =
|
||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_LAST_MODIFIED)
|
||||
contentResolver.useQuery(
|
||||
DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, treeDocumentId),
|
||||
PROJECTION
|
||||
) { cursor ->
|
||||
val childUriIndex =
|
||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
||||
val displayNameIndex =
|
||||
|
@ -146,7 +134,6 @@ private class DeviceFSImpl(
|
|||
emit(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
directoryDeferred.complete(DeviceDirectory(uri, relativePath, parent, children))
|
||||
emitAll(recursive.asFlow().flattenMerge())
|
||||
}
|
||||
|
@ -159,6 +146,7 @@ private class DeviceFSImpl(
|
|||
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
|
||||
DocumentsContract.Document.COLUMN_MIME_TYPE,
|
||||
DocumentsContract.Document.COLUMN_SIZE,
|
||||
DocumentsContract.Document.COLUMN_LAST_MODIFIED)
|
||||
DocumentsContract.Document.COLUMN_LAST_MODIFIED
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ private class EvaluateStepImpl(
|
|||
override suspend fun evaluate(extractedMusic: Flow<Extracted>): MutableLibrary =
|
||||
extractedMusic
|
||||
.filterIsInstance<Extracted.Valid>()
|
||||
.fold(MusicGraph.builder()) { graphBuilder, extracted ->
|
||||
.tryFold(MusicGraph.builder()) { graphBuilder, extracted ->
|
||||
when (extracted) {
|
||||
is RawSong -> graphBuilder.add(tagInterpreter.interpret(extracted))
|
||||
is RawPlaylist ->
|
||||
|
|
|
@ -26,39 +26,6 @@ import kotlinx.coroutines.flow.map
|
|||
import kotlinx.coroutines.flow.receiveAsFlow
|
||||
import kotlinx.coroutines.flow.withIndex
|
||||
|
||||
internal sealed interface Divert<L, R> {
|
||||
data class Left<L, R>(val value: L) : Divert<L, R>
|
||||
|
||||
data class Right<L, R>(val value: R) : Divert<L, R>
|
||||
}
|
||||
|
||||
internal class DivertedFlow<L, R>(
|
||||
val manager: Flow<Nothing>,
|
||||
val left: Flow<L>,
|
||||
val right: Flow<R>
|
||||
)
|
||||
|
||||
internal inline fun <T, L, R> Flow<T>.divert(
|
||||
crossinline predicate: (T) -> Divert<L, R>
|
||||
): DivertedFlow<L, R> {
|
||||
val leftChannel = Channel<L>(Channel.UNLIMITED)
|
||||
val rightChannel = Channel<R>(Channel.UNLIMITED)
|
||||
val managedFlow =
|
||||
flow<Nothing> {
|
||||
collect {
|
||||
when (val result = predicate(it)) {
|
||||
is Divert.Left -> leftChannel.send(result.value)
|
||||
is Divert.Right -> rightChannel.send(result.value)
|
||||
}
|
||||
}
|
||||
leftChannel.close()
|
||||
rightChannel.close()
|
||||
}
|
||||
return DivertedFlow(managedFlow, leftChannel.receiveAsFlow(), rightChannel.receiveAsFlow())
|
||||
}
|
||||
|
||||
internal class DistributedFlow<T>(val manager: Flow<Nothing>, val flows: Flow<Flow<T>>)
|
||||
|
||||
/**
|
||||
* Equally "distributes" the values of some flow across n new flows.
|
||||
*
|
||||
|
@ -95,24 +62,14 @@ internal fun <T, R> Flow<T>.tryMap(transform: suspend (T) -> R): Flow<R> = flow
|
|||
}
|
||||
}
|
||||
|
||||
internal fun <T, R> Flow<T>.tryMapNotNull(transform: suspend (T) -> R?): Flow<R> = flow {
|
||||
collect { value ->
|
||||
try {
|
||||
transform(value)?.let { emit(it) }
|
||||
} catch (e: Exception) {
|
||||
throw PipelineException(value, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun <A, T> Flow<T>.tryFold(initial: A, operation: suspend (A, T) -> A): Flow<A> = flow {
|
||||
internal suspend fun <T, A> Flow<T>.tryFold(initial: A, operation: suspend (A, T) -> A): A {
|
||||
var accumulator = initial
|
||||
collect { value ->
|
||||
try {
|
||||
accumulator = operation(accumulator, value)
|
||||
emit(accumulator)
|
||||
} catch (e: Exception) {
|
||||
throw PipelineException(value, e)
|
||||
}
|
||||
}
|
||||
return accumulator
|
||||
}
|
Loading…
Reference in a new issue