From 3435978ed57bcdd274451f733c02151732f051dc Mon Sep 17 00:00:00 2001 From: AbdallahMehiz Date: Thu, 15 Aug 2024 20:31:45 +0100 Subject: [PATCH] feat: sort files in file picker --- .../mehiz/mpvkt/ui/home/FilePickerScreen.kt | 4 +++- .../mehiz/mpvkt/ui/utils/FilesComparator.kt | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/live/mehiz/mpvkt/ui/utils/FilesComparator.kt diff --git a/app/src/main/java/live/mehiz/mpvkt/ui/home/FilePickerScreen.kt b/app/src/main/java/live/mehiz/mpvkt/ui/home/FilePickerScreen.kt index 626563f..e6d6463 100644 --- a/app/src/main/java/live/mehiz/mpvkt/ui/home/FilePickerScreen.kt +++ b/app/src/main/java/live/mehiz/mpvkt/ui/home/FilePickerScreen.kt @@ -46,6 +46,7 @@ import com.github.k1rakishou.fsaf.file.AbstractFile import `is`.xyz.mpv.Utils import live.mehiz.mpvkt.R import live.mehiz.mpvkt.presentation.Screen +import live.mehiz.mpvkt.ui.utils.FilesComparator import org.koin.compose.koinInject import java.lang.Long.signum import java.text.StringCharacterIterator @@ -101,7 +102,8 @@ data class FilePickerScreen(val uri: String) : Screen() { val fileList = fileManager.listFiles(directory).filterNot { !Utils.MEDIA_EXTENSIONS.contains(fileManager.getName(it).substringAfterLast('.')) && fileManager.isFile(it) || fileManager.getName(it).startsWith('.') - } + }.sortedWith(FilesComparator(fileManager)) + LazyColumn(modifier) { item { FileListing( diff --git a/app/src/main/java/live/mehiz/mpvkt/ui/utils/FilesComparator.kt b/app/src/main/java/live/mehiz/mpvkt/ui/utils/FilesComparator.kt new file mode 100644 index 0000000..f7e2a85 --- /dev/null +++ b/app/src/main/java/live/mehiz/mpvkt/ui/utils/FilesComparator.kt @@ -0,0 +1,19 @@ +package live.mehiz.mpvkt.ui.utils + +import com.github.k1rakishou.fsaf.FileManager +import com.github.k1rakishou.fsaf.file.AbstractFile + +/** + * Sorts files/directories alphabetically while giving directories priority + * credit goes to mpv-android + */ +class FilesComparator( + private val fileManager: FileManager +) : Comparator { + override fun compare(o1: AbstractFile?, o2: AbstractFile?): Int { + val iso1ADirectory = fileManager.isDirectory(o1!!) + val iso2ADirectory = fileManager.isDirectory(o2!!) + if (iso1ADirectory != iso2ADirectory) return if (iso2ADirectory) 1 else -1 + return fileManager.getName(o1).compareTo(fileManager.getName(o2), ignoreCase = true) + } +}