mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-02-10 08:50:51 +01:00
Extract dialogs and permissions into separate composables
This commit is contained in:
parent
b536626c3c
commit
74f4675f19
@ -66,63 +66,12 @@ class MainActivity : ComponentActivity() {
|
|||||||
setContent {
|
setContent {
|
||||||
AppThemeSurface {
|
AppThemeSurface {
|
||||||
val showMoreApps = onEventValue { !resources.getBoolean(R.bool.hide_google_relations) }
|
val showMoreApps = onEventValue { !resources.getBoolean(R.bool.hide_google_relations) }
|
||||||
val sosPermissionLauncher = rememberLauncherForActivityResult(
|
val sosPermissionLauncher = getCameraPermissionLauncher(onResult = getPermissionResultHandler(true))
|
||||||
contract = ActivityResultContracts.RequestPermission(),
|
val stroboscopePermissionLauncher = getCameraPermissionLauncher(onResult = getPermissionResultHandler(false))
|
||||||
onResult = {
|
|
||||||
if (it) {
|
|
||||||
cameraPermissionGranted(true)
|
|
||||||
} else {
|
|
||||||
toast(R.string.camera_permission)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
val stroboscopePermissionLauncher = rememberLauncherForActivityResult(
|
|
||||||
contract = ActivityResultContracts.RequestPermission(),
|
|
||||||
onResult = {
|
|
||||||
if (it) {
|
|
||||||
cameraPermissionGranted(false)
|
|
||||||
} else {
|
|
||||||
toast(R.string.camera_permission)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
val sleepTimerCustomDialogState = rememberAlertDialogState().apply {
|
val sleepTimerCustomDialogState = getSleepTimerCustomDialogState()
|
||||||
DialogMember {
|
val sleepTimerDialogState = getSleepTimerDialogState(showCustomSleepTimerDialog = sleepTimerCustomDialogState::show)
|
||||||
SleepTimerCustomAlertDialog(
|
val sleepTimerPermissionDialogState = getSleepTimerPermissionDialogState(showSleepTimerDialog = sleepTimerDialogState::show)
|
||||||
alertDialogState = this,
|
|
||||||
onConfirmClick = {
|
|
||||||
if (it > 0) {
|
|
||||||
pickedSleepTimer(it)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val sleepTimerDialogState = rememberAlertDialogState().apply {
|
|
||||||
DialogMember {
|
|
||||||
SleepTimerRadioDialog(
|
|
||||||
alertDialogState = this,
|
|
||||||
onCustomValueSelected = sleepTimerCustomDialogState::show
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val sleepTimerPermissionDialogState = rememberAlertDialogState().apply {
|
|
||||||
DialogMember {
|
|
||||||
PermissionRequiredAlertDialog(
|
|
||||||
alertDialogState = this,
|
|
||||||
text = stringResource(id = R.string.allow_alarm_sleep_timer),
|
|
||||||
positiveActionCallback = {
|
|
||||||
openRequestExactAlarmSettings(baseConfig.appId)
|
|
||||||
},
|
|
||||||
negativeActionCallback = {
|
|
||||||
sleepTimerDialogState.show()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MainScreen(
|
MainScreen(
|
||||||
flashlightButton = {
|
flashlightButton = {
|
||||||
@ -283,6 +232,56 @@ class MainActivity : ComponentActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun getCameraPermissionLauncher(
|
||||||
|
onResult: (Boolean) -> Unit
|
||||||
|
) = rememberLauncherForActivityResult(
|
||||||
|
contract = ActivityResultContracts.RequestPermission(),
|
||||||
|
onResult = onResult
|
||||||
|
)
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun getSleepTimerCustomDialogState() = rememberAlertDialogState().apply {
|
||||||
|
DialogMember {
|
||||||
|
SleepTimerCustomAlertDialog(
|
||||||
|
alertDialogState = this,
|
||||||
|
onConfirmClick = {
|
||||||
|
if (it > 0) {
|
||||||
|
pickedSleepTimer(it)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun getSleepTimerDialogState(
|
||||||
|
showCustomSleepTimerDialog: () -> Unit
|
||||||
|
) = rememberAlertDialogState().apply {
|
||||||
|
DialogMember {
|
||||||
|
SleepTimerRadioDialog(
|
||||||
|
alertDialogState = this,
|
||||||
|
onCustomValueSelected = showCustomSleepTimerDialog
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun getSleepTimerPermissionDialogState(
|
||||||
|
showSleepTimerDialog: () -> Unit
|
||||||
|
) = rememberAlertDialogState().apply {
|
||||||
|
DialogMember {
|
||||||
|
PermissionRequiredAlertDialog(
|
||||||
|
alertDialogState = this,
|
||||||
|
text = stringResource(id = R.string.allow_alarm_sleep_timer),
|
||||||
|
positiveActionCallback = {
|
||||||
|
openRequestExactAlarmSettings(baseConfig.appId)
|
||||||
|
},
|
||||||
|
negativeActionCallback = showSleepTimerDialog
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
viewModel.onResume()
|
viewModel.onResume()
|
||||||
@ -335,6 +334,18 @@ class MainActivity : ComponentActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getPermissionResultHandler(isSos: Boolean): (Boolean) -> Unit = {
|
||||||
|
handlePermissionResult(isSos, it)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handlePermissionResult(isSos: Boolean, granted: Boolean) {
|
||||||
|
if (granted) {
|
||||||
|
cameraPermissionGranted(isSos)
|
||||||
|
} else {
|
||||||
|
toast(R.string.camera_permission)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun cameraPermissionGranted(isSOS: Boolean) {
|
private fun cameraPermissionGranted(isSOS: Boolean) {
|
||||||
if (isSOS) {
|
if (isSOS) {
|
||||||
viewModel.enableSos()
|
viewModel.enableSos()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user