diff --git a/app/src/main/java/emu/skyline/preference/CopyGlobalSettingsPreference.kt b/app/src/main/java/emu/skyline/preference/CopyGlobalSettingsPreference.kt new file mode 100644 index 00000000..d8165d1a --- /dev/null +++ b/app/src/main/java/emu/skyline/preference/CopyGlobalSettingsPreference.kt @@ -0,0 +1,40 @@ +/* + * SPDX-License-Identifier: MPL-2.0 + * Copyright © 2023 Skyline Team and Contributors (https://github.com/skyline-emu/) + */ + +package emu.skyline.preference + +import android.app.Activity +import android.content.Context +import android.util.AttributeSet +import androidx.preference.Preference +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import emu.skyline.R +import emu.skyline.settings.EmulationSettings + +/** + * Copies global emulation settings into the current shared preferences, showing a dialog to confirm the action + * This preference recreates the activity to update the UI after modifying shared preferences + */ +class CopyGlobalSettingsPreference @JvmOverloads constructor(context : Context, attrs : AttributeSet? = null, defStyleAttr : Int = androidx.preference.R.attr.preferenceStyle) : Preference(context, attrs, defStyleAttr) { + init { + setOnPreferenceClickListener { + MaterialAlertDialogBuilder(context) + .setTitle(title) + .setMessage(R.string.copy_global_settings_warning) + .setPositiveButton(android.R.string.ok) { _, _ -> + // Copy global settings to the current ones + val emulationSettings = EmulationSettings.forPrefName(preferenceManager.sharedPreferencesName) + emulationSettings.copyFromGlobal() + + // Recreate the activity to update the UI after modifying shared preferences + (context as? Activity)?.recreate() + } + .setNegativeButton(android.R.string.cancel, null) + .show() + + true + } + } +} diff --git a/app/src/main/java/emu/skyline/settings/EmulationSettings.kt b/app/src/main/java/emu/skyline/settings/EmulationSettings.kt index f2e855e0..9c736607 100644 --- a/app/src/main/java/emu/skyline/settings/EmulationSettings.kt +++ b/app/src/main/java/emu/skyline/settings/EmulationSettings.kt @@ -10,6 +10,8 @@ import android.content.pm.ActivityInfo import emu.skyline.R import emu.skyline.SkylineApplication import emu.skyline.utils.sharedPreferences +import kotlin.reflect.KMutableProperty +import kotlin.reflect.full.memberProperties /** * Settings used during emulation. Use [forTitleId] to retrieve settings for a given `titleId`. @@ -56,6 +58,23 @@ class EmulationSettings private constructor(context : Context, prefName : String // Debug var validationLayer by sharedPreferences(context, false, prefName = prefName) + /** + * Copies all settings from the global settings to this instance. + * This is a no-op if the instance this is called on is the global one + */ + fun copyFromGlobal() { + if (isGlobal) + return + + for (prop in EmulationSettings::class.memberProperties) { + if (prop.name == "useCustomSettings") + continue + + if (prop is KMutableProperty<*>) + prop.setter.call(this, prop.get(global)) + } + } + companion object { const val SYSTEM_GPU_DRIVER = "system" @@ -71,6 +90,11 @@ class EmulationSettings private constructor(context : Context, prefName : String */ fun forTitleId(titleId : String) = EmulationSettings(SkylineApplication.context, prefNameForTitle(titleId)) + /** + * Returns emulation settings for the given preferences name + */ + fun forPrefName(prefName : String) = EmulationSettings(SkylineApplication.context, prefName) + /** * Returns emulation settings to be used during emulation of the given `titleId`. * Global settings are returned if custom settings are disabled for this title diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0185e106..da62452a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -57,6 +57,9 @@ Reset Custom Settings Reset custom settings to the default values Are you sure you want to reset all settings to the default values? Current settings will be lost + Copy Global Settings + Copy global settings to this game + Are you sure you want to copy global settings to this game? Current settings will be lost System Use Docked Mode diff --git a/app/src/main/res/xml/custom_game_preferences.xml b/app/src/main/res/xml/custom_game_preferences.xml index c64b7b69..3f002b59 100644 --- a/app/src/main/res/xml/custom_game_preferences.xml +++ b/app/src/main/res/xml/custom_game_preferences.xml @@ -13,5 +13,9 @@ android:summary="@string/reset_custom_settings_desc" app:key="reset_custom_settings" app:title="@string/reset_custom_settings" /> +