From 4de3abdd5a3b2e13af7d7bc5b82f09c06830e32a Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Sun, 19 Mar 2023 23:40:15 -0400 Subject: android: Refactor CheckBoxSetting to SwitchSetting --- .../settings/model/view/CheckBoxSetting.kt | 94 ---------------------- .../features/settings/model/view/SettingsItem.kt | 2 +- .../features/settings/model/view/SwitchSetting.kt | 94 ++++++++++++++++++++++ .../features/settings/ui/SettingsAdapter.kt | 4 +- .../settings/ui/SettingsFragmentPresenter.kt | 10 +-- .../ui/viewholder/CheckBoxSettingViewHolder.kt | 47 ----------- .../ui/viewholder/SwitchSettingViewHolder.kt | 47 +++++++++++ 7 files changed, 149 insertions(+), 149 deletions(-) delete mode 100644 src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/CheckBoxSetting.kt create mode 100644 src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SwitchSetting.kt delete mode 100644 src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/CheckBoxSettingViewHolder.kt create mode 100644 src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SwitchSettingViewHolder.kt (limited to 'src') diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/CheckBoxSetting.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/CheckBoxSetting.kt deleted file mode 100644 index c6c6f6f89..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/CheckBoxSetting.kt +++ /dev/null @@ -1,94 +0,0 @@ -// SPDX-FileCopyrightText: 2023 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -package org.yuzu.yuzu_emu.features.settings.model.view - -import org.yuzu.yuzu_emu.R -import org.yuzu.yuzu_emu.YuzuApplication -import org.yuzu.yuzu_emu.features.settings.model.BooleanSetting -import org.yuzu.yuzu_emu.features.settings.model.IntSetting -import org.yuzu.yuzu_emu.features.settings.model.Setting -import org.yuzu.yuzu_emu.features.settings.ui.SettingsFragmentView - -class CheckBoxSetting : SettingsItem { - override val type = TYPE_CHECKBOX - - private var defaultValue: Boolean - private var showPerformanceWarning: Boolean - private var fragmentView: SettingsFragmentView? = null - - constructor( - key: String, - section: String, - setting: Setting?, - titleId: Int, - descriptionId: Int, - defaultValue: Boolean - ) : super(key, section, setting, titleId, descriptionId) { - this.defaultValue = defaultValue - showPerformanceWarning = false - } - - constructor( - key: String, - section: String, - titleId: Int, - descriptionId: Int, - defaultValue: Boolean, - setting: Setting, - show_performance_warning: Boolean, - view: SettingsFragmentView - ) : super(key, section, setting, titleId, descriptionId) { - this.defaultValue = defaultValue - fragmentView = view - showPerformanceWarning = show_performance_warning - } - - val isChecked: Boolean - get() { - if (setting == null) { - return defaultValue - } - - // Try integer setting - try { - val setting = setting as IntSetting - return setting.value == 1 - } catch (_: ClassCastException) { - } - - // Try boolean setting - try { - val setting = setting as BooleanSetting - return setting.value - } catch (_: ClassCastException) { - } - return defaultValue - } - - /** - * Write a value to the backing boolean. If that boolean was previously null, - * initializes a new one and returns it, so it can be added to the Hashmap. - * - * @param checked Pretty self explanatory. - * @return null if overwritten successfully; otherwise, a newly created BooleanSetting. - */ - fun setChecked(checked: Boolean): IntSetting? { - // Show a performance warning if the setting has been disabled - if (showPerformanceWarning && !checked) { - fragmentView!!.showToastMessage( - YuzuApplication.appContext.getString(R.string.performance_warning), true - ) - } - - return if (setting == null) { - val newSetting = IntSetting(key!!, section!!, if (checked) 1 else 0) - setting = newSetting - newSetting - } else { - val newSetting = setting as IntSetting - newSetting.value = if (checked) 1 else 0 - null - } - } -} diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SettingsItem.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SettingsItem.kt index 91cd1a558..c7ad60046 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SettingsItem.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SettingsItem.kt @@ -23,7 +23,7 @@ abstract class SettingsItem( companion object { const val TYPE_HEADER = 0 - const val TYPE_CHECKBOX = 1 + const val TYPE_SWITCH = 1 const val TYPE_SINGLE_CHOICE = 2 const val TYPE_SLIDER = 3 const val TYPE_SUBMENU = 4 diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SwitchSetting.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SwitchSetting.kt new file mode 100644 index 000000000..b793012cc --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SwitchSetting.kt @@ -0,0 +1,94 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.features.settings.model.view + +import org.yuzu.yuzu_emu.R +import org.yuzu.yuzu_emu.YuzuApplication +import org.yuzu.yuzu_emu.features.settings.model.BooleanSetting +import org.yuzu.yuzu_emu.features.settings.model.IntSetting +import org.yuzu.yuzu_emu.features.settings.model.Setting +import org.yuzu.yuzu_emu.features.settings.ui.SettingsFragmentView + +class SwitchSetting : SettingsItem { + override val type = TYPE_SWITCH + + private var defaultValue: Boolean + private var showPerformanceWarning: Boolean + private var fragmentView: SettingsFragmentView? = null + + constructor( + key: String, + section: String, + setting: Setting?, + titleId: Int, + descriptionId: Int, + defaultValue: Boolean + ) : super(key, section, setting, titleId, descriptionId) { + this.defaultValue = defaultValue + showPerformanceWarning = false + } + + constructor( + key: String, + section: String, + titleId: Int, + descriptionId: Int, + defaultValue: Boolean, + setting: Setting, + show_performance_warning: Boolean, + view: SettingsFragmentView + ) : super(key, section, setting, titleId, descriptionId) { + this.defaultValue = defaultValue + fragmentView = view + showPerformanceWarning = show_performance_warning + } + + val isChecked: Boolean + get() { + if (setting == null) { + return defaultValue + } + + // Try integer setting + try { + val setting = setting as IntSetting + return setting.value == 1 + } catch (_: ClassCastException) { + } + + // Try boolean setting + try { + val setting = setting as BooleanSetting + return setting.value + } catch (_: ClassCastException) { + } + return defaultValue + } + + /** + * Write a value to the backing boolean. If that boolean was previously null, + * initializes a new one and returns it, so it can be added to the Hashmap. + * + * @param checked Pretty self explanatory. + * @return null if overwritten successfully; otherwise, a newly created BooleanSetting. + */ + fun setChecked(checked: Boolean): IntSetting? { + // Show a performance warning if the setting has been disabled + if (showPerformanceWarning && !checked) { + fragmentView!!.showToastMessage( + YuzuApplication.appContext.getString(R.string.performance_warning), true + ) + } + + return if (setting == null) { + val newSetting = IntSetting(key!!, section!!, if (checked) 1 else 0) + setting = newSetting + newSetting + } else { + val newSetting = setting as IntSetting + newSetting.value = if (checked) 1 else 0 + 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 4eac386cc..1b8a0a5ea 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 @@ -52,7 +52,7 @@ class SettingsAdapter( } SettingsItem.TYPE_CHECKBOX -> { view = inflater.inflate(R.layout.list_item_setting_switch, parent, false) - CheckBoxSettingViewHolder(view, this) + SwitchSettingViewHolder(view, this) } SettingsItem.TYPE_SINGLE_CHOICE, SettingsItem.TYPE_STRING_SINGLE_CHOICE -> { view = inflater.inflate(R.layout.list_item_setting, parent, false) @@ -103,7 +103,7 @@ class SettingsAdapter( notifyDataSetChanged() } - fun onBooleanClick(item: CheckBoxSetting, position: Int, checked: Boolean) { + fun onBooleanClick(item: SwitchSetting, position: Int, checked: Boolean) { val setting = item.setChecked(checked) if (setting != null) { fragmentView.putSetting(setting) 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 f475b26c4..ef825ec58 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 @@ -139,7 +139,7 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) val cpuAccuracy = cpuSection!!.getSetting(SettingsFile.KEY_CPU_ACCURACY) sl.apply { add( - CheckBoxSetting( + SwitchSetting( SettingsFile.KEY_RENDERER_USE_SPEED_LIMIT, Settings.SECTION_RENDERER, frameLimitEnable, @@ -184,7 +184,7 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) val language = systemSection.getSetting(SettingsFile.KEY_LANGUAGE_INDEX) sl.apply { add( - CheckBoxSetting( + SwitchSetting( SettingsFile.KEY_USE_DOCKED_MODE, Settings.SECTION_SYSTEM, dockedMode, @@ -283,7 +283,7 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) ) ) add( - CheckBoxSetting( + SwitchSetting( SettingsFile.KEY_RENDERER_FORCE_MAX_CLOCK, Settings.SECTION_RENDERER, rendererForceMaxClocks, @@ -293,7 +293,7 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) ) ) add( - CheckBoxSetting( + SwitchSetting( SettingsFile.KEY_RENDERER_ASYNCHRONOUS_SHADERS, Settings.SECTION_RENDERER, rendererAsynchronousShaders, @@ -303,7 +303,7 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) ) ) add( - CheckBoxSetting( + SwitchSetting( SettingsFile.KEY_RENDERER_DEBUG, Settings.SECTION_RENDERER, rendererDebug, diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/CheckBoxSettingViewHolder.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/CheckBoxSettingViewHolder.kt deleted file mode 100644 index 3296aed68..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/CheckBoxSettingViewHolder.kt +++ /dev/null @@ -1,47 +0,0 @@ -// SPDX-FileCopyrightText: 2023 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -package org.yuzu.yuzu_emu.features.settings.ui.viewholder - -import android.view.View -import android.widget.CompoundButton -import android.widget.TextView -import com.google.android.material.materialswitch.MaterialSwitch -import org.yuzu.yuzu_emu.R -import org.yuzu.yuzu_emu.features.settings.model.view.CheckBoxSetting -import org.yuzu.yuzu_emu.features.settings.model.view.SettingsItem -import org.yuzu.yuzu_emu.features.settings.ui.SettingsAdapter - -class CheckBoxSettingViewHolder(itemView: View, adapter: SettingsAdapter) : - SettingViewHolder(itemView, adapter) { - private lateinit var item: CheckBoxSetting - private lateinit var textSettingName: TextView - private lateinit var textSettingDescription: TextView - private lateinit var switch: MaterialSwitch - - override fun findViews(root: View) { - textSettingName = root.findViewById(R.id.text_setting_name) - textSettingDescription = root.findViewById(R.id.text_setting_description) - switch = root.findViewById(R.id.switch_widget) - } - - override fun bind(item: SettingsItem) { - this.item = item as CheckBoxSetting - textSettingName.setText(item.nameId) - if (item.descriptionId!! > 0) { - textSettingDescription.setText(item.descriptionId) - textSettingDescription.visibility = View.VISIBLE - } else { - textSettingDescription.text = "" - textSettingDescription.visibility = View.GONE - } - switch.isChecked = this.item.isChecked - switch.setOnCheckedChangeListener { _: CompoundButton, _: Boolean -> - adapter.onBooleanClick(item, bindingAdapterPosition, switch.isChecked) - } - } - - override fun onClick(clicked: View) { - switch.toggle() - } -} 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 new file mode 100644 index 000000000..f8b229830 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/viewholder/SwitchSettingViewHolder.kt @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.features.settings.ui.viewholder + +import android.view.View +import android.widget.CompoundButton +import android.widget.TextView +import com.google.android.material.materialswitch.MaterialSwitch +import org.yuzu.yuzu_emu.R +import org.yuzu.yuzu_emu.features.settings.model.view.SwitchSetting +import org.yuzu.yuzu_emu.features.settings.model.view.SettingsItem +import org.yuzu.yuzu_emu.features.settings.ui.SettingsAdapter + +class SwitchSettingViewHolder(itemView: View, adapter: SettingsAdapter) : + SettingViewHolder(itemView, adapter) { + private lateinit var item: SwitchSetting + private lateinit var textSettingName: TextView + private lateinit var textSettingDescription: TextView + private lateinit var switch: MaterialSwitch + + override fun findViews(root: View) { + textSettingName = root.findViewById(R.id.text_setting_name) + textSettingDescription = root.findViewById(R.id.text_setting_description) + switch = root.findViewById(R.id.switch_widget) + } + + override fun bind(item: SettingsItem) { + this.item = item as SwitchSetting + textSettingName.setText(item.nameId) + if (item.descriptionId!! > 0) { + textSettingDescription.setText(item.descriptionId) + textSettingDescription.visibility = View.VISIBLE + } else { + textSettingDescription.text = "" + textSettingDescription.visibility = View.GONE + } + switch.isChecked = this.item.isChecked + switch.setOnCheckedChangeListener { _: CompoundButton, _: Boolean -> + adapter.onBooleanClick(item, bindingAdapterPosition, switch.isChecked) + } + } + + override fun onClick(clicked: View) { + switch.toggle() + } +} -- cgit v1.2.3