From 3a542f3a4d8c86fda51336127c0f656acebd5a26 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 31 Dec 2023 23:37:32 +0100 Subject: [PATCH] fix: Sort batches --- .../myzel394/alibi/helpers/BatchesFolder.kt | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt b/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt index 8e10bb2..6cd59b4 100644 --- a/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt +++ b/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt @@ -18,7 +18,7 @@ import java.time.format.DateTimeFormatter import com.arthenica.ffmpegkit.FFmpegKitConfig import android.util.Log import androidx.annotation.RequiresApi -import androidx.core.net.toFile +import androidx.core.net.toUri import app.myzel394.alibi.ui.RECORDER_INTERNAL_SELECTED_VALUE import app.myzel394.alibi.ui.RECORDER_MEDIA_SELECTED_VALUE import app.myzel394.alibi.ui.SUPPORTS_SCOPED_STORAGE @@ -112,15 +112,19 @@ abstract class BatchesFolder( } fun getBatchesForFFmpeg(): List { + // TODO: There is probably a better way to do this iteratively, look at it if you have time return when (type) { BatchType.INTERNAL -> - (getInternalFolder() + ((getInternalFolder() .listFiles() ?.filter { it.nameWithoutExtension.toIntOrNull() != null } ?.toList() - ?: emptyList()) + ?: emptyList()) as List) + .sortedBy { + it.nameWithoutExtension.toInt() + } .map { it.absolutePath } BatchType.CUSTOM -> getCustomDefinedFolder() @@ -128,6 +132,9 @@ abstract class BatchesFolder( .filter { it.name?.substringBeforeLast(".")?.toIntOrNull() != null } + .sortedBy { + it.name!!.substringBeforeLast(".").toInt() + } .map { FFmpegKitConfig.getSafParameterForRead( context, @@ -136,24 +143,32 @@ abstract class BatchesFolder( } BatchType.MEDIA -> { - val filePaths = mutableListOf() + val fileUris = mutableListOf() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { queryMediaContent { _, _, uri, _ -> - filePaths.add( - FFmpegKitConfig.getSafParameterForRead( - context, - uri, - )!! - ) + fileUris.add(uri) } } else { legacyMediaFolder.listFiles()?.forEach { - filePaths.add(it.absolutePath) + fileUris.add(it.toUri()) } } - filePaths + fileUris + .sortedBy { + return@sortedBy it + .lastPathSegment!! + .substring(mediaPrefix.length) + .substringBeforeLast(".") + .toInt() + } + .map { uri -> + FFmpegKitConfig.getSafParameterForRead( + context, + uri, + )!! + } } } } @@ -367,6 +382,7 @@ abstract class BatchesFolder( null, ) } else { + // TODO: Fix "would you like to try saving" -> Save button legacyMediaFolder.listFiles()?.forEach { val fileCounter = it.nameWithoutExtension.substring(mediaPrefix.length).toIntOrNull()