2020-07-17 16:35:39 +02:00
|
|
|
package sushi.hardcore.droidfs.explorers
|
|
|
|
|
2020-08-01 15:39:00 +02:00
|
|
|
import sushi.hardcore.droidfs.util.PathUtils
|
2020-07-17 16:35:39 +02:00
|
|
|
import java.util.*
|
|
|
|
|
2021-08-19 23:13:32 +02:00
|
|
|
class ExplorerElement(val name: String, val elementType: Short, var size: Long = -1, mTime: Long = -1, val parentPath: String) {
|
2021-04-04 12:31:49 +02:00
|
|
|
val mTime = Date((mTime * 1000).toString().toLong())
|
2020-11-03 17:22:09 +01:00
|
|
|
val fullPath: String = PathUtils.pathJoin(parentPath, name)
|
2020-07-17 16:35:39 +02:00
|
|
|
|
|
|
|
val isDirectory: Boolean
|
|
|
|
get() = elementType.toInt() == 0
|
|
|
|
|
|
|
|
val isParentFolder: Boolean
|
|
|
|
get() = elementType.toInt() == -1
|
|
|
|
|
|
|
|
val isRegularFile: Boolean
|
|
|
|
get() = elementType.toInt() == 1
|
|
|
|
|
2020-08-04 11:44:29 +02:00
|
|
|
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
|
|
|
|
fun new(name: String, elementType: Short, size: Long, mTime: Long, parentPath: String): ExplorerElement {
|
|
|
|
return ExplorerElement(name, elementType, size, mTime, parentPath)
|
|
|
|
}
|
|
|
|
|
2021-04-04 12:31:49 +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 {
|
|
|
|
return 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" -> {
|
2021-04-04 12:31:49 +02:00
|
|
|
explorerElements.sortWith { a, b ->
|
|
|
|
doSort(a, b, foldersFirst) { a.fullPath.compareTo(b.fullPath, true) }
|
|
|
|
}
|
2020-08-04 11:44:29 +02:00
|
|
|
}
|
|
|
|
"size" -> {
|
2021-04-04 12:31:49 +02:00
|
|
|
explorerElements.sortWith { a, b ->
|
|
|
|
doSort(a, b, foldersFirst) { (a.size - b.size).toInt() }
|
|
|
|
}
|
2020-08-04 11:44:29 +02:00
|
|
|
}
|
|
|
|
"date" -> {
|
2021-04-04 12:31:49 +02:00
|
|
|
explorerElements.sortWith { a, b ->
|
|
|
|
doSort(a, b, foldersFirst) { a.mTime.compareTo(b.mTime) }
|
|
|
|
}
|
2020-08-04 11:44:29 +02:00
|
|
|
}
|
|
|
|
"name_desc" -> {
|
2021-04-04 12:31:49 +02:00
|
|
|
explorerElements.sortWith { a, b ->
|
|
|
|
doSort(a, b, foldersFirst) { b.fullPath.compareTo(a.fullPath, true) }
|
|
|
|
}
|
2020-08-04 11:44:29 +02:00
|
|
|
}
|
|
|
|
"size_desc" -> {
|
2021-04-04 12:31:49 +02:00
|
|
|
explorerElements.sortWith { a, b ->
|
|
|
|
doSort(a, b, foldersFirst) { (b.size - a.size).toInt() }
|
|
|
|
}
|
2020-08-04 11:44:29 +02:00
|
|
|
}
|
|
|
|
"date_desc" -> {
|
2021-04-04 12:31:49 +02:00
|
|
|
explorerElements.sortWith { a, b ->
|
|
|
|
doSort(a, b, foldersFirst) { b.mTime.compareTo(a.mTime) }
|
|
|
|
}
|
2020-08-04 11:44:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|