From 7dddf5cb3cbc09ce32c108d2fb1343ce43d7641c Mon Sep 17 00:00:00 2001 From: t895 Date: Mon, 27 Nov 2023 15:04:03 -0500 Subject: [PATCH] android: Save global settings in onStop --- .../features/settings/model/Settings.kt | 24 --------------- .../features/settings/ui/SettingsActivity.kt | 30 +++---------------- .../features/settings/ui/SettingsAdapter.kt | 2 -- .../fragments/SettingsDialogFragment.kt | 8 ----- .../yuzu/yuzu_emu/model/SettingsViewModel.kt | 3 -- .../org/yuzu/yuzu_emu/ui/main/MainActivity.kt | 7 +++++ 6 files changed, 11 insertions(+), 63 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt index d005c656e0..e3cd661859 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/Settings.kt @@ -3,33 +3,9 @@ package org.yuzu.yuzu_emu.features.settings.model -import android.text.TextUtils -import android.widget.Toast import org.yuzu.yuzu_emu.R -import org.yuzu.yuzu_emu.YuzuApplication -import org.yuzu.yuzu_emu.utils.NativeConfig object Settings { - private val context get() = YuzuApplication.appContext - - fun saveSettings(gameId: String = "") { - if (TextUtils.isEmpty(gameId)) { - Toast.makeText( - context, - context.getString(R.string.ini_saved), - Toast.LENGTH_SHORT - ).show() - NativeConfig.saveSettings() - } else { - // TODO: Save custom game settings - Toast.makeText( - context, - context.getString(R.string.gameid_saved, gameId), - Toast.LENGTH_SHORT - ).show() - } - } - enum class Category { Android, Audio, diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.kt index 48bdbdd75f..64bfc6dd04 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivity.kt @@ -19,12 +19,13 @@ import androidx.lifecycle.repeatOnLifecycle import androidx.navigation.fragment.NavHostFragment import androidx.navigation.navArgs import com.google.android.material.color.MaterialColors +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch import java.io.IOException import org.yuzu.yuzu_emu.R import org.yuzu.yuzu_emu.databinding.ActivitySettingsBinding -import org.yuzu.yuzu_emu.features.settings.model.Settings import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile import org.yuzu.yuzu_emu.fragments.ResetSettingsDialogFragment import org.yuzu.yuzu_emu.model.SettingsViewModel @@ -53,10 +54,6 @@ class SettingsActivity : AppCompatActivity() { WindowCompat.setDecorFitsSystemWindows(window, false) - if (savedInstanceState != null) { - settingsViewModel.shouldSave = savedInstanceState.getBoolean(KEY_SHOULD_SAVE) - } - if (InsetsHelper.getSystemGestureType(applicationContext) != InsetsHelper.GESTURE_NAVIGATION ) { @@ -127,12 +124,6 @@ class SettingsActivity : AppCompatActivity() { } } - override fun onSaveInstanceState(outState: Bundle) { - // Critical: If super method is not called, rotations will be busted. - super.onSaveInstanceState(outState) - outState.putBoolean(KEY_SHOULD_SAVE, settingsViewModel.shouldSave) - } - override fun onStart() { super.onStart() // TODO: Load custom settings contextually @@ -141,16 +132,10 @@ class SettingsActivity : AppCompatActivity() { } } - /** - * If this is called, the user has left the settings screen (potentially through the - * home button) and will expect their changes to be persisted. So we kick off an - * IntentService which will do so on a background thread. - */ override fun onStop() { super.onStop() - if (isFinishing && settingsViewModel.shouldSave) { - Log.debug("[SettingsActivity] Settings activity stopping. Saving settings to INI...") - Settings.saveSettings() + CoroutineScope(Dispatchers.IO).launch { + NativeConfig.saveSettings() } } @@ -160,9 +145,6 @@ class SettingsActivity : AppCompatActivity() { } fun onSettingsReset() { - // Prevents saving to a non-existent settings file - settingsViewModel.shouldSave = false - // Delete settings file because the user may have changed values that do not exist in the UI NativeConfig.unloadConfig() val settingsFile = SettingsFile.getSettingsFile(SettingsFile.FILE_NAME_CONFIG) @@ -194,8 +176,4 @@ class SettingsActivity : AppCompatActivity() { windowInsets } } - - companion object { - private const val KEY_SHOULD_SAVE = "should_save" - } } 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 a7a029fc12..af2c1e5820 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 @@ -105,7 +105,6 @@ class SettingsAdapter( fun onBooleanClick(item: SwitchSetting, checked: Boolean) { item.checked = checked settingsViewModel.setShouldReloadSettingsList(true) - settingsViewModel.shouldSave = true } fun onSingleChoiceClick(item: SingleChoiceSetting, position: Int) { @@ -161,7 +160,6 @@ class SettingsAdapter( epochTime += timePicker.hour.toLong() * 60 * 60 epochTime += timePicker.minute.toLong() * 60 if (item.value != epochTime) { - settingsViewModel.shouldSave = true notifyItemChanged(position) item.value = epochTime } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SettingsDialogFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SettingsDialogFragment.kt index d18ec69749..b88d2c0381 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SettingsDialogFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/SettingsDialogFragment.kt @@ -52,7 +52,6 @@ class SettingsDialogFragment : DialogFragment(), DialogInterface.OnClickListener .setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int -> settingsViewModel.clickedItem!!.setting.reset() settingsViewModel.setAdapterItemChanged(position) - settingsViewModel.shouldSave = true } .setNegativeButton(android.R.string.cancel, null) .create() @@ -137,24 +136,17 @@ class SettingsDialogFragment : DialogFragment(), DialogInterface.OnClickListener is SingleChoiceSetting -> { val scSetting = settingsViewModel.clickedItem as SingleChoiceSetting val value = getValueForSingleChoiceSelection(scSetting, which) - if (scSetting.selectedValue != value) { - settingsViewModel.shouldSave = true - } scSetting.selectedValue = value } is StringSingleChoiceSetting -> { val scSetting = settingsViewModel.clickedItem as StringSingleChoiceSetting val value = scSetting.getValueAt(which) - if (scSetting.selectedValue != value) settingsViewModel.shouldSave = true scSetting.selectedValue = value } is SliderSetting -> { val sliderSetting = settingsViewModel.clickedItem as SliderSetting - if (sliderSetting.selectedValue != settingsViewModel.sliderProgress.value) { - settingsViewModel.shouldSave = true - } sliderSetting.selectedValue = settingsViewModel.sliderProgress.value } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/SettingsViewModel.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/SettingsViewModel.kt index 6f947674e7..ccc981e950 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/SettingsViewModel.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/SettingsViewModel.kt @@ -13,8 +13,6 @@ import org.yuzu.yuzu_emu.features.settings.model.view.SettingsItem class SettingsViewModel : ViewModel() { var game: Game? = null - var shouldSave = false - var clickedItem: SettingsItem? = null val shouldRecreate: StateFlow get() = _shouldRecreate @@ -73,6 +71,5 @@ class SettingsViewModel : ViewModel() { fun clear() { game = null - shouldSave = false } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt index 745901e197..16323a316a 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt @@ -253,6 +253,13 @@ class MainActivity : AppCompatActivity(), ThemeProvider { super.onResume() } + override fun onStop() { + super.onStop() + CoroutineScope(Dispatchers.IO).launch { + NativeConfig.saveSettings() + } + } + override fun onDestroy() { EmulationActivity.stopForegroundService(this) super.onDestroy()