Skip to content

Commit

Permalink
Migrate factory reset dialog to compose.
Browse files Browse the repository at this point in the history
  • Loading branch information
Swordfish90 committed Apr 1, 2024
1 parent 7387649 commit 5d3f06d
Showing 1 changed file with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.swordfish.lemuroid.app.mobile.feature.settings.advanced

import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import com.alorma.compose.settings.storage.disk.rememberPreferenceIntSettingState
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.swordfish.lemuroid.R
import com.swordfish.lemuroid.app.mobile.feature.main.MainRoute
import com.swordfish.lemuroid.app.utils.android.compose.MergedPaddingValues
Expand Down Expand Up @@ -84,7 +87,7 @@ private fun GeneralSettings(
viewModel: AdvancedSettingsViewModel,
navController: NavController,
) {
val context = LocalContext.current.applicationContext
val factoryResetDialogState = remember { mutableStateOf(false) }

LemuroidCardSettingsGroup(
title = { Text(text = stringResource(id = R.string.settings_category_general)) },
Expand Down Expand Up @@ -117,17 +120,42 @@ private fun GeneralSettings(
LemuroidSettingsMenuLink(
title = { Text(text = stringResource(id = R.string.settings_title_reset_settings)) },
subtitle = { Text(text = stringResource(id = R.string.settings_description_reset_settings)) },
onClick = {
MaterialAlertDialogBuilder(context)
.setTitle(R.string.reset_settings_warning_message_title)
.setMessage(R.string.reset_settings_warning_message_description)
.setPositiveButton(R.string.ok) { _, _ ->
viewModel.resetAllSettings()
navController.popBackStack(MainRoute.SETTINGS.route, false)
}
.setNegativeButton(R.string.cancel) { _, _ -> }
.show()
},
onClick = { factoryResetDialogState.value = true },
)
}

if (factoryResetDialogState.value) {
FactoryResetDialog(factoryResetDialogState, viewModel, navController)
}
}

@Composable
private fun FactoryResetDialog(
factoryResetDialogState: MutableState<Boolean>,
viewModel: AdvancedSettingsViewModel,
navController: NavController,
) {
val onDismiss = {
factoryResetDialogState.value = false
}
AlertDialog(
title = { Text(stringResource(id = R.string.reset_settings_warning_message_title)) },
text = { Text(stringResource(id = R.string.reset_settings_warning_message_description)) },
onDismissRequest = onDismiss,
confirmButton = {
TextButton(
onClick = {
viewModel.resetAllSettings()
navController.popBackStack(MainRoute.SETTINGS.route, false)
},
) {
Text(text = stringResource(id = R.string.ok))
}
},
dismissButton = {
TextButton(onClick = onDismiss) {
Text(text = stringResource(id = R.string.cancel))
}
},
)
}

0 comments on commit 5d3f06d

Please sign in to comment.