change main activity control flow

This commit is contained in:
qimiko committed 2024-02-18 05:19:45 -07:00
1 parent e37de9fc3a
commit 9c8a5ab372
1 file changed
+152 -125
@@ -656,6 +656,112 @@ suspend fun showFailureSnackbar(
} }
} }
@Composable
fun PlayButton(
stopAutomaticLaunch: Boolean,
blockLaunch: Boolean,
onPlayGame: (Boolean) -> Unit
) {
val context = LocalContext.current
var showSafeModeDialog by remember { mutableStateOf(false) }
var launchInSafeMode by remember { mutableStateOf(false) }
val shouldAutomaticallyLaunch by PreferenceUtils.useBooleanPreference(
preferenceKey = PreferenceUtils.Key.LOAD_AUTOMATICALLY
)
if (shouldAutomaticallyLaunch && !stopAutomaticLaunch && !showSafeModeDialog) {
val countdownTimer = useCountdownTimer(
time = 3000,
onCountdownFinish = { onPlayGame(launchInSafeMode) }
)
if (countdownTimer != 0L) {
Text(
pluralStringResource(
R.plurals.automatically_load_countdown,
countdownTimer.toInt(),
countdownTimer
),
style = Typography.bodyMedium,
color = MaterialTheme.colorScheme.onSecondaryContainer,
)
Spacer(Modifier.size(12.dp))
}
}
// compose apis don't provide a good way of adding long press to a button
val interactionSource = remember { MutableInteractionSource() }
val viewConfiguration = LocalViewConfiguration.current
val haptics = LocalHapticFeedback.current
LaunchedEffect(interactionSource) {
interactionSource.interactions.collectLatest { interaction ->
when (interaction) {
is PressInteraction.Press -> {
launchInSafeMode = false
delay(viewConfiguration.longPressTimeoutMillis)
// perform a second delay to make the action more obvious
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
delay(viewConfiguration.longPressTimeoutMillis)
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
showSafeModeDialog = true
}
is PressInteraction.Release -> {
if (!showSafeModeDialog) {
onPlayGame(launchInSafeMode)
}
}
}
}
}
Row {
Button(
onClick = { },
enabled = !blockLaunch,
interactionSource = interactionSource
) {
Icon(
Icons.Filled.PlayArrow,
contentDescription = context.getString(R.string.launcher_launch_icon_alt)
)
Spacer(Modifier.size(ButtonDefaults.IconSpacing))
Text(context.getString(R.string.launcher_launch))
}
Spacer(Modifier.size(2.dp))
IconButton(onClick = { onSettings(context) }) {
Icon(
Icons.Filled.Settings,
contentDescription = context.getString(R.string.launcher_settings_icon_alt)
)
}
}
if (showSafeModeDialog) {
SafeModeDialog(
onDismiss = {
launchInSafeMode = false
showSafeModeDialog = false
},
onLaunch = {
launchInSafeMode = true
onPlayGame(true)
showSafeModeDialog = false
}
)
}
}
@Composable @Composable
fun MainScreen( fun MainScreen(
gdInstalled: Boolean = true, gdInstalled: Boolean = true,
@@ -665,10 +771,6 @@ fun MainScreen(
) { ) {
val context = LocalContext.current val context = LocalContext.current
val shouldAutomaticallyLaunch by PreferenceUtils.useBooleanPreference(
preferenceKey = PreferenceUtils.Key.LOAD_AUTOMATICALLY
)
val shouldUpdate by PreferenceUtils.useBooleanPreference(PreferenceUtils.Key.UPDATE_AUTOMATICALLY) val shouldUpdate by PreferenceUtils.useBooleanPreference(PreferenceUtils.Key.UPDATE_AUTOMATICALLY)
val autoUpdateState by releaseViewModel.uiState.collectAsState() val autoUpdateState by releaseViewModel.uiState.collectAsState()
@@ -678,7 +780,6 @@ fun MainScreen(
val geodeInstalled = geodePreinstalled || geodeJustInstalled val geodeInstalled = geodePreinstalled || geodeJustInstalled
var beginLaunch by remember { mutableStateOf(false) } var beginLaunch by remember { mutableStateOf(false) }
var showSafeModeDialog by remember { mutableStateOf(false) }
var launchInSafeMode by remember { mutableStateOf(false) } var launchInSafeMode by remember { mutableStateOf(false) }
LaunchedEffect(shouldUpdate) { LaunchedEffect(shouldUpdate) {
@@ -731,118 +832,58 @@ fun MainScreen(
modifier = Modifier.padding(12.dp) modifier = Modifier.padding(12.dp)
) )
if (gdInstalled && geodeInstalled) { when {
val stopLaunch = releaseViewModel.isInUpdate || hasError || showSafeModeDialog gdInstalled && geodeInstalled -> {
if (shouldAutomaticallyLaunch && !stopLaunch) { val stopLaunch = releaseViewModel.isInUpdate || hasError
val countdownTimer = useCountdownTimer( PlayButton(
time = 3000, stopAutomaticLaunch = stopLaunch,
onCountdownFinish = { beginLaunch = true } blockLaunch = releaseViewModel.isInUpdate,
onPlayGame = { safeMode ->
launchInSafeMode = safeMode
beginLaunch = true
},
)
}
gdInstalled -> {
Text(
context.getString(R.string.geode_download_title),
modifier = Modifier.padding(12.dp)
) )
if (countdownTimer != 0L) { Row {
Text( Button(
pluralStringResource( onClick = { releaseViewModel.runReleaseCheck(true) },
R.plurals.automatically_load_countdown, enabled = !releaseViewModel.isInUpdate
countdownTimer.toInt(), ) {
countdownTimer Icon(
), painterResource(R.drawable.icon_download),
style = Typography.bodyMedium, contentDescription = context.getString(R.string.launcher_download_icon_alt)
color = MaterialTheme.colorScheme.onSecondaryContainer, )
) Spacer(Modifier.size(ButtonDefaults.IconSpacing))
Spacer(Modifier.size(12.dp)) Text(context.getString(R.string.launcher_download))
} }
} Spacer(Modifier.size(2.dp))
IconButton(onClick = { onSettings(context) }) {
// compose apis don't provide a good way of adding long press to a button Icon(
val interactionSource = remember { MutableInteractionSource() } Icons.Filled.Settings,
val viewConfiguration = LocalViewConfiguration.current contentDescription = context.getString(R.string.launcher_settings_icon_alt)
val haptics = LocalHapticFeedback.current )
LaunchedEffect(interactionSource) {
interactionSource.interactions.collectLatest { interaction ->
when (interaction) {
is PressInteraction.Press -> {
launchInSafeMode = false
delay(viewConfiguration.longPressTimeoutMillis)
// perform a second delay to make the action more obvious
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
delay(viewConfiguration.longPressTimeoutMillis)
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
showSafeModeDialog = true
}
is PressInteraction.Release -> {
if (!showSafeModeDialog) {
beginLaunch = true
}
}
} }
} }
} }
else -> {
Row { Text(
Button( context.getString(R.string.game_not_found),
onClick = { }, modifier = Modifier.padding(12.dp)
enabled = !releaseViewModel.isInUpdate,
interactionSource = interactionSource
) {
Icon(
Icons.Filled.PlayArrow,
contentDescription = context.getString(R.string.launcher_launch_icon_alt)
)
Spacer(Modifier.size(ButtonDefaults.IconSpacing))
Text(context.getString(R.string.launcher_launch))
}
Spacer(Modifier.size(2.dp))
IconButton(onClick = { onSettings(context) }) {
Icon(
Icons.Filled.Settings,
contentDescription = context.getString(R.string.launcher_settings_icon_alt)
)
}
}
} else if (gdInstalled) {
Text(
context.getString(R.string.geode_download_title),
modifier = Modifier.padding(12.dp)
)
Row {
Button(
onClick = { releaseViewModel.runReleaseCheck(true) },
enabled = !releaseViewModel.isInUpdate
) {
Icon(
painterResource(R.drawable.icon_download),
contentDescription = context.getString(R.string.launcher_download_icon_alt)
)
Spacer(Modifier.size(ButtonDefaults.IconSpacing))
Text(context.getString(R.string.launcher_download))
}
Spacer(Modifier.size(2.dp))
IconButton(onClick = { onSettings(context) }) {
Icon(
Icons.Filled.Settings,
contentDescription = context.getString(R.string.launcher_settings_icon_alt)
)
}
}
} else {
Text(
context.getString(R.string.game_not_found),
modifier = Modifier.padding(12.dp)
)
OutlinedButton(onClick = { onSettings(context) }) {
Icon(
Icons.Filled.Settings,
contentDescription = context.getString(R.string.launcher_settings_icon_alt)
) )
Spacer(Modifier.size(ButtonDefaults.IconSpacing)) OutlinedButton(onClick = { onSettings(context) }) {
Text(context.getString(R.string.launcher_settings)) Icon(
Icons.Filled.Settings,
contentDescription = context.getString(R.string.launcher_settings_icon_alt)
)
Spacer(Modifier.size(ButtonDefaults.IconSpacing))
Text(context.getString(R.string.launcher_settings))
}
} }
} }
@@ -860,20 +901,6 @@ fun MainScreen(
launchInSafeMode = false launchInSafeMode = false
} }
} }
if (showSafeModeDialog) {
SafeModeDialog(
onDismiss = {
showSafeModeDialog = false
},
onLaunch = {
launchInSafeMode = true
beginLaunch = true
showSafeModeDialog = false
}
)
}
} }
@Preview(showSystemUi = true) @Preview(showSystemUi = true)