Move preference fragment to a separate file for modularity

This commit is contained in:
lynxnb 2023-02-22 17:47:33 +01:00 committed by Niccolò Betto
parent a683978e8c
commit d1fd44e32e
2 changed files with 115 additions and 75 deletions

View File

@ -0,0 +1,84 @@
/*
* SPDX-License-Identifier: MPL-2.0
* Copyright © 2023 Skyline Team and Contributors (https://github.com/skyline-emu/)
*/
package emu.skyline.settings
import android.os.Bundle
import android.view.View
import androidx.preference.CheckBoxPreference
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import emu.skyline.BuildConfig
import emu.skyline.R
import emu.skyline.preference.IntegerListPreference
import emu.skyline.utils.GpuDriverHelper
import emu.skyline.utils.WindowInsetsHelper
/**
* This fragment is used to display the global preferences
*/
class GlobalSettingsFragment : PreferenceFragmentCompat() {
companion object {
private const val DIALOG_FRAGMENT_TAG = "androidx.preference.PreferenceFragment.DIALOG"
}
override fun onViewCreated(view : View, savedInstanceState : Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView = view.findViewById<View>(R.id.recycler_view)
WindowInsetsHelper.setPadding(recyclerView, bottom = true)
}
/**
* This constructs the preferences from XML preference resources
*/
override fun onCreatePreferences(savedInstanceState : Bundle?, rootKey : String?) {
addPreferencesFromResource(R.xml.app_preferences)
addPreferencesFromResource(R.xml.emulation_preferences)
addPreferencesFromResource(R.xml.input_preferences)
addPreferencesFromResource(R.xml.credits_preferences)
// Uncheck `disable_frame_throttling` if `force_triple_buffering` gets disabled
val disableFrameThrottlingPref = findPreference<CheckBoxPreference>("disable_frame_throttling")!!
findPreference<CheckBoxPreference>("force_triple_buffering")?.setOnPreferenceChangeListener { _, newValue ->
if (newValue == false)
disableFrameThrottlingPref.isChecked = false
true
}
// Only show debug settings in debug builds
@Suppress("SENSELESS_COMPARISON")
if (BuildConfig.BUILD_TYPE != "release")
findPreference<Preference>("category_debug")?.isVisible = true
if (!GpuDriverHelper.supportsForceMaxGpuClocks()) {
val forceMaxGpuClocksPref = findPreference<CheckBoxPreference>("force_max_gpu_clocks")!!
forceMaxGpuClocksPref.isSelectable = false
forceMaxGpuClocksPref.isChecked = false
forceMaxGpuClocksPref.summary = context!!.getString(R.string.force_max_gpu_clocks_desc_unsupported)
}
resources.getStringArray(R.array.credits_entries).asIterable().shuffled().forEach {
findPreference<PreferenceCategory>("category_credits")?.addPreference(Preference(context!!).apply {
title = it
})
}
}
override fun onDisplayPreferenceDialog(preference : Preference) {
if (preference is IntegerListPreference) {
// Check if dialog is already showing
if (parentFragmentManager.findFragmentByTag(DIALOG_FRAGMENT_TAG) != null)
return
val dialogFragment = IntegerListPreference.IntegerListPreferenceDialogFragmentCompat.newInstance(preference.getKey())
@Suppress("DEPRECATION")
dialogFragment.setTargetFragment(this, 0) // androidx.preference.PreferenceDialogFragmentCompat depends on the target fragment being set correctly even though it's deprecated
dialogFragment.show(parentFragmentManager, DIALOG_FRAGMENT_TAG)
} else {
super.onDisplayPreferenceDialog(preference)
}
}
}

View File

@ -5,26 +5,30 @@
package emu.skyline.settings
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.*
import android.text.TextUtils
import android.view.KeyEvent
import android.view.ViewTreeObserver
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.*
import androidx.preference.*
import emu.skyline.BuildConfig
import androidx.core.view.WindowCompat
import androidx.preference.PreferenceFragmentCompat
import com.google.android.material.internal.ToolbarUtils
import emu.skyline.R
import emu.skyline.databinding.SettingsActivityBinding
import emu.skyline.preference.IntegerListPreference
import emu.skyline.utils.GpuDriverHelper
import emu.skyline.utils.WindowInsetsHelper
class SettingsActivity : AppCompatActivity() {
val binding by lazy { SettingsActivityBinding.inflate(layoutInflater) }
/**
* This is the instance of [PreferenceFragment] that is shown inside [R.id.settings]
* The instance of [PreferenceFragmentCompat] that is shown inside [R.id.settings]
*/
private val preferenceFragment = PreferenceFragment()
private val preferenceFragment by lazy {
GlobalSettingsFragment()
}
/**
* This initializes all of the elements in the activity and displays the settings fragment
@ -68,79 +72,31 @@ class SettingsActivity : AppCompatActivity() {
}
}
fun enableMarquee(textView : TextView) {
textView.ellipsize = TextUtils.TruncateAt.MARQUEE
textView.isSelected = true
textView.marqueeRepeatLimit = -1
}
// Set a temporary subtitle because the retrieval of the subtitle TextView fails if subtitle is null
supportActionBar?.subtitle = "sub"
@SuppressLint("RestrictedApi")
val toolbarTitleTextView = ToolbarUtils.getTitleTextView(binding.titlebar.toolbar)
@SuppressLint("RestrictedApi")
val toolbarSubtitleTextView = ToolbarUtils.getSubtitleTextView(binding.titlebar.toolbar)
toolbarTitleTextView?.let { enableMarquee(it) }
toolbarSubtitleTextView?.let { enableMarquee(it) }
// Reset the subtitle to null
supportActionBar?.subtitle = null
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, preferenceFragment)
.commit()
}
/**
* This fragment is used to display all of the preferences
*/
class PreferenceFragment : PreferenceFragmentCompat() {
companion object {
private const val DIALOG_FRAGMENT_TAG = "androidx.preference.PreferenceFragment.DIALOG"
}
override fun onViewCreated(view : View, savedInstanceState : Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView = view.findViewById<View>(R.id.recycler_view)
WindowInsetsHelper.setPadding(recyclerView, bottom = true)
}
/**
* This constructs the preferences from XML preference resources
*/
override fun onCreatePreferences(savedInstanceState : Bundle?, rootKey : String?) {
addPreferencesFromResource(R.xml.app_preferences)
addPreferencesFromResource(R.xml.game_preferences)
addPreferencesFromResource(R.xml.input_preferences)
addPreferencesFromResource(R.xml.credits_preferences)
// Uncheck `disable_frame_throttling` if `force_triple_buffering` gets disabled
val disableFrameThrottlingPref = findPreference<CheckBoxPreference>("disable_frame_throttling")!!
findPreference<CheckBoxPreference>("force_triple_buffering")?.setOnPreferenceChangeListener { _, newValue ->
if (newValue == false)
disableFrameThrottlingPref.isChecked = false
true
}
// Only show debug settings in debug builds
@Suppress("SENSELESS_COMPARISON")
if (BuildConfig.BUILD_TYPE != "release")
findPreference<Preference>("category_debug")?.isVisible = true
if (!GpuDriverHelper.supportsForceMaxGpuClocks()) {
val forceMaxGpuClocksPref = findPreference<CheckBoxPreference>("force_max_gpu_clocks")!!
forceMaxGpuClocksPref.isSelectable = false
forceMaxGpuClocksPref.isChecked = false
forceMaxGpuClocksPref.summary = context!!.getString(R.string.force_max_gpu_clocks_desc_unsupported)
}
resources.getStringArray(R.array.credits_entries).asIterable().shuffled().forEach {
findPreference<PreferenceCategory>("category_credits")?.addPreference(Preference(context!!).apply {
title = it
})
}
}
override fun onDisplayPreferenceDialog(preference : Preference) {
if (preference is IntegerListPreference) {
// Check if dialog is already showing
if (parentFragmentManager.findFragmentByTag(DIALOG_FRAGMENT_TAG) != null)
return
val dialogFragment = IntegerListPreference.IntegerListPreferenceDialogFragmentCompat.newInstance(preference.getKey())
@Suppress("DEPRECATION")
dialogFragment.setTargetFragment(this, 0) // androidx.preference.PreferenceDialogFragmentCompat depends on the target fragment being set correctly even though it's deprecated
dialogFragment.show(parentFragmentManager, DIALOG_FRAGMENT_TAG)
} else {
super.onDisplayPreferenceDialog(preference)
}
}
}
/**
* This handles on calling [onBackPressed] when [KeyEvent.KEYCODE_BUTTON_B] is lifted
*/