1
0
Fork 0
forked from suyu/suyu

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 isRuntimeEditable: Boolean
val valueAsString: String
val defaultValue: Any
}

View file

@ -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);

View file

@ -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);

View file

@ -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",

View file

@ -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("", "", "");

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

View file

@ -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) {

View file

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

View file

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

View file

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

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

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)
}
}
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) {
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()
}
}
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="error_saving">Error saving %1$s.ini: %2$s</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 -->
<string name="select_gpu_driver">Select GPU driver</string>