android: Store settings object in viewmodel

This commit is contained in:
Charles Lombardo 2023-03-22 22:40:31 -04:00 committed by bunnei
parent 06e58cf088
commit aaefe8a0e0
7 changed files with 45 additions and 57 deletions

View File

@ -0,0 +1,7 @@
package org.yuzu.yuzu_emu.features.settings.model
import androidx.lifecycle.ViewModel
class SettingsViewModel : ViewModel() {
var settings = Settings()
}

View File

@ -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() {

View File

@ -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 {
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()
}

View File

@ -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.

View File

@ -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<SettingsItem>) {
settingsAdapter!!.setSettings(settingsList)
}

View File

@ -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 {

View File

@ -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.