From d1fd44e32eb46466bd17227312ded519c3ea9d33 Mon Sep 17 00:00:00 2001 From: lynxnb Date: Wed, 22 Feb 2023 17:47:33 +0100 Subject: [PATCH] Move preference fragment to a separate file for modularity --- .../settings/GlobalSettingsFragment.kt | 84 ++++++++++++++ .../emu/skyline/settings/SettingsActivity.kt | 106 +++++------------- 2 files changed, 115 insertions(+), 75 deletions(-) create mode 100644 app/src/main/java/emu/skyline/settings/GlobalSettingsFragment.kt diff --git a/app/src/main/java/emu/skyline/settings/GlobalSettingsFragment.kt b/app/src/main/java/emu/skyline/settings/GlobalSettingsFragment.kt new file mode 100644 index 00000000..92b43ca7 --- /dev/null +++ b/app/src/main/java/emu/skyline/settings/GlobalSettingsFragment.kt @@ -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(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("disable_frame_throttling")!! + findPreference("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("category_debug")?.isVisible = true + + if (!GpuDriverHelper.supportsForceMaxGpuClocks()) { + val forceMaxGpuClocksPref = findPreference("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("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) + } + } +} diff --git a/app/src/main/java/emu/skyline/settings/SettingsActivity.kt b/app/src/main/java/emu/skyline/settings/SettingsActivity.kt index bf870186..14a245ad 100644 --- a/app/src/main/java/emu/skyline/settings/SettingsActivity.kt +++ b/app/src/main/java/emu/skyline/settings/SettingsActivity.kt @@ -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(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("disable_frame_throttling")!! - findPreference("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("category_debug")?.isVisible = true - - - if (!GpuDriverHelper.supportsForceMaxGpuClocks()) { - val forceMaxGpuClocksPref = findPreference("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("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 */