Skip to content

Commit

Permalink
feat: sort files in file picker
Browse files Browse the repository at this point in the history
  • Loading branch information
abdallahmehiz committed Aug 15, 2024
1 parent b143f3e commit 3435978
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/live/mehiz/mpvkt/ui/utils/FilesComparator.kt
Original file line number Diff line number Diff line change
@@ -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<AbstractFile> {
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)
}
}

0 comments on commit 3435978

Please sign in to comment.