Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android: Add isEnabled setting item conditional check #595

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class DateTimeSetting(
titleId: Int,
descriptionId: Int,
val key: String? = null,
private val defaultValue: String? = null
private val defaultValue: String? = null,
override var isEnabled: Boolean = true
) : SettingsItem(setting, titleId, descriptionId) {
override val type = TYPE_DATETIME_SETTING

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ abstract class SettingsItem(
return setting?.isRuntimeEditable ?: false
}

open var isEnabled: Boolean = true

companion object {
const val TYPE_HEADER = 0
const val TYPE_SWITCH = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class SingleChoiceSetting(
val choicesId: Int,
val valuesId: Int,
val key: String? = null,
val defaultValue: Int? = null
val defaultValue: Int? = null,
override var isEnabled: Boolean = true
) : SettingsItem(setting, titleId, descriptionId) {
override val type = TYPE_SINGLE_CHOICE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class SliderSetting(
val max: Int,
val units: String,
val key: String? = null,
val defaultValue: Float? = null
val defaultValue: Float? = null,
override var isEnabled: Boolean = true
) : SettingsItem(setting, titleId, descriptionId) {
override val type = TYPE_SLIDER
val selectedFloat: Float
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class StringInputSetting(
titleId: Int,
descriptionId: Int,
val defaultValue: String,
val characterLimit: Int = 0
val characterLimit: Int = 0,
override var isEnabled: Boolean = true
) : SettingsItem(setting, titleId, descriptionId) {
override val type = TYPE_STRING_INPUT

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class StringSingleChoiceSetting(
val choices: Array<String>,
val values: Array<String>?,
val key: String? = null,
private val defaultValue: String? = null
private val defaultValue: String? = null,
override var isEnabled: Boolean = true
) : SettingsItem(setting, titleId, descriptionId) {
override val type = TYPE_STRING_SINGLE_CHOICE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class SwitchSetting(
titleId: Int,
descriptionId: Int,
val key: String? = null,
val defaultValue: Any? = null
val defaultValue: Any? = null,
override var isEnabled: Boolean = true
) : SettingsItem(setting, titleId, descriptionId) {
override val type = TYPE_SWITCH

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package org.citra.citra_emu.features.settings.ui
import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.icu.util.Calendar
import android.icu.util.TimeZone
import android.text.Editable
Expand All @@ -17,11 +16,11 @@ import android.text.TextWatcher
import android.text.format.DateFormat
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.dialog.MaterialAlertDialogBuilder
Expand Down Expand Up @@ -66,7 +65,6 @@ import org.citra.citra_emu.features.settings.ui.viewholder.SwitchSettingViewHold
import org.citra.citra_emu.fragments.MessageDialogFragment
import org.citra.citra_emu.fragments.MotionBottomSheetDialogFragment
import org.citra.citra_emu.utils.SystemSaveGame
import java.lang.IllegalStateException
import java.lang.NumberFormatException
import java.text.SimpleDateFormat
import kotlin.math.roundToInt
Expand Down Expand Up @@ -153,15 +151,71 @@ class SettingsAdapter(
return getItem(position)?.type ?: -1
}

fun setSettingsList(settings: ArrayList<SettingsItem>?) {
this.settings = settings ?: arrayListOf()
notifyDataSetChanged()
fun setSettingsList(newSettings: ArrayList<SettingsItem>?) {
if (settings == null) {
settings = newSettings ?: arrayListOf()
notifyDataSetChanged()
return
}

val oldSettings = settings
val diffResult = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
override fun getOldListSize() = oldSettings?.size ?: 0
override fun getNewListSize() = newSettings?.size ?: 0

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldSettings?.get(oldItemPosition)?.setting
val newItem = newSettings?.get(newItemPosition)?.setting
return oldItem?.key == newItem?.key
}

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldSettings?.get(oldItemPosition)
val newItem = newSettings?.get(newItemPosition)

if (oldItem == null || newItem == null || oldItem.type != newItem.type) {
return false
}

return when (oldItem.type) {
SettingsItem.TYPE_SLIDER -> {
(oldItem as SliderSetting).isEnabled == (newItem as SliderSetting).isEnabled
}
SettingsItem.TYPE_SWITCH -> {
(oldItem as SwitchSetting).isEnabled == (newItem as SwitchSetting).isEnabled
}
SettingsItem.TYPE_SINGLE_CHOICE -> {
(oldItem as SingleChoiceSetting).isEnabled == (newItem as SingleChoiceSetting).isEnabled
}
SettingsItem.TYPE_DATETIME_SETTING -> {
(oldItem as DateTimeSetting).isEnabled == (newItem as DateTimeSetting).isEnabled
}
SettingsItem.TYPE_STRING_SINGLE_CHOICE -> {
(oldItem as StringSingleChoiceSetting).isEnabled == (newItem as StringSingleChoiceSetting).isEnabled
}
SettingsItem.TYPE_STRING_INPUT -> {
(oldItem as StringInputSetting).isEnabled == (newItem as StringInputSetting).isEnabled
}
else -> {
oldItem == newItem
}
}
}
})

settings = newSettings ?: arrayListOf()
diffResult.dispatchUpdatesTo(this)
}

fun onBooleanClick(item: SwitchSetting, position: Int, checked: Boolean) {
val setting = item.setChecked(checked)
fragmentView.putSetting(setting)
fragmentView.onSettingChanged()

// If statement is required otherwise the app will crash on activity recreate ex. theme settings
if (fragmentView.activityView != null)
// Reload the settings list to update the UI
fragmentView.loadSettingsList()
}

private fun onSingleChoiceClick(item: SingleChoiceSetting) {
Expand Down Expand Up @@ -247,6 +301,7 @@ class SettingsAdapter(
notifyItemChanged(clickedPosition)
val setting = item.setSelectedValue(rtcString)
fragmentView.putSetting(setting)
fragmentView.loadSettingsList()
clickedItem = null
}
datePicker.show(
Expand Down Expand Up @@ -402,6 +457,7 @@ class SettingsAdapter(
else -> throw IllegalStateException("Unrecognized type used for SingleChoiceSetting!")
}
fragmentView?.putSetting(setting)
fragmentView.loadSettingsList()
closeDialog()
}
}
Expand All @@ -425,6 +481,7 @@ class SettingsAdapter(
}

fragmentView?.putSetting(setting)
fragmentView.loadSettingsList()
closeDialog()
}
}
Expand All @@ -447,6 +504,7 @@ class SettingsAdapter(
fragmentView?.putSetting(setting)
}
}
fragmentView.loadSettingsList()
closeDialog()
}
}
Expand All @@ -459,6 +517,7 @@ class SettingsAdapter(
}
val setting = it.setSelectedValue(textInputValue ?: "")
fragmentView?.putSetting(setting)
fragmentView.loadSettingsList()
closeDialog()
}
}
Expand Down Expand Up @@ -488,17 +547,27 @@ class SettingsAdapter(
}
notifyItemChanged(position)
fragmentView.onSettingChanged()
fragmentView.loadSettingsList()
}
.setNegativeButton(android.R.string.cancel, null)
.show()

return true
}

fun onClickDisabledSetting() {
MessageDialogFragment.newInstance(
R.string.setting_not_editable,
fun onClickDisabledSetting(isRuntimeDisabled: Boolean) {
val titleId = if (isRuntimeDisabled)
R.string.setting_not_editable
else
R.string.setting_disabled
val messageId = if (isRuntimeDisabled)
R.string.setting_not_editable_description
else
R.string.setting_disabled_description

MessageDialogFragment.newInstance(
titleId,
messageId
).show((fragmentView as SettingsFragment).childFragmentManager, MessageDialogFragment.TAG)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DateTimeViewHolder(val binding: ListItemSettingBinding, adapter: SettingsA
val dateFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
binding.textSettingValue.text = dateFormatter.format(zonedTime)

if (setting.isEditable) {
if (setting.isEditable && setting.isEnabled) {
binding.textSettingName.alpha = 1f
binding.textSettingDescription.alpha = 1f
binding.textSettingValue.alpha = 1f
Expand All @@ -59,18 +59,18 @@ class DateTimeViewHolder(val binding: ListItemSettingBinding, adapter: SettingsA
}

override fun onClick(clicked: View) {
if (setting.isEditable) {
if (setting.isEditable && setting.isEnabled) {
adapter.onDateTimeClick(setting, bindingAdapterPosition)
} else {
adapter.onClickDisabledSetting()
adapter.onClickDisabledSetting(!setting.isEditable)
}
}

override fun onLongClick(clicked: View): Boolean {
if (setting.isEditable) {
if (setting.isEditable && setting.isEnabled) {
return adapter.onLongClick(setting.setting!!, bindingAdapterPosition)
} else {
adapter.onClickDisabledSetting()
adapter.onClickDisabledSetting(!setting.isEditable)
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ class InputBindingSettingViewHolder(val binding: ListItemSettingBinding, adapter
if (setting.isEditable) {
adapter.onInputBindingClick(setting, bindingAdapterPosition)
} else {
adapter.onClickDisabledSetting()
adapter.onClickDisabledSetting(!setting.isEditable)
}
}

override fun onLongClick(clicked: View): Boolean {
if (setting.isEditable) {
adapter.onLongClick(setting.setting!!, bindingAdapterPosition)
} else {
adapter.onClickDisabledSetting()
adapter.onClickDisabledSetting(!setting.isEditable)
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class RunnableViewHolder(val binding: ListItemSettingBinding, adapter: SettingsA

override fun onClick(clicked: View) {
if (!setting.isRuntimeRunnable && EmulationActivity.isRunning()) {
adapter.onClickDisabledSetting()
adapter.onClickDisabledSetting(true)
} else {
setting.runnable.invoke()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SingleChoiceViewHolder(val binding: ListItemSettingBinding, adapter: Setti
binding.textSettingValue.visibility = View.VISIBLE
binding.textSettingValue.text = getTextSetting()

if (setting.isEditable) {
if (setting.isEditable && setting.isEnabled) {
binding.textSettingName.alpha = 1f
binding.textSettingDescription.alpha = 1f
binding.textSettingValue.alpha = 1f
Expand Down Expand Up @@ -65,8 +65,8 @@ class SingleChoiceViewHolder(val binding: ListItemSettingBinding, adapter: Setti
}

override fun onClick(clicked: View) {
if (!setting.isEditable) {
adapter.onClickDisabledSetting()
if (!setting.isEditable || !setting.isEnabled) {
adapter.onClickDisabledSetting(!setting.isEditable)
return
}

Expand All @@ -84,10 +84,10 @@ class SingleChoiceViewHolder(val binding: ListItemSettingBinding, adapter: Setti
}

override fun onLongClick(clicked: View): Boolean {
if (setting.isEditable) {
if (setting.isEditable && setting.isEnabled) {
return adapter.onLongClick(setting.setting!!, bindingAdapterPosition)
} else {
adapter.onClickDisabledSetting()
adapter.onClickDisabledSetting(!setting.isEditable)
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SliderViewHolder(val binding: ListItemSettingBinding, adapter: SettingsAda
else -> "${(setting.setting as AbstractIntSetting).int}${setting.units}"
}

if (setting.isEditable) {
if (setting.isEditable && setting.isEnabled) {
binding.textSettingName.alpha = 1f
binding.textSettingDescription.alpha = 1f
binding.textSettingValue.alpha = 1f
Expand All @@ -47,18 +47,18 @@ class SliderViewHolder(val binding: ListItemSettingBinding, adapter: SettingsAda
}

override fun onClick(clicked: View) {
if (setting.isEditable) {
if (setting.isEditable && setting.isEnabled) {
adapter.onSliderClick(setting, bindingAdapterPosition)
} else {
adapter.onClickDisabledSetting()
adapter.onClickDisabledSetting(!setting.isEditable)
}
}

override fun onLongClick(clicked: View): Boolean {
if (setting.isEditable) {
if (setting.isEditable && setting.isEnabled) {
return adapter.onLongClick(setting.setting!!, bindingAdapterPosition)
} else {
adapter.onClickDisabledSetting()
adapter.onClickDisabledSetting(!setting.isEditable)
}
return false
}
Expand Down
Loading
Loading