just quickly resort directories at sorting change, when possible

This commit is contained in:
tibbi 2017-09-25 22:34:58 +02:00
parent 0da84f2958
commit 417c1169f8
4 changed files with 39 additions and 21 deletions

View file

@ -19,6 +19,8 @@ import com.google.gson.Gson
import com.simplemobiletools.commons.dialogs.CreateNewFolderDialog import com.simplemobiletools.commons.dialogs.CreateNewFolderDialog
import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.* 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.models.Release
import com.simplemobiletools.commons.views.MyScalableRecyclerView import com.simplemobiletools.commons.views.MyScalableRecyclerView
import com.simplemobiletools.gallery.BuildConfig import com.simplemobiletools.gallery.BuildConfig
@ -220,7 +222,11 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
private fun showSortingDialog() { private fun showSortingDialog() {
ChangeSortingDialog(this, true, false) { 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<Directory>, isFromCache: Boolean) { private fun gotDirectories(newDirs: ArrayList<Directory>, isFromCache: Boolean) {
val dirs = getSortedDirectories(newDirs)
mLastMediaModified = getLastMediaModified() mLastMediaModified = getLastMediaModified()
directories_refresh_layout.isRefreshing = false directories_refresh_layout.isRefreshing = false
mIsGettingDirs = false mIsGettingDirs = false
@ -429,8 +437,9 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
directories_empty_text.beVisibleIf(dirs.isEmpty() && !isFromCache) directories_empty_text.beVisibleIf(dirs.isEmpty() && !isFromCache)
checkLastMediaChanged() checkLastMediaChanged()
if (dirs.hashCode() == mDirs.hashCode()) if (dirs.hashCode() == mDirs.hashCode()) {
return return
}
mDirs = dirs mDirs = dirs

View file

@ -77,9 +77,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
dirs.add(directory) dirs.add(directory)
} }
Directory.sorting = config.directorySorting return dirs
dirs.sort()
return movePinnedToFront(dirs)
} }
private fun groupDirectories(media: ArrayList<Medium>): HashMap<String, ArrayList<Medium>> { private fun groupDirectories(media: ArrayList<Medium>): HashMap<String, ArrayList<Medium>> {
@ -133,16 +131,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
private fun isThisOrParentExcluded(path: String, excludedPaths: MutableSet<String>, includedPaths: MutableSet<String>) = private fun isThisOrParentExcluded(path: String, excludedPaths: MutableSet<String>, includedPaths: MutableSet<String>) =
includedPaths.none { path.startsWith(it) } && excludedPaths.any { path.startsWith(it) } includedPaths.none { path.startsWith(it) } && excludedPaths.any { path.startsWith(it) }
private fun movePinnedToFront(dirs: ArrayList<Directory>): ArrayList<Directory> {
val foundFolders = ArrayList<Directory>()
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<Directory>) { override fun onPostExecute(dirs: ArrayList<Directory>) {
super.onPostExecute(dirs) super.onPostExecute(dirs)
callback(dirs) callback(dirs)

View file

@ -15,6 +15,7 @@ import com.simplemobiletools.gallery.adapters.DirectoryAdapter
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getCachedDirectories import com.simplemobiletools.gallery.extensions.getCachedDirectories
import com.simplemobiletools.gallery.extensions.getSortedDirectories
import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Directory
import kotlinx.android.synthetic.main.dialog_directory_picker.view.* 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 val showHidden = activity.config.shouldShowHidden
FilePickerDialog(activity, sourcePath, false, showHidden, true) { FilePickerDialog(activity, sourcePath, false, showHidden, true) {
callback(it) callback(it)
} }
} }
private fun gotDirectories(directories: ArrayList<Directory>) { private fun gotDirectories(newDirs: ArrayList<Directory>) {
if (directories.hashCode() == shownDirectories.hashCode()) val dirs = activity.getSortedDirectories(newDirs)
if (dirs.hashCode() == shownDirectories.hashCode())
return return
shownDirectories = directories shownDirectories = dirs
val adapter = DirectoryAdapter(activity, directories, null, true) { val adapter = DirectoryAdapter(activity, dirs, null, true) {
if (it.path.trimEnd('/') == sourcePath) { if (it.path.trimEnd('/') == sourcePath) {
activity.toast(R.string.source_and_destination_same) activity.toast(R.string.source_and_destination_same)
return@DirectoryAdapter return@DirectoryAdapter

View file

@ -17,6 +17,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_SIZE
import com.simplemobiletools.commons.helpers.SORT_DESCENDING import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.gallery.activities.SettingsActivity import com.simplemobiletools.gallery.activities.SettingsActivity
import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.models.Directory
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import java.io.File import java.io.File
@ -289,3 +290,21 @@ fun Context.getLastMediaModified(): Int {
} }
return 0 return 0
} }
fun Context.movePinnedDirectoriesToFront(dirs: ArrayList<Directory>): ArrayList<Directory> {
val foundFolders = ArrayList<Directory>()
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<Directory>): ArrayList<Directory> {
Directory.sorting = config.directorySorting
val dirs = source.clone() as ArrayList<Directory>
dirs.sort()
return movePinnedDirectoriesToFront(dirs)
}