diff options
| author | 2023-11-27 15:04:03 -0500 | |
|---|---|---|
| committer | 2023-11-27 23:46:30 -0500 | |
| commit | 7dddf5cb3cbc09ce32c108d2fb1343ce43d7641c (patch) | |
| tree | 80f20a59cb66ae17fa26ddb81655c39e754364ff | |
| parent | android: Multi directory UI (diff) | |
| download | yuzu-7dddf5cb3cbc09ce32c108d2fb1343ce43d7641c.tar.gz yuzu-7dddf5cb3cbc09ce32c108d2fb1343ce43d7641c.tar.xz yuzu-7dddf5cb3cbc09ce32c108d2fb1343ce43d7641c.zip | |
android: Save global settings in onStop
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 d005c656e..e3cd66185 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 @@ | |||
| 3 | 3 | ||
| 4 | package org.yuzu.yuzu_emu.features.settings.model | 4 | package org.yuzu.yuzu_emu.features.settings.model |
| 5 | 5 | ||
| 6 | import android.text.TextUtils | ||
| 7 | import android.widget.Toast | ||
| 8 | import org.yuzu.yuzu_emu.R | 6 | import org.yuzu.yuzu_emu.R |
| 9 | import org.yuzu.yuzu_emu.YuzuApplication | ||
| 10 | import org.yuzu.yuzu_emu.utils.NativeConfig | ||
| 11 | 7 | ||
| 12 | object Settings { | 8 | object Settings { |
| 13 | private val context get() = YuzuApplication.appContext | ||
| 14 | |||
| 15 | fun saveSettings(gameId: String = "") { | ||
| 16 | if (TextUtils.isEmpty(gameId)) { | ||
| 17 | Toast.makeText( | ||
| 18 | context, | ||
| 19 | context.getString(R.string.ini_saved), | ||
| 20 | Toast.LENGTH_SHORT | ||
| 21 | ).show() | ||
| 22 | NativeConfig.saveSettings() | ||
| 23 | } else { | ||
| 24 | // TODO: Save custom game settings | ||
| 25 | Toast.makeText( | ||
| 26 | context, | ||
| 27 | context.getString(R.string.gameid_saved, gameId), | ||
| 28 | Toast.LENGTH_SHORT | ||
| 29 | ).show() | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | enum class Category { | 9 | enum class Category { |
| 34 | Android, | 10 | Android, |
| 35 | Audio, | 11 | 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 48bdbdd75..64bfc6dd0 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 | |||
| 19 | import androidx.navigation.fragment.NavHostFragment | 19 | import androidx.navigation.fragment.NavHostFragment |
| 20 | import androidx.navigation.navArgs | 20 | import androidx.navigation.navArgs |
| 21 | import com.google.android.material.color.MaterialColors | 21 | import com.google.android.material.color.MaterialColors |
| 22 | import kotlinx.coroutines.CoroutineScope | ||
| 23 | import kotlinx.coroutines.Dispatchers | ||
| 22 | import kotlinx.coroutines.flow.collectLatest | 24 | import kotlinx.coroutines.flow.collectLatest |
| 23 | import kotlinx.coroutines.launch | 25 | import kotlinx.coroutines.launch |
| 24 | import java.io.IOException | 26 | import java.io.IOException |
| 25 | import org.yuzu.yuzu_emu.R | 27 | import org.yuzu.yuzu_emu.R |
| 26 | import org.yuzu.yuzu_emu.databinding.ActivitySettingsBinding | 28 | import org.yuzu.yuzu_emu.databinding.ActivitySettingsBinding |
| 27 | import org.yuzu.yuzu_emu.features.settings.model.Settings | ||
| 28 | import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile | 29 | import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile |
| 29 | import org.yuzu.yuzu_emu.fragments.ResetSettingsDialogFragment | 30 | import org.yuzu.yuzu_emu.fragments.ResetSettingsDialogFragment |
| 30 | import org.yuzu.yuzu_emu.model.SettingsViewModel | 31 | import org.yuzu.yuzu_emu.model.SettingsViewModel |
| @@ -53,10 +54,6 @@ class SettingsActivity : AppCompatActivity() { | |||
| 53 | 54 | ||
| 54 | WindowCompat.setDecorFitsSystemWindows(window, false) | 55 | WindowCompat.setDecorFitsSystemWindows(window, false) |
| 55 | 56 | ||
| 56 | if (savedInstanceState != null) { | ||
| 57 | settingsViewModel.shouldSave = savedInstanceState.getBoolean(KEY_SHOULD_SAVE) | ||
| 58 | } | ||
| 59 | |||
| 60 | if (InsetsHelper.getSystemGestureType(applicationContext) != | 57 | if (InsetsHelper.getSystemGestureType(applicationContext) != |
| 61 | InsetsHelper.GESTURE_NAVIGATION | 58 | InsetsHelper.GESTURE_NAVIGATION |
| 62 | ) { | 59 | ) { |
| @@ -127,12 +124,6 @@ class SettingsActivity : AppCompatActivity() { | |||
| 127 | } | 124 | } |
| 128 | } | 125 | } |
| 129 | 126 | ||
| 130 | override fun onSaveInstanceState(outState: Bundle) { | ||
| 131 | // Critical: If super method is not called, rotations will be busted. | ||
| 132 | super.onSaveInstanceState(outState) | ||
| 133 | outState.putBoolean(KEY_SHOULD_SAVE, settingsViewModel.shouldSave) | ||
| 134 | } | ||
| 135 | |||
| 136 | override fun onStart() { | 127 | override fun onStart() { |
| 137 | super.onStart() | 128 | super.onStart() |
| 138 | // TODO: Load custom settings contextually | 129 | // TODO: Load custom settings contextually |
| @@ -141,16 +132,10 @@ class SettingsActivity : AppCompatActivity() { | |||
| 141 | } | 132 | } |
| 142 | } | 133 | } |
| 143 | 134 | ||
| 144 | /** | ||
| 145 | * If this is called, the user has left the settings screen (potentially through the | ||
| 146 | * home button) and will expect their changes to be persisted. So we kick off an | ||
| 147 | * IntentService which will do so on a background thread. | ||
| 148 | */ | ||
| 149 | override fun onStop() { | 135 | override fun onStop() { |
| 150 | super.onStop() | 136 | super.onStop() |
| 151 | if (isFinishing && settingsViewModel.shouldSave) { | 137 | CoroutineScope(Dispatchers.IO).launch { |
| 152 | Log.debug("[SettingsActivity] Settings activity stopping. Saving settings to INI...") | 138 | NativeConfig.saveSettings() |
| 153 | Settings.saveSettings() | ||
| 154 | } | 139 | } |
| 155 | } | 140 | } |
| 156 | 141 | ||
| @@ -160,9 +145,6 @@ class SettingsActivity : AppCompatActivity() { | |||
| 160 | } | 145 | } |
| 161 | 146 | ||
| 162 | fun onSettingsReset() { | 147 | fun onSettingsReset() { |
| 163 | // Prevents saving to a non-existent settings file | ||
| 164 | settingsViewModel.shouldSave = false | ||
| 165 | |||
| 166 | // Delete settings file because the user may have changed values that do not exist in the UI | 148 | // Delete settings file because the user may have changed values that do not exist in the UI |
| 167 | NativeConfig.unloadConfig() | 149 | NativeConfig.unloadConfig() |
| 168 | val settingsFile = SettingsFile.getSettingsFile(SettingsFile.FILE_NAME_CONFIG) | 150 | val settingsFile = SettingsFile.getSettingsFile(SettingsFile.FILE_NAME_CONFIG) |
| @@ -194,8 +176,4 @@ class SettingsActivity : AppCompatActivity() { | |||
| 194 | windowInsets | 176 | windowInsets |
| 195 | } | 177 | } |
| 196 | } | 178 | } |
| 197 | |||
| 198 | companion object { | ||
| 199 | private const val KEY_SHOULD_SAVE = "should_save" | ||
| 200 | } | ||
| 201 | } | 179 | } |
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 a7a029fc1..af2c1e582 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( | |||
| 105 | fun onBooleanClick(item: SwitchSetting, checked: Boolean) { | 105 | fun onBooleanClick(item: SwitchSetting, checked: Boolean) { |
| 106 | item.checked = checked | 106 | item.checked = checked |
| 107 | settingsViewModel.setShouldReloadSettingsList(true) | 107 | settingsViewModel.setShouldReloadSettingsList(true) |
| 108 | settingsViewModel.shouldSave = true | ||
| 109 | } | 108 | } |
| 110 | 109 | ||
| 111 | fun onSingleChoiceClick(item: SingleChoiceSetting, position: Int) { | 110 | fun onSingleChoiceClick(item: SingleChoiceSetting, position: Int) { |
| @@ -161,7 +160,6 @@ class SettingsAdapter( | |||
| 161 | epochTime += timePicker.hour.toLong() * 60 * 60 | 160 | epochTime += timePicker.hour.toLong() * 60 * 60 |
| 162 | epochTime += timePicker.minute.toLong() * 60 | 161 | epochTime += timePicker.minute.toLong() * 60 |
| 163 | if (item.value != epochTime) { | 162 | if (item.value != epochTime) { |
| 164 | settingsViewModel.shouldSave = true | ||
| 165 | notifyItemChanged(position) | 163 | notifyItemChanged(position) |
| 166 | item.value = epochTime | 164 | item.value = epochTime |
| 167 | } | 165 | } |
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 d18ec6974..b88d2c038 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 | |||
| 52 | .setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int -> | 52 | .setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int -> |
| 53 | settingsViewModel.clickedItem!!.setting.reset() | 53 | settingsViewModel.clickedItem!!.setting.reset() |
| 54 | settingsViewModel.setAdapterItemChanged(position) | 54 | settingsViewModel.setAdapterItemChanged(position) |
| 55 | settingsViewModel.shouldSave = true | ||
| 56 | } | 55 | } |
| 57 | .setNegativeButton(android.R.string.cancel, null) | 56 | .setNegativeButton(android.R.string.cancel, null) |
| 58 | .create() | 57 | .create() |
| @@ -137,24 +136,17 @@ class SettingsDialogFragment : DialogFragment(), DialogInterface.OnClickListener | |||
| 137 | is SingleChoiceSetting -> { | 136 | is SingleChoiceSetting -> { |
| 138 | val scSetting = settingsViewModel.clickedItem as SingleChoiceSetting | 137 | val scSetting = settingsViewModel.clickedItem as SingleChoiceSetting |
| 139 | val value = getValueForSingleChoiceSelection(scSetting, which) | 138 | val value = getValueForSingleChoiceSelection(scSetting, which) |
| 140 | if (scSetting.selectedValue != value) { | ||
| 141 | settingsViewModel.shouldSave = true | ||
| 142 | } | ||
| 143 | scSetting.selectedValue = value | 139 | scSetting.selectedValue = value |
| 144 | } | 140 | } |
| 145 | 141 | ||
| 146 | is StringSingleChoiceSetting -> { | 142 | is StringSingleChoiceSetting -> { |
| 147 | val scSetting = settingsViewModel.clickedItem as StringSingleChoiceSetting | 143 | val scSetting = settingsViewModel.clickedItem as StringSingleChoiceSetting |
| 148 | val value = scSetting.getValueAt(which) | 144 | val value = scSetting.getValueAt(which) |
| 149 | if (scSetting.selectedValue != value) settingsViewModel.shouldSave = true | ||
| 150 | scSetting.selectedValue = value | 145 | scSetting.selectedValue = value |
| 151 | } | 146 | } |
| 152 | 147 | ||
| 153 | is SliderSetting -> { | 148 | is SliderSetting -> { |
| 154 | val sliderSetting = settingsViewModel.clickedItem as SliderSetting | 149 | val sliderSetting = settingsViewModel.clickedItem as SliderSetting |
| 155 | if (sliderSetting.selectedValue != settingsViewModel.sliderProgress.value) { | ||
| 156 | settingsViewModel.shouldSave = true | ||
| 157 | } | ||
| 158 | sliderSetting.selectedValue = settingsViewModel.sliderProgress.value | 150 | sliderSetting.selectedValue = settingsViewModel.sliderProgress.value |
| 159 | } | 151 | } |
| 160 | } | 152 | } |
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 6f947674e..ccc981e95 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 | |||
| 13 | class SettingsViewModel : ViewModel() { | 13 | class SettingsViewModel : ViewModel() { |
| 14 | var game: Game? = null | 14 | var game: Game? = null |
| 15 | 15 | ||
| 16 | var shouldSave = false | ||
| 17 | |||
| 18 | var clickedItem: SettingsItem? = null | 16 | var clickedItem: SettingsItem? = null |
| 19 | 17 | ||
| 20 | val shouldRecreate: StateFlow<Boolean> get() = _shouldRecreate | 18 | val shouldRecreate: StateFlow<Boolean> get() = _shouldRecreate |
| @@ -73,6 +71,5 @@ class SettingsViewModel : ViewModel() { | |||
| 73 | 71 | ||
| 74 | fun clear() { | 72 | fun clear() { |
| 75 | game = null | 73 | game = null |
| 76 | shouldSave = false | ||
| 77 | } | 74 | } |
| 78 | } | 75 | } |
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 745901e19..16323a316 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 { | |||
| 253 | super.onResume() | 253 | super.onResume() |
| 254 | } | 254 | } |
| 255 | 255 | ||
| 256 | override fun onStop() { | ||
| 257 | super.onStop() | ||
| 258 | CoroutineScope(Dispatchers.IO).launch { | ||
| 259 | NativeConfig.saveSettings() | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 256 | override fun onDestroy() { | 263 | override fun onDestroy() { |
| 257 | EmulationActivity.stopForegroundService(this) | 264 | EmulationActivity.stopForegroundService(this) |
| 258 | super.onDestroy() | 265 | super.onDestroy() |