Merge pull request #10622 from t895/load-settings

android: Load settings at the start of each activity
This commit is contained in:
bunnei 2023-06-04 19:14:38 -07:00 committed by GitHub
commit 78319435e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 6 deletions

View file

@ -21,6 +21,7 @@ import android.view.MotionEvent
import android.view.Surface import android.view.Surface
import android.view.View import android.view.View
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
@ -34,6 +35,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.NativeLibrary
import org.yuzu.yuzu_emu.R import org.yuzu.yuzu_emu.R
import org.yuzu.yuzu_emu.features.settings.model.SettingsViewModel
import org.yuzu.yuzu_emu.fragments.EmulationFragment import org.yuzu.yuzu_emu.fragments.EmulationFragment
import org.yuzu.yuzu_emu.model.Game import org.yuzu.yuzu_emu.model.Game
import org.yuzu.yuzu_emu.utils.ControllerMappingHelper import org.yuzu.yuzu_emu.utils.ControllerMappingHelper
@ -60,6 +62,8 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener {
private lateinit var game: Game private lateinit var game: Game
private val settingsViewModel: SettingsViewModel by viewModels()
override fun onDestroy() { override fun onDestroy() {
stopForegroundService(this) stopForegroundService(this)
super.onDestroy() super.onDestroy()
@ -68,6 +72,8 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
ThemeHelper.setTheme(this) ThemeHelper.setTheme(this)
settingsViewModel.settings.loadSettings()
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
if (savedInstanceState == null) { if (savedInstanceState == null) {
// Get params we were passed // Get params we were passed

View file

@ -39,7 +39,7 @@ class Settings {
val isEmpty: Boolean val isEmpty: Boolean
get() = sections.isEmpty() get() = sections.isEmpty()
fun loadSettings(view: SettingsActivityView) { fun loadSettings(view: SettingsActivityView? = null) {
sections = SettingsSectionMap() sections = SettingsSectionMap()
loadYuzuSettings(view) loadYuzuSettings(view)
if (!TextUtils.isEmpty(gameId)) { if (!TextUtils.isEmpty(gameId)) {
@ -48,13 +48,13 @@ class Settings {
isLoaded = true isLoaded = true
} }
private fun loadYuzuSettings(view: SettingsActivityView) { private fun loadYuzuSettings(view: SettingsActivityView?) {
for ((fileName) in configFileSectionsMap) { for ((fileName) in configFileSectionsMap) {
sections.putAll(SettingsFile.readFile(fileName, view)) sections.putAll(SettingsFile.readFile(fileName, view))
} }
} }
private fun loadCustomGameSettings(gameId: String, view: SettingsActivityView) { private fun loadCustomGameSettings(gameId: String, view: SettingsActivityView?) {
// Custom game settings // Custom game settings
mergeSections(SettingsFile.readCustomGameSettings(gameId, view)) mergeSections(SettingsFile.readCustomGameSettings(gameId, view))
} }

View file

@ -37,7 +37,7 @@ object SettingsFile {
private fun readFile( private fun readFile(
ini: File?, ini: File?,
isCustomGame: Boolean, isCustomGame: Boolean,
view: SettingsActivityView? view: SettingsActivityView? = null
): HashMap<String, SettingSection?> { ): HashMap<String, SettingSection?> {
val sections: HashMap<String, SettingSection?> = SettingsSectionMap() val sections: HashMap<String, SettingSection?> = SettingsSectionMap()
var reader: BufferedReader? = null var reader: BufferedReader? = null
@ -74,10 +74,13 @@ object SettingsFile {
return sections return sections
} }
fun readFile(fileName: String, view: SettingsActivityView): HashMap<String, SettingSection?> { fun readFile(fileName: String, view: SettingsActivityView?): HashMap<String, SettingSection?> {
return readFile(getSettingsFile(fileName), false, view) return readFile(getSettingsFile(fileName), false, view)
} }
fun readFile(fileName: String): HashMap<String, SettingSection?> =
readFile(getSettingsFile(fileName), false)
/** /**
* Reads a given .ini file from disk and returns it as a HashMap of SettingSections, themselves * Reads a given .ini file from disk and returns it as a HashMap of SettingSections, themselves
* effectively a HashMap of key/value settings. If unsuccessful, outputs an error telling why it * effectively a HashMap of key/value settings. If unsuccessful, outputs an error telling why it
@ -88,7 +91,7 @@ object SettingsFile {
*/ */
fun readCustomGameSettings( fun readCustomGameSettings(
gameId: String, gameId: String,
view: SettingsActivityView view: SettingsActivityView?
): HashMap<String, SettingSection?> { ): HashMap<String, SettingSection?> {
return readFile(getCustomGameSettingsFile(gameId), true, view) return readFile(getCustomGameSettingsFile(gameId), true, view)
} }

View file

@ -35,6 +35,7 @@ import org.yuzu.yuzu_emu.activities.EmulationActivity
import org.yuzu.yuzu_emu.databinding.ActivityMainBinding import org.yuzu.yuzu_emu.databinding.ActivityMainBinding
import org.yuzu.yuzu_emu.databinding.DialogProgressBarBinding import org.yuzu.yuzu_emu.databinding.DialogProgressBarBinding
import org.yuzu.yuzu_emu.features.settings.model.Settings import org.yuzu.yuzu_emu.features.settings.model.Settings
import org.yuzu.yuzu_emu.features.settings.model.SettingsViewModel
import org.yuzu.yuzu_emu.features.settings.ui.SettingsActivity import org.yuzu.yuzu_emu.features.settings.ui.SettingsActivity
import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile
import org.yuzu.yuzu_emu.fragments.MessageDialogFragment import org.yuzu.yuzu_emu.fragments.MessageDialogFragment
@ -48,6 +49,7 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
private val homeViewModel: HomeViewModel by viewModels() private val homeViewModel: HomeViewModel by viewModels()
private val gamesViewModel: GamesViewModel by viewModels() private val gamesViewModel: GamesViewModel by viewModels()
private val settingsViewModel: SettingsViewModel by viewModels()
override var themeId: Int = 0 override var themeId: Int = 0
@ -55,6 +57,8 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
val splashScreen = installSplashScreen() val splashScreen = installSplashScreen()
splashScreen.setKeepOnScreenCondition { !DirectoryInitialization.areDirectoriesReady } splashScreen.setKeepOnScreenCondition { !DirectoryInitialization.areDirectoriesReady }
settingsViewModel.settings.loadSettings()
ThemeHelper.setTheme(this) ThemeHelper.setTheme(this)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)