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
+119 -92
@@ -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
fun MainScreen(
gdInstalled: Boolean = true,
@@ -665,10 +771,6 @@ fun MainScreen(
) {
val context = LocalContext.current
val shouldAutomaticallyLaunch by PreferenceUtils.useBooleanPreference(
preferenceKey = PreferenceUtils.Key.LOAD_AUTOMATICALLY
)
val shouldUpdate by PreferenceUtils.useBooleanPreference(PreferenceUtils.Key.UPDATE_AUTOMATICALLY)
val autoUpdateState by releaseViewModel.uiState.collectAsState()
@@ -678,7 +780,6 @@ fun MainScreen(
val geodeInstalled = geodePreinstalled || geodeJustInstalled
var beginLaunch by remember { mutableStateOf(false) }
var showSafeModeDialog by remember { mutableStateOf(false) }
var launchInSafeMode by remember { mutableStateOf(false) }
LaunchedEffect(shouldUpdate) {
@@ -731,81 +832,19 @@ fun MainScreen(
modifier = Modifier.padding(12.dp)
)
if (gdInstalled && geodeInstalled) {
val stopLaunch = releaseViewModel.isInUpdate || hasError || showSafeModeDialog
if (shouldAutomaticallyLaunch && !stopLaunch) {
val countdownTimer = useCountdownTimer(
time = 3000,
onCountdownFinish = { beginLaunch = true }
)
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) {
when {
gdInstalled && geodeInstalled -> {
val stopLaunch = releaseViewModel.isInUpdate || hasError
PlayButton(
stopAutomaticLaunch = stopLaunch,
blockLaunch = releaseViewModel.isInUpdate,
onPlayGame = { safeMode ->
launchInSafeMode = safeMode
beginLaunch = true
}
}
}
}
}
Row {
Button(
onClick = { },
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) {
gdInstalled -> {
Text(
context.getString(R.string.geode_download_title),
modifier = Modifier.padding(12.dp)
@@ -831,7 +870,8 @@ fun MainScreen(
)
}
}
} else {
}
else -> {
Text(
context.getString(R.string.game_not_found),
modifier = Modifier.padding(12.dp)
@@ -845,6 +885,7 @@ fun MainScreen(
Text(context.getString(R.string.launcher_settings))
}
}
}
UpdateCard(
releaseViewModel,
@@ -860,20 +901,6 @@ fun MainScreen(
launchInSafeMode = false
}
}
if (showSafeModeDialog) {
SafeModeDialog(
onDismiss = {
showSafeModeDialog = false
},
onLaunch = {
launchInSafeMode = true
beginLaunch = true
showSafeModeDialog = false
}
)
}
}
@Preview(showSystemUi = true)