DroidFS/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerElement.kt

88 lines
3.3 KiB
Kotlin
Raw Normal View History

2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs.explorers
2022-01-13 19:23:37 +01:00
import sushi.hardcore.droidfs.collation.getCollationKeyForFileName
2022-06-18 21:13:16 +02:00
import sushi.hardcore.droidfs.filesystems.Stat
import sushi.hardcore.droidfs.util.PathUtils
2022-01-13 19:23:37 +01:00
import java.text.Collator
2020-07-17 16:35:39 +02:00
2022-06-18 21:13:16 +02:00
class ExplorerElement(val name: String, val stat: Stat, val parentPath: String) {
2020-11-03 17:22:09 +01:00
val fullPath: String = PathUtils.pathJoin(parentPath, name)
2022-01-13 19:23:37 +01:00
val collationKey = Collator.getInstance().getCollationKeyForFileName(fullPath)
2020-07-17 16:35:39 +02:00
val isDirectory: Boolean
2022-06-18 21:13:16 +02:00
get() = stat.type == Stat.S_IFDIR
2020-07-17 16:35:39 +02:00
val isRegularFile: Boolean
2022-06-18 21:13:16 +02:00
get() = stat.type == Stat.S_IFREG
2020-07-17 16:35:39 +02:00
2022-06-18 21:13:16 +02:00
val isSymlink: Boolean
get() = stat.type == Stat.S_IFLNK
2022-06-18 21:13:16 +02:00
val isParentFolder: Boolean
get() = stat.type == Stat.PARENT_FOLDER_TYPE
companion object {
2021-09-05 14:36:03 +02:00
@JvmStatic
//this function is needed because I had some problems calling the constructor from JNI, probably due to arguments with default values
2022-06-18 21:13:16 +02:00
fun new(name: String, elementType: Int, size: Long, mTime: Long, parentPath: String): ExplorerElement {
return ExplorerElement(name, Stat(elementType, size, mTime*1000), parentPath)
2021-09-05 14:36:03 +02:00
}
private fun foldersFirst(a: ExplorerElement, b: ExplorerElement, default: () -> Int): Int {
return if (a.isDirectory && b.isRegularFile) {
-1
} else if (b.isDirectory && a.isRegularFile) {
1
} else {
default()
}
}
private fun doSort(a: ExplorerElement, b: ExplorerElement, foldersFirst: Boolean, sorter: () -> Int): Int {
2021-11-11 20:14:48 +01:00
return if (b.isParentFolder) {
1
} else if (a.isParentFolder) {
-1
} else {
2021-11-11 20:14:48 +01:00
if (foldersFirst) {
foldersFirst(a, b, sorter)
} else {
sorter()
}
}
}
fun sortBy(sortOrder: String, foldersFirst: Boolean, explorerElements: MutableList<ExplorerElement>) {
2020-08-04 11:44:29 +02:00
when (sortOrder) {
"name" -> {
explorerElements.sortWith { a, b ->
2022-01-13 19:23:37 +01:00
doSort(a, b, foldersFirst) { a.collationKey.compareTo(b.collationKey) }
}
2020-08-04 11:44:29 +02:00
}
"size" -> {
explorerElements.sortWith { a, b ->
2022-06-18 21:13:16 +02:00
doSort(a, b, foldersFirst) { (a.stat.size - b.stat.size).toInt() }
}
2020-08-04 11:44:29 +02:00
}
"date" -> {
explorerElements.sortWith { a, b ->
2022-06-18 21:13:16 +02:00
doSort(a, b, foldersFirst) { a.stat.mTime.compareTo(b.stat.mTime) }
}
2020-08-04 11:44:29 +02:00
}
"name_desc" -> {
explorerElements.sortWith { a, b ->
2022-01-13 19:23:37 +01:00
doSort(a, b, foldersFirst) { b.collationKey.compareTo(a.collationKey) }
}
2020-08-04 11:44:29 +02:00
}
"size_desc" -> {
explorerElements.sortWith { a, b ->
2022-06-18 21:13:16 +02:00
doSort(a, b, foldersFirst) { (b.stat.size - a.stat.size).toInt() }
}
2020-08-04 11:44:29 +02:00
}
"date_desc" -> {
explorerElements.sortWith { a, b ->
2022-06-18 21:13:16 +02:00
doSort(a, b, foldersFirst) { b.stat.mTime.compareTo(a.stat.mTime) }
}
2020-08-04 11:44:29 +02:00
}
}
}
}
2020-07-17 16:35:39 +02:00
}