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 b29caf3e3..d9c674e9c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -33,6 +33,7 @@ import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.MediaAdapter import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask +import com.simplemobiletools.gallery.dialogs.ChangeGroupingDialog import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog import com.simplemobiletools.gallery.dialogs.ExcludeFolderDialog import com.simplemobiletools.gallery.dialogs.FilterMediaDialog @@ -418,23 +419,9 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } private fun showGroupByDialog() { - val items = arrayListOf( - RadioItem(GROUP_BY_NONE, getString(R.string.do_not_group_files)), - RadioItem(GROUP_BY_LAST_MODIFIED, getString(R.string.by_last_modified)), - RadioItem(GROUP_BY_DATE_TAKEN, getString(R.string.by_date_taken)), - RadioItem(GROUP_BY_FILE_TYPE, getString(R.string.by_file_type)), - RadioItem(GROUP_BY_EXTENSION, getString(R.string.by_extension)) - ) - - if (mShowAll) { - items.add(RadioItem(GROUP_BY_FOLDER, getString(R.string.by_folder))) - } - - RadioGroupDialog(this, items, config.groupBy) { - config.groupBy = it as Int - invalidateOptionsMenu() - media_grid.adapter = null - setupAdapter() + ChangeGroupingDialog(this, mShowAll, mPath) { + mLoadedInitialPhotos = false + getMedia() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeGroupingDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeGroupingDialog.kt new file mode 100644 index 000000000..63cecbc2f --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeGroupingDialog.kt @@ -0,0 +1,87 @@ +package com.simplemobiletools.gallery.dialogs + +import android.content.DialogInterface +import android.support.v7.app.AlertDialog +import android.view.View +import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.extensions.beVisibleIf +import com.simplemobiletools.commons.extensions.setupDialogStuff +import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.extensions.config +import com.simplemobiletools.gallery.helpers.* +import kotlinx.android.synthetic.main.dialog_change_grouping.view.* + +class ChangeGroupingDialog(val activity: BaseSimpleActivity, val isShowingAll: Boolean, val path: String = "", val callback: () -> Unit) : + DialogInterface.OnClickListener { + private var currGrouping = 0 + private var config = activity.config + private var view: View + + init { + view = activity.layoutInflater.inflate(R.layout.dialog_change_grouping, null).apply { + use_for_this_folder_divider.beVisibleIf(!isShowingAll) + grouping_dialog_use_for_this_folder.beVisibleIf(!isShowingAll) + grouping_dialog_use_for_this_folder.isChecked = config.hasCustomGrouping(path) + grouping_dialog_radio_folder.beVisibleIf(isShowingAll) + } + + AlertDialog.Builder(activity) + .setPositiveButton(R.string.ok, this) + .setNegativeButton(R.string.cancel, null) + .create().apply { + activity.setupDialogStuff(view, this, R.string.group_by) + } + + currGrouping = config.getFolderGrouping(path) + setupGroupRadio() + setupOrderRadio() + } + + private fun setupGroupRadio() { + val groupingRadio = view.grouping_dialog_radio_grouping + + val groupBtn = when { + currGrouping and GROUP_BY_NONE != 0 -> groupingRadio.grouping_dialog_radio_none + currGrouping and GROUP_BY_LAST_MODIFIED != 0 -> groupingRadio.grouping_dialog_radio_last_modified + currGrouping and GROUP_BY_DATE_TAKEN != 0 -> groupingRadio.grouping_dialog_radio_date_taken + currGrouping and GROUP_BY_FILE_TYPE != 0 -> groupingRadio.grouping_dialog_radio_file_type + currGrouping and GROUP_BY_EXTENSION != 0 -> groupingRadio.grouping_dialog_radio_extension + else -> groupingRadio.grouping_dialog_radio_folder + } + groupBtn.isChecked = true + } + + private fun setupOrderRadio() { + val orderRadio = view.grouping_dialog_radio_order + var orderBtn = orderRadio.grouping_dialog_radio_ascending + + if (currGrouping and GROUP_DESCENDING != 0) { + orderBtn = orderRadio.grouping_dialog_radio_descending + } + orderBtn.isChecked = true + } + + override fun onClick(dialog: DialogInterface, which: Int) { + val groupingRadio = view.grouping_dialog_radio_grouping + var grouping = when (groupingRadio.checkedRadioButtonId) { + R.id.grouping_dialog_radio_none -> GROUP_BY_NONE + R.id.grouping_dialog_radio_last_modified -> GROUP_BY_LAST_MODIFIED + R.id.grouping_dialog_radio_date_taken -> GROUP_BY_DATE_TAKEN + R.id.grouping_dialog_radio_file_type -> GROUP_BY_FILE_TYPE + R.id.grouping_dialog_radio_extension -> GROUP_BY_EXTENSION + else -> GROUP_BY_FOLDER + } + + if (view.grouping_dialog_radio_order.checkedRadioButtonId == R.id.grouping_dialog_radio_descending) { + grouping = grouping or GROUP_DESCENDING + } + + if (view.grouping_dialog_use_for_this_folder.isChecked) { + config.saveFolderGrouping(path, grouping) + } else { + config.removeFolderGrouping(path) + config.groupBy = grouping + } + callback() + } +} 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 cf291465c..40b22e446 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt @@ -36,6 +36,28 @@ class Config(context: Context) : BaseConfig(context) { fun hasCustomSorting(path: String) = prefs.contains(SORT_FOLDER_PREFIX + path.toLowerCase()) + fun saveFolderGrouping(path: String, value: Int) { + if (path.isEmpty()) { + groupBy = value + } else { + prefs.edit().putInt(GROUP_FOLDER_PREFIX + path.toLowerCase(), value).apply() + } + } + + fun getFolderGrouping(path: String): Int { + var groupBy = prefs.getInt(GROUP_FOLDER_PREFIX + path.toLowerCase(), groupBy) + if (path.isNotEmpty() && groupBy and GROUP_BY_FOLDER != 0) { + groupBy -= GROUP_BY_FOLDER + 1 + } + return groupBy + } + + fun removeFolderGrouping(path: String) { + prefs.edit().remove(GROUP_FOLDER_PREFIX + path.toLowerCase()).apply() + } + + fun hasCustomGrouping(path: String) = prefs.contains(GROUP_FOLDER_PREFIX + path.toLowerCase()) + 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 de9e4e7ec..3e8c478b1 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 const val DIRECTORY_SORT_ORDER = "directory_sort_order" const val SORT_FOLDER_PREFIX = "sort_folder_" +const val GROUP_FOLDER_PREFIX = "group_folder_" const val SHOW_HIDDEN_MEDIA = "show_hidden_media" const val TEMPORARILY_SHOW_HIDDEN = "temporarily_show_hidden" const val IS_THIRD_PARTY_INTENT = "is_third_party_intent" @@ -114,9 +115,10 @@ const val LOCAITON_INTERNAL = 1 const val LOCATION_SD = 2 const val LOCATION_OTG = 3 -const val GROUP_BY_NONE = 0 -const val GROUP_BY_LAST_MODIFIED = 1 -const val GROUP_BY_DATE_TAKEN = 2 -const val GROUP_BY_FILE_TYPE = 3 -const val GROUP_BY_EXTENSION = 4 -const val GROUP_BY_FOLDER = 5 +const val GROUP_BY_NONE = 1 +const val GROUP_BY_LAST_MODIFIED = 2 +const val GROUP_BY_DATE_TAKEN = 4 +const val GROUP_BY_FILE_TYPE = 8 +const val GROUP_BY_EXTENSION = 16 +const val GROUP_BY_FOLDER = 32 +const val GROUP_DESCENDING = 1024 diff --git a/app/src/main/res/layout/dialog_change_grouping.xml b/app/src/main/res/layout/dialog_change_grouping.xml new file mode 100644 index 000000000..21292ec5d --- /dev/null +++ b/app/src/main/res/layout/dialog_change_grouping.xml @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +