diff options
| author | 2023-03-07 21:14:06 -0500 | |
|---|---|---|
| committer | 2023-06-03 00:05:37 -0700 | |
| commit | 34ce4877bd69f9bdb7dc8f370a5f94afb2ed7da1 (patch) | |
| tree | 1faaaba70106f9a92fd300250a2cef12ffbdcd9b /src/android/app | |
| parent | android: Convert SettingsActivity to Kotlin (diff) | |
| download | yuzu-34ce4877bd69f9bdb7dc8f370a5f94afb2ed7da1.tar.gz yuzu-34ce4877bd69f9bdb7dc8f370a5f94afb2ed7da1.tar.xz yuzu-34ce4877bd69f9bdb7dc8f370a5f94afb2ed7da1.zip | |
android: Convert SettingsActivityPresenter to Kotlin
Diffstat (limited to 'src/android/app')
2 files changed, 99 insertions, 122 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityPresenter.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityPresenter.java deleted file mode 100644 index 25b7758a9..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityPresenter.java +++ /dev/null | |||
| @@ -1,122 +0,0 @@ | |||
| 1 | package org.yuzu.yuzu_emu.features.settings.ui; | ||
| 2 | |||
| 3 | import android.content.IntentFilter; | ||
| 4 | import android.os.Bundle; | ||
| 5 | import android.text.TextUtils; | ||
| 6 | |||
| 7 | import org.yuzu.yuzu_emu.NativeLibrary; | ||
| 8 | import org.yuzu.yuzu_emu.features.settings.model.Settings; | ||
| 9 | import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile; | ||
| 10 | import org.yuzu.yuzu_emu.utils.DirectoryInitialization; | ||
| 11 | import org.yuzu.yuzu_emu.utils.DirectoryInitialization.DirectoryInitializationState; | ||
| 12 | import org.yuzu.yuzu_emu.utils.DirectoryStateReceiver; | ||
| 13 | import org.yuzu.yuzu_emu.utils.Log; | ||
| 14 | import org.yuzu.yuzu_emu.utils.ThemeUtil; | ||
| 15 | |||
| 16 | import java.io.File; | ||
| 17 | |||
| 18 | public final class SettingsActivityPresenter { | ||
| 19 | private static final String KEY_SHOULD_SAVE = "should_save"; | ||
| 20 | |||
| 21 | private SettingsActivityView mView; | ||
| 22 | |||
| 23 | private Settings mSettings = new Settings(); | ||
| 24 | |||
| 25 | private boolean mShouldSave; | ||
| 26 | |||
| 27 | private DirectoryStateReceiver directoryStateReceiver; | ||
| 28 | |||
| 29 | private String menuTag; | ||
| 30 | private String gameId; | ||
| 31 | |||
| 32 | public SettingsActivityPresenter(SettingsActivityView view) { | ||
| 33 | mView = view; | ||
| 34 | } | ||
| 35 | |||
| 36 | public void onCreate(Bundle savedInstanceState, String menuTag, String gameId) { | ||
| 37 | if (savedInstanceState == null) { | ||
| 38 | this.menuTag = menuTag; | ||
| 39 | this.gameId = gameId; | ||
| 40 | } else { | ||
| 41 | mShouldSave = savedInstanceState.getBoolean(KEY_SHOULD_SAVE); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | public void onStart() { | ||
| 46 | prepareDirectoriesIfNeeded(); | ||
| 47 | } | ||
| 48 | |||
| 49 | void loadSettingsUI() { | ||
| 50 | if (mSettings.isEmpty()) { | ||
| 51 | if (!TextUtils.isEmpty(gameId)) { | ||
| 52 | mSettings.loadSettings(gameId, mView); | ||
| 53 | } else { | ||
| 54 | mSettings.loadSettings(mView); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | mView.showSettingsFragment(menuTag, false, gameId); | ||
| 59 | mView.onSettingsFileLoaded(mSettings); | ||
| 60 | } | ||
| 61 | |||
| 62 | private void prepareDirectoriesIfNeeded() { | ||
| 63 | File configFile = new File(DirectoryInitialization.getUserDirectory() + "/config/" + SettingsFile.FILE_NAME_CONFIG + ".ini"); | ||
| 64 | if (!configFile.exists()) { | ||
| 65 | Log.error(DirectoryInitialization.getUserDirectory() + "/config/" + SettingsFile.FILE_NAME_CONFIG + ".ini"); | ||
| 66 | Log.error("yuzu config file could not be found!"); | ||
| 67 | } | ||
| 68 | if (DirectoryInitialization.areDirectoriesReady()) { | ||
| 69 | loadSettingsUI(); | ||
| 70 | } else { | ||
| 71 | mView.showLoading(); | ||
| 72 | IntentFilter statusIntentFilter = new IntentFilter( | ||
| 73 | DirectoryInitialization.BROADCAST_ACTION); | ||
| 74 | |||
| 75 | directoryStateReceiver = | ||
| 76 | new DirectoryStateReceiver(directoryInitializationState -> | ||
| 77 | { | ||
| 78 | if (directoryInitializationState == DirectoryInitializationState.YUZU_DIRECTORIES_INITIALIZED) { | ||
| 79 | mView.hideLoading(); | ||
| 80 | loadSettingsUI(); | ||
| 81 | } else if (directoryInitializationState == DirectoryInitializationState.CANT_FIND_EXTERNAL_STORAGE) { | ||
| 82 | mView.showExternalStorageNotMountedHint(); | ||
| 83 | mView.hideLoading(); | ||
| 84 | } | ||
| 85 | }); | ||
| 86 | |||
| 87 | mView.startDirectoryInitializationService(directoryStateReceiver, statusIntentFilter); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | public void setSettings(Settings settings) { | ||
| 92 | mSettings = settings; | ||
| 93 | } | ||
| 94 | |||
| 95 | public Settings getSettings() { | ||
| 96 | return mSettings; | ||
| 97 | } | ||
| 98 | |||
| 99 | public void onStop(boolean finishing) { | ||
| 100 | if (directoryStateReceiver != null) { | ||
| 101 | mView.stopListeningToDirectoryInitializationService(directoryStateReceiver); | ||
| 102 | directoryStateReceiver = null; | ||
| 103 | } | ||
| 104 | |||
| 105 | if (mSettings != null && finishing && mShouldSave) { | ||
| 106 | Log.debug("[SettingsActivity] Settings activity stopping. Saving settings to INI..."); | ||
| 107 | mSettings.saveSettings(mView); | ||
| 108 | } | ||
| 109 | |||
| 110 | ThemeUtil.applyTheme(); | ||
| 111 | |||
| 112 | NativeLibrary.ReloadSettings(); | ||
| 113 | } | ||
| 114 | |||
| 115 | public void onSettingChanged() { | ||
| 116 | mShouldSave = true; | ||
| 117 | } | ||
| 118 | |||
| 119 | public void saveState(Bundle outState) { | ||
| 120 | outState.putBoolean(KEY_SHOULD_SAVE, mShouldSave); | ||
| 121 | } | ||
| 122 | } | ||
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 new file mode 100644 index 000000000..4b24d6108 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsActivityPresenter.kt | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | package org.yuzu.yuzu_emu.features.settings.ui | ||
| 2 | |||
| 3 | import android.content.IntentFilter | ||
| 4 | import android.os.Bundle | ||
| 5 | import android.text.TextUtils | ||
| 6 | import org.yuzu.yuzu_emu.NativeLibrary | ||
| 7 | import org.yuzu.yuzu_emu.features.settings.model.Settings | ||
| 8 | import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile | ||
| 9 | import org.yuzu.yuzu_emu.utils.DirectoryInitialization | ||
| 10 | import org.yuzu.yuzu_emu.utils.DirectoryInitialization.DirectoryInitializationState | ||
| 11 | import org.yuzu.yuzu_emu.utils.DirectoryStateReceiver | ||
| 12 | import org.yuzu.yuzu_emu.utils.Log | ||
| 13 | import java.io.File | ||
| 14 | |||
| 15 | class SettingsActivityPresenter(private val activityView: SettingsActivityView) { | ||
| 16 | var settings: Settings? = Settings() | ||
| 17 | private var shouldSave = false | ||
| 18 | private var directoryStateReceiver: DirectoryStateReceiver? = null | ||
| 19 | private lateinit var menuTag: String | ||
| 20 | private lateinit var gameId: String | ||
| 21 | |||
| 22 | fun onCreate(savedInstanceState: Bundle?, menuTag: String, gameId: String) { | ||
| 23 | if (savedInstanceState == null) { | ||
| 24 | this.menuTag = menuTag | ||
| 25 | this.gameId = gameId | ||
| 26 | } else { | ||
| 27 | shouldSave = savedInstanceState.getBoolean(KEY_SHOULD_SAVE) | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | fun onStart() { | ||
| 32 | prepareDirectoriesIfNeeded() | ||
| 33 | } | ||
| 34 | |||
| 35 | private fun loadSettingsUI() { | ||
| 36 | if (settings!!.isEmpty) { | ||
| 37 | if (!TextUtils.isEmpty(gameId)) { | ||
| 38 | settings!!.loadSettings(gameId, activityView) | ||
| 39 | } else { | ||
| 40 | settings!!.loadSettings(activityView) | ||
| 41 | } | ||
| 42 | } | ||
| 43 | activityView.showSettingsFragment(menuTag, false, gameId) | ||
| 44 | activityView.onSettingsFileLoaded(settings) | ||
| 45 | } | ||
| 46 | |||
| 47 | private fun prepareDirectoriesIfNeeded() { | ||
| 48 | val configFile = | ||
| 49 | File(DirectoryInitialization.userDirectory + "/config/" + SettingsFile.FILE_NAME_CONFIG + ".ini") | ||
| 50 | if (!configFile.exists()) { | ||
| 51 | Log.error(DirectoryInitialization.userDirectory + "/config/" + SettingsFile.FILE_NAME_CONFIG + ".ini") | ||
| 52 | Log.error("yuzu config file could not be found!") | ||
| 53 | } | ||
| 54 | if (DirectoryInitialization.areDirectoriesReady()) { | ||
| 55 | loadSettingsUI() | ||
| 56 | } else { | ||
| 57 | activityView.showLoading() | ||
| 58 | val statusIntentFilter = IntentFilter(DirectoryInitialization.BROADCAST_ACTION) | ||
| 59 | directoryStateReceiver = | ||
| 60 | DirectoryStateReceiver { directoryInitializationState: DirectoryInitializationState -> | ||
| 61 | if (directoryInitializationState == DirectoryInitializationState.YUZU_DIRECTORIES_INITIALIZED) { | ||
| 62 | activityView.hideLoading() | ||
| 63 | loadSettingsUI() | ||
| 64 | } else if (directoryInitializationState == DirectoryInitializationState.CANT_FIND_EXTERNAL_STORAGE) { | ||
| 65 | activityView.showExternalStorageNotMountedHint() | ||
| 66 | activityView.hideLoading() | ||
| 67 | } | ||
| 68 | } | ||
| 69 | activityView.startDirectoryInitializationService( | ||
| 70 | directoryStateReceiver, | ||
| 71 | statusIntentFilter | ||
| 72 | ) | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | fun onStop(finishing: Boolean) { | ||
| 77 | if (directoryStateReceiver != null) { | ||
| 78 | activityView.stopListeningToDirectoryInitializationService(directoryStateReceiver!!) | ||
| 79 | directoryStateReceiver = null | ||
| 80 | } | ||
| 81 | if (settings != null && finishing && shouldSave) { | ||
| 82 | Log.debug("[SettingsActivity] Settings activity stopping. Saving settings to INI...") | ||
| 83 | settings!!.saveSettings(activityView) | ||
| 84 | } | ||
| 85 | NativeLibrary.ReloadSettings() | ||
| 86 | } | ||
| 87 | |||
| 88 | fun onSettingChanged() { | ||
| 89 | shouldSave = true | ||
| 90 | } | ||
| 91 | |||
| 92 | fun saveState(outState: Bundle) { | ||
| 93 | outState.putBoolean(KEY_SHOULD_SAVE, shouldSave) | ||
| 94 | } | ||
| 95 | |||
| 96 | companion object { | ||
| 97 | private const val KEY_SHOULD_SAVE = "should_save" | ||
| 98 | } | ||
| 99 | } | ||