From f119ccf477f64574529a742f5948357e47d9f4d8 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Sun, 4 Apr 2021 12:31:49 +0200 Subject: [PATCH] Case insensitive sort & Folders first option --- app/build.gradle | 11 +++-- .../droidfs/explorers/BaseExplorerActivity.kt | 10 ++-- .../droidfs/explorers/ExplorerElement.kt | 46 +++++++++++++++---- .../file_viewers/FileViewerActivity.kt | 4 +- app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/root_preferences.xml | 7 +++ build.gradle | 2 +- 7 files changed, 62 insertions(+), 20 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 12aee1f..efa1c94 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -60,9 +60,10 @@ dependencies { implementation "com.google.android.exoplayer:exoplayer-core:$exoplayer_version" implementation "com.google.android.exoplayer:exoplayer-ui:$exoplayer_version" - def camerax_version = "1.1.0-alpha01" - implementation "androidx.camera:camera-camera2:$camerax_version" - implementation "androidx.camera:camera-lifecycle:$camerax_version" - implementation "androidx.camera:camera-view:1.0.0-alpha21" - implementation "androidx.camera:camera-extensions:1.0.0-alpha21" + def camerax_v1 = "1.1.0-alpha03" + implementation "androidx.camera:camera-camera2:$camerax_v1" + implementation "androidx.camera:camera-lifecycle:$camerax_v1" + def camerax_v2 = "1.0.0-alpha23" + implementation "androidx.camera:camera-view:$camerax_v2" + implementation "androidx.camera:camera-extensions:$camerax_v2" } diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt index 41cb6aa..7301bee 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt @@ -45,6 +45,7 @@ import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder open class BaseExplorerActivity : BaseActivity() { private lateinit var sortOrderEntries: Array private lateinit var sortOrderValues: Array + private var foldersFirst = true private var currentSortOrderIndex = 0 protected lateinit var gocryptfsVolume: GocryptfsVolume private lateinit var volumeName: String @@ -70,6 +71,7 @@ open class BaseExplorerActivity : BaseActivity() { gocryptfsVolume = GocryptfsVolume(sessionID) sortOrderEntries = resources.getStringArray(R.array.sort_orders_entries) sortOrderValues = resources.getStringArray(R.array.sort_orders_values) + foldersFirst = sharedPrefs.getBoolean("folders_first", true) currentSortOrderIndex = resources.getStringArray(R.array.sort_orders_values).indexOf(sharedPrefs.getString(ConstValues.sort_order_key, "name")) init() setSupportActionBar(toolbar) @@ -188,7 +190,7 @@ open class BaseExplorerActivity : BaseActivity() { } private fun sortExplorerElements() { - ExplorerElement.sortBy(sortOrderValues[currentSortOrderIndex], explorerElements) + ExplorerElement.sortBy(sortOrderValues[currentSortOrderIndex], foldersFirst, explorerElements) val sharedPrefsEditor = sharedPrefs.edit() sharedPrefsEditor.putString(ConstValues.sort_order_key, sortOrderValues[currentSortOrderIndex]) sharedPrefsEditor.apply() @@ -249,11 +251,11 @@ open class BaseExplorerActivity : BaseActivity() { } } - private fun createFolder(folder_name: String){ - if (folder_name.isEmpty()) { + private fun createFolder(folderName: String){ + if (folderName.isEmpty()) { Toast.makeText(this, R.string.error_filename_empty, Toast.LENGTH_SHORT).show() } else { - if (!gocryptfsVolume.mkdir(PathUtils.pathJoin(currentDirectoryPath, folder_name))) { + if (!gocryptfsVolume.mkdir(PathUtils.pathJoin(currentDirectoryPath, folderName))) { ColoredAlertDialogBuilder(this) .setTitle(R.string.error) .setMessage(R.string.error_mkdir) diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerElement.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerElement.kt index 68e8b6e..cf2c884 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerElement.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerElement.kt @@ -3,8 +3,8 @@ package sushi.hardcore.droidfs.explorers import sushi.hardcore.droidfs.util.PathUtils import java.util.* -class ExplorerElement(val name: String, val elementType: Short, var size: Long, val mtime: Long, val parentPath: String) { - val mTime = Date((mtime * 1000).toString().toLong()) +class ExplorerElement(val name: String, val elementType: Short, var size: Long, mTime: Long, val parentPath: String) { + val mTime = Date((mTime * 1000).toString().toLong()) val fullPath: String = PathUtils.pathJoin(parentPath, name) val isDirectory: Boolean @@ -17,25 +17,53 @@ class ExplorerElement(val name: String, val elementType: Short, var size: Long, get() = elementType.toInt() == 1 companion object { - fun sortBy(sortOrder: String, explorerElements: MutableList) { + 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) { when (sortOrder) { "name" -> { - explorerElements.sortWith(Comparator { o1, o2 -> o1.fullPath.compareTo(o2.fullPath) }) + explorerElements.sortWith { a, b -> + doSort(a, b, foldersFirst) { a.fullPath.compareTo(b.fullPath, true) } + } } "size" -> { - explorerElements.sortWith(Comparator { o1, o2 -> (o1.size - o2.size).toInt() }) + explorerElements.sortWith { a, b -> + doSort(a, b, foldersFirst) { (a.size - b.size).toInt() } + } } "date" -> { - explorerElements.sortWith(Comparator { o1, o2 -> o1.mTime.compareTo(o2.mTime) }) + explorerElements.sortWith { a, b -> + doSort(a, b, foldersFirst) { a.mTime.compareTo(b.mTime) } + } } "name_desc" -> { - explorerElements.sortWith(Comparator { o1, o2 -> o2.fullPath.compareTo(o1.fullPath) }) + explorerElements.sortWith { a, b -> + doSort(a, b, foldersFirst) { b.fullPath.compareTo(a.fullPath, true) } + } } "size_desc" -> { - explorerElements.sortWith(Comparator { o1, o2 -> (o2.size - o1.size).toInt() }) + explorerElements.sortWith { a, b -> + doSort(a, b, foldersFirst) { (b.size - a.size).toInt() } + } } "date_desc" -> { - explorerElements.sortWith(Comparator { o1, o2 -> o2.mTime.compareTo(o1.mTime) }) + explorerElements.sortWith { a, b -> + doSort(a, b, foldersFirst) { b.mTime.compareTo(a.mTime) } + } } } } diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/FileViewerActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/FileViewerActivity.kt index 15c36ae..db2d022 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/FileViewerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/FileViewerActivity.kt @@ -16,6 +16,7 @@ abstract class FileViewerActivity: BaseActivity() { protected lateinit var filePath: String private var isFinishingIntentionally = false private var usf_keep_open = false + private var foldersFirst = true private var wasMapped = false protected val mappedPlaylist = mutableListOf() protected var currentPlaylistIndex = -1 @@ -26,6 +27,7 @@ abstract class FileViewerActivity: BaseActivity() { val sessionID = intent.getIntExtra("sessionID", -1) gocryptfsVolume = GocryptfsVolume(sessionID) usf_keep_open = sharedPrefs.getBoolean("usf_keep_open", false) + foldersFirst = sharedPrefs.getBoolean("folders_first", true) hideSystemUi() viewFile() } @@ -108,7 +110,7 @@ abstract class FileViewerActivity: BaseActivity() { } } val sortOrder = intent.getStringExtra("sortOrder") ?: "name" - ExplorerElement.sortBy(sortOrder, mappedPlaylist) + ExplorerElement.sortBy(sortOrder, foldersFirst, mappedPlaylist) //find current index for ((i, e) in mappedPlaylist.withIndex()){ if (filePath == e.fullPath){ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 09daaa9..b9c94f7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -190,4 +190,6 @@ Exporting files… Moving files… Wiping files… + Folders first + Show folders at the beginning of the list diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index dd047b4..d6d6504 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -28,6 +28,13 @@ android:title="@string/settings_title_sort_order" android:icon="@drawable/icon_sort"/> + + diff --git a/build.gradle b/build.gradle index 085dea5..06ea8a4 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.2' + classpath 'com.android.tools.build:gradle:4.1.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }