From 940ed8a43b240987be24d4f8694d761517338832 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 28 Feb 2017 21:06:17 +0100 Subject: [PATCH] implement custom sorting per folder --- .../gallery/activities/MediaActivity.kt | 2 +- .../gallery/asynctasks/GetMediaAsynctask.kt | 2 +- .../gallery/dialogs/ChangeSortingDialog.kt | 13 ++++++++++--- .../simplemobiletools/gallery/helpers/Config.kt | 16 ++++++++++++++++ .../gallery/helpers/Constants.kt | 1 + 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index ed577ec71..acf6519f0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -145,7 +145,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } private fun showSortingDialog() { - ChangeSortingDialog(this, false, true) { + ChangeSortingDialog(this, false, true, mPath) { getMedia() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt index 50d8c8020..5621cf61c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -25,7 +25,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo override fun onPreExecute() { super.onPreExecute() showMedia = config.showMedia - fileSorting = config.fileSorting + fileSorting = config.getFileSorting(mPath) } override fun doInBackground(vararg params: Void): ArrayList { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt index 90152ae8c..fee14edbb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt @@ -12,7 +12,8 @@ import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.helpers.* import kotlinx.android.synthetic.main.dialog_change_sorting.view.* -class ChangeSortingDialog(val activity: SimpleActivity, val isDirectorySorting: Boolean, val showFolderCheckbox: Boolean, val callback: () -> Unit) : +class ChangeSortingDialog(val activity: SimpleActivity, val isDirectorySorting: Boolean, showFolderCheckbox: Boolean, + val path: String = "", val callback: () -> Unit) : DialogInterface.OnClickListener { companion object { private var currSorting = 0 @@ -26,6 +27,7 @@ class ChangeSortingDialog(val activity: SimpleActivity, val isDirectorySorting: view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_sorting, null) view.use_for_this_folder_divider.beVisibleIf(showFolderCheckbox) view.sorting_dialog_use_for_this_folder.beVisibleIf(showFolderCheckbox) + view.sorting_dialog_use_for_this_folder.isChecked = config.hasCustomSorting(path) AlertDialog.Builder(activity) .setPositiveButton(R.string.ok, this) @@ -34,7 +36,7 @@ class ChangeSortingDialog(val activity: SimpleActivity, val isDirectorySorting: activity.setupDialogStuff(view, this, R.string.sort_by) } - currSorting = if (isDirectorySorting) config.directorySorting else config.fileSorting + currSorting = if (isDirectorySorting) config.directorySorting else config.getFileSorting(path) setupSortRadio() setupOrderRadio() } @@ -78,7 +80,12 @@ class ChangeSortingDialog(val activity: SimpleActivity, val isDirectorySorting: if (isDirectorySorting) { config.directorySorting = sorting } else { - config.fileSorting = sorting + if (view.sorting_dialog_use_for_this_folder.isChecked) { + config.saveFileSorting(path, sorting) + } else { + config.removeFileSorting(path) + config.fileSorting = sorting + } } callback.invoke() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt index ca095a53a..f5941defa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt @@ -18,6 +18,22 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getInt(DIRECTORY_SORT_ORDER, SORT_BY_DATE_MODIFIED or SORT_DESCENDING) set(order) = prefs.edit().putInt(DIRECTORY_SORT_ORDER, order).apply() + fun saveFileSorting(path: String, value: Int) { + if (path.isEmpty()) { + fileSorting = value + } else { + prefs.edit().putInt(SORT_FOLDER_PREFIX + path, value).apply() + } + } + + fun getFileSorting(path: String) = prefs.getInt(SORT_FOLDER_PREFIX + path, fileSorting) + + fun removeFileSorting(path: String) { + prefs.edit().remove(SORT_FOLDER_PREFIX + path).apply() + } + + fun hasCustomSorting(path: String) = prefs.contains(SORT_FOLDER_PREFIX + path) + var wasHideFolderTooltipShown: Boolean get() = prefs.getBoolean(HIDE_FOLDER_TOOLTIP_SHOWN, false) set(wasShown) = prefs.edit().putBoolean(HIDE_FOLDER_TOOLTIP_SHOWN, wasShown).apply() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt index e97b043e8..3d93c8ca5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.gallery.helpers // shared preferences val SORT_ORDER = "sort_order" val DIRECTORY_SORT_ORDER = "directory_sort_order" +val SORT_FOLDER_PREFIX = "sort_folder_" val SHOW_HIDDEN_FOLDERS = "show_hidden_folders" val AUTOPLAY_VIDEOS = "autoplay_videos" val LOOP_VIDEOS = "loop_videos"