android: Reset setting on long press

This commit is contained in:
Charles Lombardo 2023-05-13 16:24:56 -04:00 committed by bunnei
parent 2289f7ad15
commit f7934bdaf7
16 changed files with 89 additions and 7 deletions

View File

@ -8,4 +8,5 @@ interface AbstractSetting {
val section: String? val section: String?
val isRuntimeEditable: Boolean val isRuntimeEditable: Boolean
val valueAsString: String val valueAsString: String
val defaultValue: Any
} }

View File

@ -6,7 +6,7 @@ package org.yuzu.yuzu_emu.features.settings.model
enum class BooleanSetting( enum class BooleanSetting(
override val key: String, override val key: String,
override val section: String, override val section: String,
val defaultValue: Boolean override val defaultValue: Boolean
) : AbstractBooleanSetting { ) : AbstractBooleanSetting {
// No boolean settings currently exist // No boolean settings currently exist
EMPTY_SETTING("", "", false); EMPTY_SETTING("", "", false);

View File

@ -6,7 +6,7 @@ package org.yuzu.yuzu_emu.features.settings.model
enum class FloatSetting( enum class FloatSetting(
override val key: String, override val key: String,
override val section: String, override val section: String,
val defaultValue: Float override val defaultValue: Float
) : AbstractFloatSetting { ) : AbstractFloatSetting {
// No float settings currently exist // No float settings currently exist
EMPTY_SETTING("", "", 0f); EMPTY_SETTING("", "", 0f);

View File

@ -6,7 +6,7 @@ package org.yuzu.yuzu_emu.features.settings.model
enum class IntSetting( enum class IntSetting(
override val key: String, override val key: String,
override val section: String, override val section: String,
val defaultValue: Int override val defaultValue: Int
) : AbstractIntSetting { ) : AbstractIntSetting {
RENDERER_USE_SPEED_LIMIT( RENDERER_USE_SPEED_LIMIT(
"use_speed_limit", "use_speed_limit",

View File

@ -6,7 +6,7 @@ package org.yuzu.yuzu_emu.features.settings.model
enum class StringSetting( enum class StringSetting(
override val key: String, override val key: String,
override val section: String, override val section: String,
defaultValue: String override val defaultValue: String
) : AbstractStringSetting { ) : AbstractStringSetting {
// No string settings currently exist // No string settings currently exist
EMPTY_SETTING("", "", ""); EMPTY_SETTING("", "", "");

View File

@ -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.AbstractSetting
import org.yuzu.yuzu_emu.features.settings.model.AbstractStringSetting import org.yuzu.yuzu_emu.features.settings.model.AbstractStringSetting
import org.yuzu.yuzu_emu.features.settings.model.StringSetting
class DateTimeSetting( class DateTimeSetting(
val key: String? = null, val key: String? = null,

View File

@ -13,6 +13,7 @@ import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.setFragmentResultListener
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.datepicker.MaterialDatePicker import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.dialog.MaterialAlertDialogBuilder 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.ListItemSettingBinding
import org.yuzu.yuzu_emu.databinding.ListItemSettingSwitchBinding import org.yuzu.yuzu_emu.databinding.ListItemSettingSwitchBinding
import org.yuzu.yuzu_emu.databinding.ListItemSettingsHeaderBinding 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.FloatSetting
import org.yuzu.yuzu_emu.features.settings.model.view.* import org.yuzu.yuzu_emu.features.settings.model.view.*
import org.yuzu.yuzu_emu.features.settings.ui.viewholder.* import org.yuzu.yuzu_emu.features.settings.ui.viewholder.*
@ -52,21 +58,27 @@ class SettingsAdapter(
SettingsItem.TYPE_HEADER -> { SettingsItem.TYPE_HEADER -> {
HeaderViewHolder(ListItemSettingsHeaderBinding.inflate(inflater), this) HeaderViewHolder(ListItemSettingsHeaderBinding.inflate(inflater), this)
} }
SettingsItem.TYPE_SWITCH -> { SettingsItem.TYPE_SWITCH -> {
SwitchSettingViewHolder(ListItemSettingSwitchBinding.inflate(inflater), this) SwitchSettingViewHolder(ListItemSettingSwitchBinding.inflate(inflater), this)
} }
SettingsItem.TYPE_SINGLE_CHOICE, SettingsItem.TYPE_STRING_SINGLE_CHOICE -> { SettingsItem.TYPE_SINGLE_CHOICE, SettingsItem.TYPE_STRING_SINGLE_CHOICE -> {
SingleChoiceViewHolder(ListItemSettingBinding.inflate(inflater), this) SingleChoiceViewHolder(ListItemSettingBinding.inflate(inflater), this)
} }
SettingsItem.TYPE_SLIDER -> { SettingsItem.TYPE_SLIDER -> {
SliderViewHolder(ListItemSettingBinding.inflate(inflater), this) SliderViewHolder(ListItemSettingBinding.inflate(inflater), this)
} }
SettingsItem.TYPE_SUBMENU -> { SettingsItem.TYPE_SUBMENU -> {
SubmenuViewHolder(ListItemSettingBinding.inflate(inflater), this) SubmenuViewHolder(ListItemSettingBinding.inflate(inflater), this)
} }
SettingsItem.TYPE_DATETIME_SETTING -> { SettingsItem.TYPE_DATETIME_SETTING -> {
DateTimeViewHolder(ListItemSettingBinding.inflate(inflater), this) DateTimeViewHolder(ListItemSettingBinding.inflate(inflater), this)
} }
else -> { else -> {
// TODO: Create an error view since we can't return null now // TODO: Create an error view since we can't return null now
HeaderViewHolder(ListItemSettingsHeaderBinding.inflate(inflater), this) HeaderViewHolder(ListItemSettingsHeaderBinding.inflate(inflater), this)
@ -176,7 +188,10 @@ class SettingsAdapter(
item.setSelectedValue(rtcString) item.setSelectedValue(rtcString)
clickedItem = null clickedItem = null
} }
datePicker.show((fragmentView.activityView as AppCompatActivity).supportFragmentManager, "DatePicker") datePicker.show(
(fragmentView.activityView as AppCompatActivity).supportFragmentManager,
"DatePicker"
)
} }
fun onSliderClick(item: SliderSetting, position: Int) { fun onSliderClick(item: SliderSetting, position: Int) {
@ -231,6 +246,7 @@ class SettingsAdapter(
fragmentView.putSetting(setting) fragmentView.putSetting(setting)
closeDialog() closeDialog()
} }
is StringSingleChoiceSetting -> { is StringSingleChoiceSetting -> {
val scSetting = clickedItem as StringSingleChoiceSetting val scSetting = clickedItem as StringSingleChoiceSetting
val value = scSetting.getValueAt(which) val value = scSetting.getValueAt(which)
@ -239,6 +255,7 @@ class SettingsAdapter(
fragmentView.putSetting(setting) fragmentView.putSetting(setting)
closeDialog() closeDialog()
} }
is SliderSetting -> { is SliderSetting -> {
val sliderSetting = clickedItem as SliderSetting val sliderSetting = clickedItem as SliderSetting
if (sliderSetting.selectedValue != sliderProgress) { if (sliderSetting.selectedValue != sliderProgress) {
@ -259,6 +276,25 @@ class SettingsAdapter(
sliderProgress = -1 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() { fun closeDialog() {
if (dialog != null) { if (dialog != null) {
if (clickedPosition != -1) { if (clickedPosition != -1) {

View File

@ -326,6 +326,7 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView)
override val isRuntimeEditable: Boolean = false override val isRuntimeEditable: Boolean = false
override val valueAsString: String override val valueAsString: String
get() = preferences.getInt(Settings.PREF_THEME, 0).toString() get() = preferences.getInt(Settings.PREF_THEME, 0).toString()
override val defaultValue: Any = 0
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { 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 isRuntimeEditable: Boolean = false
override val valueAsString: String override val valueAsString: String
get() = preferences.getInt(Settings.PREF_THEME_MODE, -1).toString() get() = preferences.getInt(Settings.PREF_THEME_MODE, -1).toString()
override val defaultValue: Any = -1
} }
add( add(
@ -386,6 +388,7 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView)
override val isRuntimeEditable: Boolean = false override val isRuntimeEditable: Boolean = false
override val valueAsString: String override val valueAsString: String
get() = preferences.getBoolean(Settings.PREF_BLACK_BACKGROUNDS, false).toString() get() = preferences.getBoolean(Settings.PREF_BLACK_BACKGROUNDS, false).toString()
override val defaultValue: Any = false
} }
add( add(

View File

@ -5,6 +5,7 @@ package org.yuzu.yuzu_emu.features.settings.ui.viewholder
import android.view.View import android.view.View
import org.yuzu.yuzu_emu.databinding.ListItemSettingBinding 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.DateTimeSetting
import org.yuzu.yuzu_emu.features.settings.model.view.SettingsItem import org.yuzu.yuzu_emu.features.settings.model.view.SettingsItem
import org.yuzu.yuzu_emu.features.settings.ui.SettingsAdapter import org.yuzu.yuzu_emu.features.settings.ui.SettingsAdapter
@ -29,4 +30,11 @@ class DateTimeViewHolder(val binding: ListItemSettingBinding, adapter: SettingsA
adapter.onDateTimeClick(setting, bindingAdapterPosition) adapter.onDateTimeClick(setting, bindingAdapterPosition)
} }
} }
override fun onLongClick(clicked: View): Boolean {
if (setting.isEditable) {
return adapter.onLongClick(setting.setting!!, bindingAdapterPosition)
}
return false
}
} }

View File

@ -22,4 +22,9 @@ class HeaderViewHolder(val binding: ListItemSettingsHeaderBinding, adapter: Sett
override fun onClick(clicked: View) { override fun onClick(clicked: View) {
// no-op // no-op
} }
override fun onLongClick(clicked: View): Boolean {
// no-op
return true
}
} }

View File

@ -9,10 +9,11 @@ import org.yuzu.yuzu_emu.features.settings.model.view.SettingsItem
import org.yuzu.yuzu_emu.features.settings.ui.SettingsAdapter import org.yuzu.yuzu_emu.features.settings.ui.SettingsAdapter
abstract class SettingViewHolder(itemView: View, protected val adapter: SettingsAdapter) : abstract class SettingViewHolder(itemView: View, protected val adapter: SettingsAdapter) :
RecyclerView.ViewHolder(itemView), View.OnClickListener { RecyclerView.ViewHolder(itemView), View.OnClickListener, View.OnLongClickListener {
init { init {
itemView.setOnClickListener(this) 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. * @param clicked The view that was clicked on.
*/ */
abstract override fun onClick(clicked: View) abstract override fun onClick(clicked: View)
abstract override fun onLongClick(clicked: View): Boolean
} }

View File

@ -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
}
} }

View File

@ -29,4 +29,11 @@ class SliderViewHolder(val binding: ListItemSettingBinding, adapter: SettingsAda
adapter.onSliderClick(setting, bindingAdapterPosition) adapter.onSliderClick(setting, bindingAdapterPosition)
} }
} }
override fun onLongClick(clicked: View): Boolean {
if (setting.isEditable) {
return adapter.onLongClick(setting.setting!!, bindingAdapterPosition)
}
return false
}
} }

View File

@ -27,4 +27,9 @@ class SubmenuViewHolder(val binding: ListItemSettingBinding, adapter: SettingsAd
override fun onClick(clicked: View) { override fun onClick(clicked: View) {
adapter.onSubmenuClick(item) adapter.onSubmenuClick(item)
} }
override fun onLongClick(clicked: View): Boolean {
// no-op
return true
}
} }

View File

@ -38,4 +38,11 @@ class SwitchSettingViewHolder(val binding: ListItemSettingSwitchBinding, adapter
binding.switchWidget.toggle() binding.switchWidget.toggle()
} }
} }
override fun onLongClick(clicked: View): Boolean {
if (setting.isEditable) {
return adapter.onLongClick(setting.setting!!, bindingAdapterPosition)
}
return false
}
} }

View File

@ -141,6 +141,7 @@
<string name="gameid_saved">Saved settings for %1$s</string> <string name="gameid_saved">Saved settings for %1$s</string>
<string name="error_saving">Error saving %1$s.ini: %2$s</string> <string name="error_saving">Error saving %1$s.ini: %2$s</string>
<string name="loading">Loading...</string> <string name="loading">Loading...</string>
<string name="reset_setting_confirmation">Do you want to reset this setting back to its default value?</string>
<!-- GPU driver installation --> <!-- GPU driver installation -->
<string name="select_gpu_driver">Select GPU driver</string> <string name="select_gpu_driver">Select GPU driver</string>