From f7934bdaf7f27bb5e39cbd1c53a01072334517a5 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Sat, 13 May 2023 16:24:56 -0400 Subject: [PATCH] android: Reset setting on long press --- .../settings/model/AbstractSetting.kt | 1 + .../features/settings/model/BooleanSetting.kt | 2 +- .../features/settings/model/FloatSetting.kt | 2 +- .../features/settings/model/IntSetting.kt | 2 +- .../features/settings/model/StringSetting.kt | 2 +- .../settings/model/view/DateTimeSetting.kt | 1 - .../features/settings/ui/SettingsAdapter.kt | 38 ++++++++++++++++++- .../settings/ui/SettingsFragmentPresenter.kt | 3 ++ .../ui/viewholder/DateTimeViewHolder.kt | 8 ++++ .../ui/viewholder/HeaderViewHolder.kt | 5 +++ .../ui/viewholder/SettingViewHolder.kt | 5 ++- .../ui/viewholder/SingleChoiceViewHolder.kt | 7 ++++ .../ui/viewholder/SliderViewHolder.kt | 7 ++++ .../ui/viewholder/SubmenuViewHolder.kt | 5 +++ .../ui/viewholder/SwitchSettingViewHolder.kt | 7 ++++ .../app/src/main/res/values/strings.xml | 1 + 16 files changed, 89 insertions(+), 7 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/AbstractSetting.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/AbstractSetting.kt index 0ebf55ab7b..2585802092 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/AbstractSetting.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/AbstractSetting.kt @@ -8,4 +8,5 @@ interface AbstractSetting { val section: String? val isRuntimeEditable: Boolean val valueAsString: String + val defaultValue: Any } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/BooleanSetting.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/BooleanSetting.kt index 837e5b89da..9bc0d41761 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/BooleanSetting.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/BooleanSetting.kt @@ -6,7 +6,7 @@ package org.yuzu.yuzu_emu.features.settings.model enum class BooleanSetting( override val key: String, override val section: String, - val defaultValue: Boolean + override val defaultValue: Boolean ) : AbstractBooleanSetting { // No boolean settings currently exist EMPTY_SETTING("", "", false); diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/FloatSetting.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/FloatSetting.kt index 4c83e9527a..96537782ab 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/FloatSetting.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/FloatSetting.kt @@ -6,7 +6,7 @@ package org.yuzu.yuzu_emu.features.settings.model enum class FloatSetting( override val key: String, override val section: String, - val defaultValue: Float + override val defaultValue: Float ) : AbstractFloatSetting { // No float settings currently exist EMPTY_SETTING("", "", 0f); diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/IntSetting.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/IntSetting.kt index 1648880163..39f62cab7c 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/IntSetting.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/IntSetting.kt @@ -6,7 +6,7 @@ package org.yuzu.yuzu_emu.features.settings.model enum class IntSetting( override val key: String, override val section: String, - val defaultValue: Int + override val defaultValue: Int ) : AbstractIntSetting { RENDERER_USE_SPEED_LIMIT( "use_speed_limit", diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/StringSetting.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/StringSetting.kt index 3fe43de26f..c41180b77c 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/StringSetting.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/StringSetting.kt @@ -6,7 +6,7 @@ package org.yuzu.yuzu_emu.features.settings.model enum class StringSetting( override val key: String, override val section: String, - defaultValue: String + override val defaultValue: String ) : AbstractStringSetting { // No string settings currently exist EMPTY_SETTING("", "", ""); diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/DateTimeSetting.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/DateTimeSetting.kt index 3f399dec9d..f2038c5787 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/DateTimeSetting.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/DateTimeSetting.kt @@ -5,7 +5,6 @@ package org.yuzu.yuzu_emu.features.settings.model.view import org.yuzu.yuzu_emu.features.settings.model.AbstractSetting import org.yuzu.yuzu_emu.features.settings.model.AbstractStringSetting -import org.yuzu.yuzu_emu.features.settings.model.StringSetting class DateTimeSetting( val key: String? = null, diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsAdapter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsAdapter.kt index 286823562d..f0b9083ab3 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsAdapter.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsAdapter.kt @@ -13,6 +13,7 @@ import android.view.ViewGroup import android.widget.TextView import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.setFragmentResultListener import androidx.recyclerview.widget.RecyclerView import com.google.android.material.datepicker.MaterialDatePicker import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -24,6 +25,11 @@ import org.yuzu.yuzu_emu.databinding.DialogSliderBinding import org.yuzu.yuzu_emu.databinding.ListItemSettingBinding import org.yuzu.yuzu_emu.databinding.ListItemSettingSwitchBinding import org.yuzu.yuzu_emu.databinding.ListItemSettingsHeaderBinding +import org.yuzu.yuzu_emu.features.settings.model.AbstractBooleanSetting +import org.yuzu.yuzu_emu.features.settings.model.AbstractFloatSetting +import org.yuzu.yuzu_emu.features.settings.model.AbstractIntSetting +import org.yuzu.yuzu_emu.features.settings.model.AbstractSetting +import org.yuzu.yuzu_emu.features.settings.model.AbstractStringSetting import org.yuzu.yuzu_emu.features.settings.model.FloatSetting import org.yuzu.yuzu_emu.features.settings.model.view.* import org.yuzu.yuzu_emu.features.settings.ui.viewholder.* @@ -52,21 +58,27 @@ class SettingsAdapter( SettingsItem.TYPE_HEADER -> { HeaderViewHolder(ListItemSettingsHeaderBinding.inflate(inflater), this) } + SettingsItem.TYPE_SWITCH -> { SwitchSettingViewHolder(ListItemSettingSwitchBinding.inflate(inflater), this) } + SettingsItem.TYPE_SINGLE_CHOICE, SettingsItem.TYPE_STRING_SINGLE_CHOICE -> { SingleChoiceViewHolder(ListItemSettingBinding.inflate(inflater), this) } + SettingsItem.TYPE_SLIDER -> { SliderViewHolder(ListItemSettingBinding.inflate(inflater), this) } + SettingsItem.TYPE_SUBMENU -> { SubmenuViewHolder(ListItemSettingBinding.inflate(inflater), this) } + SettingsItem.TYPE_DATETIME_SETTING -> { DateTimeViewHolder(ListItemSettingBinding.inflate(inflater), this) } + else -> { // TODO: Create an error view since we can't return null now HeaderViewHolder(ListItemSettingsHeaderBinding.inflate(inflater), this) @@ -176,7 +188,10 @@ class SettingsAdapter( item.setSelectedValue(rtcString) clickedItem = null } - datePicker.show((fragmentView.activityView as AppCompatActivity).supportFragmentManager, "DatePicker") + datePicker.show( + (fragmentView.activityView as AppCompatActivity).supportFragmentManager, + "DatePicker" + ) } fun onSliderClick(item: SliderSetting, position: Int) { @@ -231,6 +246,7 @@ class SettingsAdapter( fragmentView.putSetting(setting) closeDialog() } + is StringSingleChoiceSetting -> { val scSetting = clickedItem as StringSingleChoiceSetting val value = scSetting.getValueAt(which) @@ -239,6 +255,7 @@ class SettingsAdapter( fragmentView.putSetting(setting) closeDialog() } + is SliderSetting -> { val sliderSetting = clickedItem as SliderSetting if (sliderSetting.selectedValue != sliderProgress) { @@ -259,6 +276,25 @@ class SettingsAdapter( sliderProgress = -1 } + fun onLongClick(setting: AbstractSetting, position: Int): Boolean { + MaterialAlertDialogBuilder(context) + .setMessage(R.string.reset_setting_confirmation) + .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, which: Int -> + when (setting) { + is AbstractBooleanSetting -> setting.boolean = setting.defaultValue as Boolean + is AbstractFloatSetting -> setting.float = setting.defaultValue as Float + is AbstractIntSetting -> setting.int = setting.defaultValue as Int + is AbstractStringSetting -> setting.string = setting.defaultValue as String + } + notifyItemChanged(position) + fragmentView.onSettingChanged() + } + .setNegativeButton(android.R.string.cancel, null) + .show() + + return true + } + fun closeDialog() { if (dialog != null) { if (clickedPosition != -1) { diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt index f6ab922943..9a7b605813 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt @@ -326,6 +326,7 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) override val isRuntimeEditable: Boolean = false override val valueAsString: String get() = preferences.getInt(Settings.PREF_THEME, 0).toString() + override val defaultValue: Any = 0 } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { @@ -362,6 +363,7 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) override val isRuntimeEditable: Boolean = false override val valueAsString: String get() = preferences.getInt(Settings.PREF_THEME_MODE, -1).toString() + override val defaultValue: Any = -1 } add( @@ -386,6 +388,7 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) override val isRuntimeEditable: Boolean = false override val valueAsString: String get() = preferences.getBoolean(Settings.PREF_BLACK_BACKGROUNDS, false).toString() + override val defaultValue: Any = false } add( diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/DateTimeViewHolder.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/DateTimeViewHolder.kt index e433373ed7..fb0c6c9e5a 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/DateTimeViewHolder.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/DateTimeViewHolder.kt @@ -5,6 +5,7 @@ package org.yuzu.yuzu_emu.features.settings.ui.viewholder import android.view.View import org.yuzu.yuzu_emu.databinding.ListItemSettingBinding +import org.yuzu.yuzu_emu.features.settings.model.AbstractStringSetting import org.yuzu.yuzu_emu.features.settings.model.view.DateTimeSetting import org.yuzu.yuzu_emu.features.settings.model.view.SettingsItem import org.yuzu.yuzu_emu.features.settings.ui.SettingsAdapter @@ -29,4 +30,11 @@ class DateTimeViewHolder(val binding: ListItemSettingBinding, adapter: SettingsA adapter.onDateTimeClick(setting, bindingAdapterPosition) } } + + override fun onLongClick(clicked: View): Boolean { + if (setting.isEditable) { + return adapter.onLongClick(setting.setting!!, bindingAdapterPosition) + } + return false + } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/HeaderViewHolder.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/HeaderViewHolder.kt index 1ac00dbfc9..f5bcf705c7 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/HeaderViewHolder.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/HeaderViewHolder.kt @@ -22,4 +22,9 @@ class HeaderViewHolder(val binding: ListItemSettingsHeaderBinding, adapter: Sett override fun onClick(clicked: View) { // no-op } + + override fun onLongClick(clicked: View): Boolean { + // no-op + return true + } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SettingViewHolder.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SettingViewHolder.kt index 73375e757b..f564608938 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SettingViewHolder.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SettingViewHolder.kt @@ -9,10 +9,11 @@ import org.yuzu.yuzu_emu.features.settings.model.view.SettingsItem import org.yuzu.yuzu_emu.features.settings.ui.SettingsAdapter abstract class SettingViewHolder(itemView: View, protected val adapter: SettingsAdapter) : - RecyclerView.ViewHolder(itemView), View.OnClickListener { + RecyclerView.ViewHolder(itemView), View.OnClickListener, View.OnLongClickListener { init { itemView.setOnClickListener(this) + itemView.setOnLongClickListener(this) } /** @@ -30,4 +31,6 @@ abstract class SettingViewHolder(itemView: View, protected val adapter: Settings * @param clicked The view that was clicked on. */ abstract override fun onClick(clicked: View) + + abstract override fun onLongClick(clicked: View): Boolean } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SingleChoiceViewHolder.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SingleChoiceViewHolder.kt index b92334522b..487e2e141b 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SingleChoiceViewHolder.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SingleChoiceViewHolder.kt @@ -50,4 +50,11 @@ class SingleChoiceViewHolder(val binding: ListItemSettingBinding, adapter: Setti ) } } + + override fun onLongClick(clicked: View): Boolean { + if (setting.isEditable) { + return adapter.onLongClick(setting.setting!!, bindingAdapterPosition) + } + return false + } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SliderViewHolder.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SliderViewHolder.kt index 1c2f74d568..930bdd152e 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SliderViewHolder.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SliderViewHolder.kt @@ -29,4 +29,11 @@ class SliderViewHolder(val binding: ListItemSettingBinding, adapter: SettingsAda adapter.onSliderClick(setting, bindingAdapterPosition) } } + + override fun onLongClick(clicked: View): Boolean { + if (setting.isEditable) { + return adapter.onLongClick(setting.setting!!, bindingAdapterPosition) + } + return false + } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SubmenuViewHolder.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SubmenuViewHolder.kt index 8125fd8654..8bd36e9d54 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SubmenuViewHolder.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SubmenuViewHolder.kt @@ -27,4 +27,9 @@ class SubmenuViewHolder(val binding: ListItemSettingBinding, adapter: SettingsAd override fun onClick(clicked: View) { adapter.onSubmenuClick(item) } + + override fun onLongClick(clicked: View): Boolean { + // no-op + return true + } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SwitchSettingViewHolder.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SwitchSettingViewHolder.kt index 825e9d0d09..c24992e214 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SwitchSettingViewHolder.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SwitchSettingViewHolder.kt @@ -38,4 +38,11 @@ class SwitchSettingViewHolder(val binding: ListItemSettingSwitchBinding, adapter binding.switchWidget.toggle() } } + + override fun onLongClick(clicked: View): Boolean { + if (setting.isEditable) { + return adapter.onLongClick(setting.setting!!, bindingAdapterPosition) + } + return false + } } diff --git a/src/android/app/src/main/res/values/strings.xml b/src/android/app/src/main/res/values/strings.xml index c5f4483d14..f6d7422065 100644 --- a/src/android/app/src/main/res/values/strings.xml +++ b/src/android/app/src/main/res/values/strings.xml @@ -141,6 +141,7 @@ Saved settings for %1$s Error saving %1$s.ini: %2$s Loading... + Do you want to reset this setting back to its default value? Select GPU driver