From aaefe8a0e0ad2b70cf6f027590421bd9e80c7333 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Wed, 22 Mar 2023 22:40:31 -0400 Subject: android: Store settings object in viewmodel --- .../features/settings/model/SettingsViewModel.kt | 7 +++++ .../features/settings/ui/SettingsActivity.kt | 34 +++++++++++++--------- .../settings/ui/SettingsActivityPresenter.kt | 20 ++++++------- .../features/settings/ui/SettingsActivityView.kt | 4 +-- .../features/settings/ui/SettingsFragment.kt | 9 +----- .../settings/ui/SettingsFragmentPresenter.kt | 17 ++--------- .../features/settings/ui/SettingsFragmentView.kt | 11 +------ 7 files changed, 45 insertions(+), 57 deletions(-) create mode 100644 src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/SettingsViewModel.kt (limited to 'src') diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/SettingsViewModel.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/SettingsViewModel.kt new file mode 100644 index 000000000..0e33a85bb --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/SettingsViewModel.kt @@ -0,0 +1,7 @@ +package org.yuzu.yuzu_emu.features.settings.model + +import androidx.lifecycle.ViewModel + +class SettingsViewModel : ViewModel() { + var settings = Settings() +} 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 fca2f2155..1683f511f 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 @@ -10,6 +10,7 @@ import android.os.Bundle import android.view.Menu import android.view.View import android.widget.Toast +import androidx.activity.viewModels import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat @@ -23,7 +24,7 @@ import org.yuzu.yuzu_emu.R import org.yuzu.yuzu_emu.databinding.ActivitySettingsBinding import org.yuzu.yuzu_emu.databinding.DialogProgressBarBinding import org.yuzu.yuzu_emu.features.settings.model.Settings -import org.yuzu.yuzu_emu.features.settings.ui.SettingsFragment.Companion.newInstance +import org.yuzu.yuzu_emu.features.settings.model.SettingsViewModel import org.yuzu.yuzu_emu.utils.* class SettingsActivity : AppCompatActivity(), SettingsActivityView { @@ -32,6 +33,14 @@ class SettingsActivity : AppCompatActivity(), SettingsActivityView { private lateinit var binding: ActivitySettingsBinding + private val settingsViewModel: SettingsViewModel by viewModels() + + override var settings: Settings + get() = settingsViewModel.settings + set(settings) { + settingsViewModel.settings = settings + } + override fun onCreate(savedInstanceState: Bundle?) { ThemeHelper.setTheme(this) @@ -93,9 +102,10 @@ class SettingsActivity : AppCompatActivity(), SettingsActivityView { } override fun showSettingsFragment(menuTag: String, addToStack: Boolean, gameId: String) { - if (!addToStack && fragment != null) { + if (!addToStack && settingsFragment != null) { return } + val transaction = supportFragmentManager.beginTransaction() if (addToStack) { if (areSystemAnimationsEnabled()) { @@ -108,7 +118,11 @@ class SettingsActivity : AppCompatActivity(), SettingsActivityView { } transaction.addToBackStack(null) } - transaction.replace(R.id.frame_content, newInstance(menuTag, gameId), FRAGMENT_TAG) + transaction.replace( + R.id.frame_content, + SettingsFragment.newInstance(menuTag, gameId), + FRAGMENT_TAG + ) transaction.commit() } @@ -165,19 +179,13 @@ class SettingsActivity : AppCompatActivity(), SettingsActivityView { ).show() } - override var settings: Settings? - get() = presenter.settings - set(settings) { - presenter.settings = settings - } - - override fun onSettingsFileLoaded(settings: Settings?) { - val fragment: SettingsFragmentView? = fragment + override fun onSettingsFileLoaded(settings: Settings) { + val fragment: SettingsFragmentView? = settingsFragment fragment?.onSettingsFileLoaded(settings) } override fun onSettingsFileNotFound() { - val fragment: SettingsFragmentView? = fragment + val fragment: SettingsFragmentView? = settingsFragment fragment?.loadDefaultSettings() } @@ -193,7 +201,7 @@ class SettingsActivity : AppCompatActivity(), SettingsActivityView { presenter.onSettingChanged() } - private val fragment: SettingsFragment? + private val settingsFragment: SettingsFragment? get() = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG) as SettingsFragment? private fun setInsets() { diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityPresenter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityPresenter.kt index fd7af5a56..60df9d5b5 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityPresenter.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityPresenter.kt @@ -16,17 +16,17 @@ import org.yuzu.yuzu_emu.utils.Log import java.io.File class SettingsActivityPresenter(private val activityView: SettingsActivityView) { - var settings: Settings? = Settings() + val settings: Settings get() = activityView.settings + private var shouldSave = false private var directoryStateReceiver: DirectoryStateReceiver? = null private lateinit var menuTag: String private lateinit var gameId: String fun onCreate(savedInstanceState: Bundle?, menuTag: String, gameId: String) { - if (savedInstanceState == null) { - this.menuTag = menuTag - this.gameId = gameId - } else { + this.menuTag = menuTag + this.gameId = gameId + if (savedInstanceState != null) { shouldSave = savedInstanceState.getBoolean(KEY_SHOULD_SAVE) } } @@ -36,11 +36,11 @@ class SettingsActivityPresenter(private val activityView: SettingsActivityView) } private fun loadSettingsUI() { - if (settings!!.isEmpty) { + if (settings.isEmpty) { if (!TextUtils.isEmpty(gameId)) { - settings!!.loadSettings(gameId, activityView) + settings.loadSettings(gameId, activityView) } else { - settings!!.loadSettings(activityView) + settings.loadSettings(activityView) } } activityView.showSettingsFragment(menuTag, false, gameId) @@ -81,9 +81,9 @@ class SettingsActivityPresenter(private val activityView: SettingsActivityView) activityView.stopListeningToDirectoryInitializationService(directoryStateReceiver!!) directoryStateReceiver = null } - if (settings != null && finishing && shouldSave) { + if (finishing && shouldSave) { Log.debug("[SettingsActivity] Settings activity stopping. Saving settings to INI...") - settings!!.saveSettings(activityView) + settings.saveSettings(activityView) } NativeLibrary.ReloadSettings() } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityView.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityView.kt index 2b0852cf6..f19ca0e30 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityView.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityView.kt @@ -26,14 +26,14 @@ interface SettingsActivityView { * * @return A possibly null HashMap of Settings. */ - var settings: Settings? + var settings: Settings /** * Called when an asynchronous load operation completes. * * @param settings The (possibly null) result of the ini load operation. */ - fun onSettingsFileLoaded(settings: Settings?) + fun onSettingsFileLoaded(settings: Settings) /** * Called when an asynchronous load operation fails. diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragment.kt index e2e07ee8b..f522a82b7 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragment.kt @@ -34,7 +34,6 @@ class SettingsFragment : Fragment(), SettingsFragmentView { super.onAttach(context) activityView = context as SettingsActivityView fragmentActivity = requireActivity() - presenter.onAttach() } override fun onCreate(savedInstanceState: Bundle?) { @@ -76,16 +75,10 @@ class SettingsFragment : Fragment(), SettingsFragmentView { } } - override fun onSettingsFileLoaded(settings: Settings?) { + override fun onSettingsFileLoaded(settings: Settings) { presenter.setSettings(settings) } - override fun passSettingsToActivity(settings: Settings) { - if (activityView != null) { - activityView!!.settings = settings - } - } - override fun showSettingsList(settingsList: ArrayList) { settingsAdapter!!.setSettings(settingsList) } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt index ef825ec58..8eaa0a0fa 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentPresenter.kt @@ -22,21 +22,10 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) this.menuTag = menuTag } - fun onViewCreated(settings: Settings?) { + fun onViewCreated(settings: Settings) { setSettings(settings) } - /** - * If the screen is rotated, the Activity will forget the settings map. This fragment - * won't, though; so rather than have the Activity reload from disk, have the fragment pass - * the settings map back to the Activity. - */ - fun onAttach() { - if (settings != null) { - fragmentView.passSettingsToActivity(settings!!) - } - } - fun putSetting(setting: Setting) { settings!!.getSection(setting.section)!!.putSetting(setting) } @@ -54,8 +43,8 @@ class SettingsFragmentPresenter(private val fragmentView: SettingsFragmentView) loadSettingsList() } - fun setSettings(settings: Settings?) { - if (settingsList == null && settings != null) { + fun setSettings(settings: Settings) { + if (settingsList == null) { this.settings = settings loadSettingsList() } else { diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentView.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentView.kt index 1b8eddfbb..2d9700fca 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentView.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsFragmentView.kt @@ -19,16 +19,7 @@ interface SettingsFragmentView { * * @param settings The (possibly null) result of the ini load operation. */ - fun onSettingsFileLoaded(settings: Settings?) - - /** - * Pass a settings HashMap to the containing activity, so that it can - * share the HashMap with other SettingsFragments; useful so that rotations - * do not require an additional load operation. - * - * @param settings An ArrayList containing all the settings HashMaps. - */ - fun passSettingsToActivity(settings: Settings) + fun onSettingsFileLoaded(settings: Settings) /** * Pass an ArrayList to the View so that it can be displayed on screen. -- cgit v1.2.3