From 417c1169f8edcb6a7ac3e01410b145a528608f57 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 25 Sep 2017 22:34:58 +0200 Subject: [PATCH] just quickly resort directories at sorting change, when possible --- .../gallery/activities/MainActivity.kt | 15 ++++++++++++--- .../asynctasks/GetDirectoriesAsynctask.kt | 14 +------------- .../gallery/dialogs/PickDirectoryDialog.kt | 12 +++++++----- .../gallery/extensions/context.kt | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 5a6075197..220d5e4b9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -19,6 +19,8 @@ import com.google.gson.Gson import com.simplemobiletools.commons.dialogs.CreateNewFolderDialog import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED +import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN import com.simplemobiletools.commons.models.Release import com.simplemobiletools.commons.views.MyScalableRecyclerView import com.simplemobiletools.gallery.BuildConfig @@ -220,7 +222,11 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private fun showSortingDialog() { ChangeSortingDialog(this, true, false) { - getDirectories() + if (config.directorySorting and SORT_BY_DATE_MODIFIED > 0 || config.directorySorting and SORT_BY_DATE_TAKEN > 0) { + getDirectories() + } else { + gotDirectories(mDirs, true) + } } } @@ -420,7 +426,9 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } } - private fun gotDirectories(dirs: ArrayList, isFromCache: Boolean) { + private fun gotDirectories(newDirs: ArrayList, isFromCache: Boolean) { + val dirs = getSortedDirectories(newDirs) + mLastMediaModified = getLastMediaModified() directories_refresh_layout.isRefreshing = false mIsGettingDirs = false @@ -429,8 +437,9 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { directories_empty_text.beVisibleIf(dirs.isEmpty() && !isFromCache) checkLastMediaChanged() - if (dirs.hashCode() == mDirs.hashCode()) + if (dirs.hashCode() == mDirs.hashCode()) { return + } mDirs = dirs diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index 9f609bb23..604822008 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -77,9 +77,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va dirs.add(directory) } - Directory.sorting = config.directorySorting - dirs.sort() - return movePinnedToFront(dirs) + return dirs } private fun groupDirectories(media: ArrayList): HashMap> { @@ -133,16 +131,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va private fun isThisOrParentExcluded(path: String, excludedPaths: MutableSet, includedPaths: MutableSet) = includedPaths.none { path.startsWith(it) } && excludedPaths.any { path.startsWith(it) } - private fun movePinnedToFront(dirs: ArrayList): ArrayList { - val foundFolders = ArrayList() - val pinnedFolders = config.pinnedFolders - - dirs.forEach { if (pinnedFolders.contains(it.path)) foundFolders.add(it) } - dirs.removeAll(foundFolders) - dirs.addAll(0, foundFolders) - return dirs - } - override fun onPostExecute(dirs: ArrayList) { super.onPostExecute(dirs) callback(dirs) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt index a52a395cd..a4e877bad 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt @@ -15,6 +15,7 @@ import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.getCachedDirectories +import com.simplemobiletools.gallery.extensions.getSortedDirectories import com.simplemobiletools.gallery.models.Directory import kotlinx.android.synthetic.main.dialog_directory_picker.view.* @@ -47,19 +48,20 @@ class PickDirectoryDialog(val activity: SimpleActivity, val sourcePath: String, } } - fun showOtherFolder() { + private fun showOtherFolder() { val showHidden = activity.config.shouldShowHidden FilePickerDialog(activity, sourcePath, false, showHidden, true) { callback(it) } } - private fun gotDirectories(directories: ArrayList) { - if (directories.hashCode() == shownDirectories.hashCode()) + private fun gotDirectories(newDirs: ArrayList) { + val dirs = activity.getSortedDirectories(newDirs) + if (dirs.hashCode() == shownDirectories.hashCode()) return - shownDirectories = directories - val adapter = DirectoryAdapter(activity, directories, null, true) { + shownDirectories = dirs + val adapter = DirectoryAdapter(activity, dirs, null, true) { if (it.path.trimEnd('/') == sourcePath) { activity.toast(R.string.source_and_destination_same) return@DirectoryAdapter diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt index 51ac6da09..e6ae6d99e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/context.kt @@ -17,6 +17,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_SIZE import com.simplemobiletools.commons.helpers.SORT_DESCENDING import com.simplemobiletools.gallery.activities.SettingsActivity import com.simplemobiletools.gallery.helpers.* +import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium import java.io.File @@ -289,3 +290,21 @@ fun Context.getLastMediaModified(): Int { } return 0 } + +fun Context.movePinnedDirectoriesToFront(dirs: ArrayList): ArrayList { + val foundFolders = ArrayList() + val pinnedFolders = config.pinnedFolders + + dirs.forEach { if (pinnedFolders.contains(it.path)) foundFolders.add(it) } + dirs.removeAll(foundFolders) + dirs.addAll(0, foundFolders) + return dirs +} + +@Suppress("UNCHECKED_CAST") +fun Context.getSortedDirectories(source: ArrayList): ArrayList { + Directory.sorting = config.directorySorting + val dirs = source.clone() as ArrayList + dirs.sort() + return movePinnedDirectoriesToFront(dirs) +}