diff --git a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt index 18685bcbc..bb580008c 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/fs/device/DeviceFS.kt @@ -75,7 +75,8 @@ private class DeviceFSImpl( location.uri, DocumentsContract.getTreeDocumentId(location.uri), location.path, - null) + null + ) } private fun exploreDirectoryImpl( @@ -99,52 +100,38 @@ private class DeviceFSImpl( 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 = - 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) - while (cursor.moveToNext()) { - val childId = cursor.getString(childUriIndex) - val displayName = cursor.getString(displayNameIndex) + while (cursor.moveToNext()) { + val childId = cursor.getString(childUriIndex) + val displayName = cursor.getString(displayNameIndex) - // Skip hidden files/directories if ignoreHidden is true - if (!withHidden && displayName.startsWith(".")) { - continue - } + // Skip hidden files/directories if ignoreHidden is true + if (!withHidden && displayName.startsWith(".")) { + continue + } - val newPath = relativePath.file(displayName) - val mimeType = cursor.getString(mimeTypeIndex) - val lastModified = cursor.getLong(lastModifiedIndex) + val newPath = relativePath.file(displayName) + val mimeType = cursor.getString(mimeTypeIndex) + val lastModified = cursor.getLong(lastModifiedIndex) - if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) { - recursive.add( - exploreDirectoryImpl(rootUri, childId, newPath, directoryDeferred) + if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) { + recursive.add( + exploreDirectoryImpl(rootUri, childId, newPath, directoryDeferred) + ) + } else { + val size = cursor.getLong(sizeIndex) + val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId) + val file = + DeviceFile( + uri = childUri, + mimeType = mimeType, + path = newPath, + size = size, + modifiedMs = lastModified, + parent = directoryDeferred ) - } else { - val size = cursor.getLong(sizeIndex) - val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId) - val file = - DeviceFile( - uri = childUri, - mimeType = mimeType, - path = newPath, - size = size, - modifiedMs = lastModified, - parent = directoryDeferred - ) - children.add(file) - emit(file) - } + children.add(file) + emit(file) } } directoryDeferred.complete(DeviceDirectory(uri, relativePath, parent, children)) @@ -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 + ) } } diff --git a/musikr/src/main/java/org/oxycblt/musikr/pipeline/EvaluateStep.kt b/musikr/src/main/java/org/oxycblt/musikr/pipeline/EvaluateStep.kt index a8b519734..a28fdab8c 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/pipeline/EvaluateStep.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/pipeline/EvaluateStep.kt @@ -52,7 +52,7 @@ private class EvaluateStepImpl( override suspend fun evaluate(extractedMusic: Flow): MutableLibrary = extractedMusic .filterIsInstance() - .fold(MusicGraph.builder()) { graphBuilder, extracted -> + .tryFold(MusicGraph.builder()) { graphBuilder, extracted -> when (extracted) { is RawSong -> graphBuilder.add(tagInterpreter.interpret(extracted)) is RawPlaylist -> diff --git a/musikr/src/main/java/org/oxycblt/musikr/pipeline/FlowUtil.kt b/musikr/src/main/java/org/oxycblt/musikr/pipeline/FlowUtil.kt index 6609e78dd..e5cd78a23 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/pipeline/FlowUtil.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/pipeline/FlowUtil.kt @@ -26,39 +26,6 @@ import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.withIndex -internal sealed interface Divert { - data class Left(val value: L) : Divert - - data class Right(val value: R) : Divert -} - -internal class DivertedFlow( - val manager: Flow, - val left: Flow, - val right: Flow -) - -internal inline fun Flow.divert( - crossinline predicate: (T) -> Divert -): DivertedFlow { - val leftChannel = Channel(Channel.UNLIMITED) - val rightChannel = Channel(Channel.UNLIMITED) - val managedFlow = - flow { - 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(val manager: Flow, val flows: Flow>) - /** * Equally "distributes" the values of some flow across n new flows. * @@ -95,24 +62,14 @@ internal fun Flow.tryMap(transform: suspend (T) -> R): Flow = flow } } -internal fun Flow.tryMapNotNull(transform: suspend (T) -> R?): Flow = flow { - collect { value -> - try { - transform(value)?.let { emit(it) } - } catch (e: Exception) { - throw PipelineException(value, e) - } - } -} - -internal fun Flow.tryFold(initial: A, operation: suspend (A, T) -> A): Flow = flow { +internal suspend fun Flow.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 +} \ No newline at end of file