Skip to content

Commit

Permalink
Fix crashes in save sync settings when upgrading from previous version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Swordfish90 committed Apr 21, 2024
1 parent 28a1f11 commit 52e9487
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 132 deletions.
4 changes: 2 additions & 2 deletions lemuroid-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ plugins {

android {
defaultConfig {
versionCode = 202
versionName = "1.15.0" // Always remember to update Cores Tag!
versionCode = 205
versionName = "1.16.0" // Always remember to update Cores Tag!
applicationId = "com.swordfish.lemuroid"
}
flavorDimensions += listOf("opensource", "cores")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,12 @@ class MainActivity : RetrogradeComponentActivity(), BusyActivity {

setContent {
val navController = rememberNavController()

val uiState =
mainViewModel.state
.observeAsState(MainViewModel.UiState())
.value

MainScreen(navController, uiState)
MainScreen(navController)
}
}

@Composable
private fun MainScreen(
navController: NavHostController,
mainUIState: MainViewModel.UiState,
) {
private fun MainScreen(navController: NavHostController) {
AppTheme {
val navBackStackEntry = navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry.value?.destination
Expand Down Expand Up @@ -164,6 +155,12 @@ class MainActivity : RetrogradeComponentActivity(), BusyActivity {
infoDialogDisplayed.value = true
}

val mainUIState =
mainViewModel.state
.observeAsState(MainViewModel.UiState())
.value

// TODO COMPOSE Get rid of this double scaffold.
Scaffold(
bottomBar = { MainNavigationBar(currentRoute, navController) },
) { outerPadding ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ fun SearchScreen(
onHelpPressed = onHelpPressed,
context = context,
saveSyncEnabled = mainUIState.saveSyncEnabled,
operationsInProgress = mainUIState.operationInProgress,
)
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private fun RomsSettings(
indexingInProgress: Boolean,
scanInProgress: Boolean,
) {
val context = LocalContext.current.applicationContext
val context = LocalContext.current

val currentDirectory = state.currentDirectory
val emptyDirectory = stringResource(R.string.none)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.swordfish.lemuroid.app.mobile.feature.settings.savesync

import android.content.Intent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
Expand All @@ -16,19 +12,20 @@ import androidx.compose.ui.res.stringResource
import com.swordfish.lemuroid.R
import com.swordfish.lemuroid.app.shared.savesync.SaveSyncWork
import com.swordfish.lemuroid.app.utils.android.compose.MergedPaddingValues
import com.swordfish.lemuroid.app.utils.android.indexSetPreferenceState
import com.swordfish.lemuroid.app.utils.android.settings.LemuroidCardSettingsGroup
import com.swordfish.lemuroid.app.utils.android.settings.LemuroidSettingsListMultiSelect
import com.swordfish.lemuroid.app.utils.android.settings.LemuroidSettingsMenuLink
import com.swordfish.lemuroid.app.utils.android.settings.LemuroidSettingsPage
import com.swordfish.lemuroid.app.utils.android.settings.LemuroidSettingsSwitch
import com.swordfish.lemuroid.app.utils.android.settings.booleanPreferenceState
import com.swordfish.lemuroid.lib.library.CoreID
import com.swordfish.lemuroid.app.utils.android.stringsSetPreferenceState

@Composable
fun SaveSyncSettingsScreen(
padding: MergedPaddingValues,
viewModel: SaveSyncSettingsViewModel,
) {
val context = LocalContext.current.applicationContext
val context = LocalContext.current

val saveSyncState =
viewModel.uiState
Expand All @@ -40,66 +37,69 @@ fun SaveSyncSettingsScreen(
.observeAsState(true)
.value

Column(
modifier =
Modifier
.fillMaxWidth()
.verticalScroll(rememberScrollState())
.padding(padding.asPaddingValues()),
) {
LemuroidSettingsMenuLink(
title = { Text(text = stringResource(id = R.string.settings_save_sync_configure, saveSyncState.provider)) },
subtitle = { Text(text = saveSyncState.configInfo) },
enabled = !isSyncInProgress,
onClick = { context.startActivity(Intent(context, saveSyncState.settingsActivity)) },
)
LemuroidSettingsSwitch(
state = booleanPreferenceState(R.string.pref_key_save_sync_enable, default = false),
title = { Text(text = stringResource(id = R.string.settings_save_sync_include_saves)) },
subtitle = {
Text(
text =
stringResource(
id = R.string.settings_save_sync_include_saves_description,
saveSyncState.savesSpace,
),
)
},
enabled = saveSyncState.isConfigured && !isSyncInProgress,
)
LemuroidSettingsListMultiSelect(
state =
indexSetPreferenceState(
stringResource(R.string.pref_key_save_sync_cores),
emptySet(),
CoreID.values().map { it.coreName },
),
title = { Text(text = stringResource(id = R.string.settings_save_sync_include_states)) },
subtitle = { Text(text = stringResource(id = R.string.settings_save_sync_include_states_description)) },
useSelectedValuesAsSubtitle = false,
items = saveSyncState.coreNames,
enabled = saveSyncState.isConfigured && !isSyncInProgress,
confirmButton = stringResource(id = R.string.ok),
)
LemuroidSettingsSwitch(
state = booleanPreferenceState(R.string.pref_key_save_sync_auto, default = false),
title = { Text(text = stringResource(id = R.string.settings_save_sync_enable_auto)) },
subtitle = { Text(text = stringResource(id = R.string.settings_save_sync_enable_auto_description)) },
enabled = saveSyncState.isConfigured && !isSyncInProgress,
)
LemuroidSettingsMenuLink(
title = { Text(text = stringResource(id = R.string.settings_save_sync_refresh)) },
subtitle = {
Text(
text =
stringResource(
id = R.string.settings_save_sync_refresh_description,
saveSyncState.lastSyncInfo,
),
)
},
enabled = saveSyncState.isConfigured && !isSyncInProgress,
onClick = { SaveSyncWork.enqueueManualWork(context) },
)
LemuroidSettingsPage(modifier = Modifier.padding(padding.asPaddingValues())) {
LemuroidCardSettingsGroup {
LemuroidSettingsMenuLink(
title = {
Text(
text =
stringResource(
id = R.string.settings_save_sync_configure,
saveSyncState.provider,
),
)
},
subtitle = { Text(text = saveSyncState.configInfo) },
enabled = !isSyncInProgress,
onClick = { context.startActivity(Intent(context, saveSyncState.settingsActivity)) },
)
LemuroidSettingsSwitch(
state = booleanPreferenceState(R.string.pref_key_save_sync_enable, default = false),
title = { Text(text = stringResource(id = R.string.settings_save_sync_include_saves)) },
subtitle = {
Text(
text =
stringResource(
id = R.string.settings_save_sync_include_saves_description,
saveSyncState.savesSpace,
),
)
},
enabled = saveSyncState.isConfigured && !isSyncInProgress,
)
LemuroidSettingsListMultiSelect(
state =
stringsSetPreferenceState(
stringResource(R.string.pref_key_save_sync_cores),
emptySet(),
),
title = { Text(text = stringResource(id = R.string.settings_save_sync_include_states)) },
subtitle = { Text(text = stringResource(id = R.string.settings_save_sync_include_states_description)) },
entryValues = saveSyncState.coreNames,
entries = saveSyncState.coreVisibleNames,
enabled = saveSyncState.isConfigured && !isSyncInProgress,
confirmButton = stringResource(id = R.string.ok),
)
LemuroidSettingsSwitch(
state = booleanPreferenceState(R.string.pref_key_save_sync_auto, default = false),
title = { Text(text = stringResource(id = R.string.settings_save_sync_enable_auto)) },
subtitle = { Text(text = stringResource(id = R.string.settings_save_sync_enable_auto_description)) },
enabled = saveSyncState.isConfigured && !isSyncInProgress,
)
LemuroidSettingsMenuLink(
title = { Text(text = stringResource(id = R.string.settings_save_sync_refresh)) },
subtitle = {
Text(
text =
stringResource(
id = R.string.settings_save_sync_refresh_description,
saveSyncState.lastSyncInfo,
),
)
},
enabled = saveSyncState.isConfigured && !isSyncInProgress,
onClick = { SaveSyncWork.enqueueManualWork(context) },
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SaveSyncSettingsViewModel(
val savesSpace: String = "",
val lastSyncInfo: String = "",
val coreNames: List<String> = emptyList(),
val coreVisibleNames: List<String> = emptyList(),
val provider: String = "",
val settingsActivity: Class<out Activity>? = null,
)
Expand All @@ -53,15 +54,19 @@ class SaveSyncSettingsViewModel(
saveSyncManager.computeSavesSpace(),
saveSyncManager.getLastSyncInfo(),
computeCoreNames(),
computeCoreVisibleNames(),
saveSyncManager.getProvider(),
saveSyncManager.getSettingsActivity(),
)
}

private fun computeCoreNames(): List<String> {
return CoreID.values().map { it.coreName }
}

private fun computeCoreVisibleNames(): List<String> {
val context = getContext()
return CoreID.values()
.map { saveSyncManager.getDisplayNameForCore(context, it) }
return CoreID.values().map { saveSyncManager.getDisplayNameForCore(context, it) }
}

private fun getContext(): Context {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ fun LemuroidTopAppBar(
}
},
actions = {
LemuroidTopBarActions(route, navController, context, mainUIState.saveSyncEnabled, onHelpPressed)
LemuroidTopBarActions(
route = route,
navController = navController,
context = context,
saveSyncEnabled = mainUIState.saveSyncEnabled,
onHelpPressed = onHelpPressed,
operationsInProgress = mainUIState.operationInProgress,
)
},
)
}
Expand All @@ -82,6 +89,7 @@ fun LemuroidTopBarActions(
navController: NavController,
context: Context,
saveSyncEnabled: Boolean,
operationsInProgress: Boolean,
onHelpPressed: () -> Unit,
) {
Row {
Expand All @@ -96,6 +104,7 @@ fun LemuroidTopBarActions(
if (saveSyncEnabled) {
IconButton(
onClick = { SaveSyncWork.enqueueManualWork(context.applicationContext) },
enabled = !operationsInProgress,
) {
Icon(
Icons.Outlined.CloudSync,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package com.swordfish.lemuroid.app.utils.android

import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import com.swordfish.lemuroid.app.utils.settings.rememberPreferenceSetSettingState
import com.swordfish.lemuroid.app.utils.settings.rememberPreferenceStringsSetSettingState
import com.swordfish.lemuroid.lib.preferences.SharedPreferencesHelper

@Composable
fun indexSetPreferenceState(
fun stringsSetPreferenceState(
key: String,
default: Set<String>,
values: List<String>,
) = rememberPreferenceSetSettingState(
) = rememberPreferenceStringsSetSettingState(
key = key,
values = values,
defaultValue = default,
preferences = SharedPreferencesHelper.getSharedPreferences(LocalContext.current),
)
Loading

0 comments on commit 52e9487

Please sign in to comment.