Migrate SleepTimerCustomDialog to compose
This commit is contained in:
parent
a3e188529d
commit
c37ced08a7
|
@ -39,7 +39,7 @@ import com.simplemobiletools.commons.models.FAQItem
|
|||
import com.simplemobiletools.commons.models.RadioItem
|
||||
import com.simplemobiletools.flashlight.BuildConfig
|
||||
import com.simplemobiletools.flashlight.R
|
||||
import com.simplemobiletools.flashlight.dialogs.SleepTimerCustomDialog
|
||||
import com.simplemobiletools.flashlight.dialogs.SleepTimerCustomAlertDialog
|
||||
import com.simplemobiletools.flashlight.extensions.config
|
||||
import com.simplemobiletools.flashlight.extensions.startAboutActivity
|
||||
import com.simplemobiletools.flashlight.helpers.*
|
||||
|
@ -87,9 +87,25 @@ class MainActivity : ComponentActivity() {
|
|||
}
|
||||
)
|
||||
|
||||
val sleepTimerCustomDialogState = rememberAlertDialogState().apply {
|
||||
DialogMember {
|
||||
SleepTimerCustomAlertDialog(
|
||||
alertDialogState = this,
|
||||
onConfirmClick = {
|
||||
if (it > 0) {
|
||||
pickedSleepTimer(it)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val sleepTimerDialogState = rememberAlertDialogState().apply {
|
||||
DialogMember {
|
||||
SleepTimerRadioDialog(alertDialogState = this)
|
||||
SleepTimerRadioDialog(
|
||||
alertDialogState = this,
|
||||
customAlertDialogState = sleepTimerCustomDialogState
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -258,7 +274,8 @@ class MainActivity : ComponentActivity() {
|
|||
|
||||
@Composable
|
||||
private fun SleepTimerRadioDialog(
|
||||
alertDialogState: AlertDialogState
|
||||
alertDialogState: AlertDialogState,
|
||||
customAlertDialogState: AlertDialogState
|
||||
) {
|
||||
val items = ArrayList(listOf(10, 30, 60, 5 * 60, 10 * 60, 30 * 60).map {
|
||||
RadioItem(it, secondsToString(it))
|
||||
|
@ -277,11 +294,7 @@ class MainActivity : ComponentActivity() {
|
|||
selectedItemId = preferences.lastSleepTimerSeconds,
|
||||
callback = {
|
||||
if (it as Int == -1) {
|
||||
SleepTimerCustomDialog(this) {
|
||||
if (it > 0) {
|
||||
pickedSleepTimer(it)
|
||||
}
|
||||
}
|
||||
customAlertDialogState.show()
|
||||
} else if (it > 0) {
|
||||
pickedSleepTimer(it)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,140 @@
|
|||
package com.simplemobiletools.flashlight.dialogs
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.dimensionResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import com.simplemobiletools.commons.R
|
||||
import com.simplemobiletools.commons.compose.alert_dialog.AlertDialogState
|
||||
import com.simplemobiletools.commons.compose.alert_dialog.rememberAlertDialogState
|
||||
import com.simplemobiletools.commons.compose.extensions.MyDevices
|
||||
import com.simplemobiletools.commons.compose.theme.AppThemeSurface
|
||||
import com.simplemobiletools.commons.dialogs.dialogBorder
|
||||
import com.simplemobiletools.commons.dialogs.dialogContainerColor
|
||||
import com.simplemobiletools.commons.dialogs.dialogElevation
|
||||
import com.simplemobiletools.commons.dialogs.dialogShape
|
||||
|
||||
private val items = listOf(
|
||||
R.string.minutes_raw,
|
||||
R.string.seconds_raw,
|
||||
)
|
||||
|
||||
@Composable
|
||||
fun SleepTimerCustomAlertDialog(
|
||||
alertDialogState: AlertDialogState,
|
||||
modifier: Modifier = Modifier,
|
||||
onConfirmClick: (seconds: Int) -> Unit,
|
||||
onCancelClick: () -> Unit = {}
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
var selectedItem by remember { mutableIntStateOf(0) }
|
||||
var value by remember { mutableStateOf("") }
|
||||
|
||||
AlertDialog(
|
||||
modifier = modifier
|
||||
.dialogBorder,
|
||||
onDismissRequest = alertDialogState::hide
|
||||
) {
|
||||
Surface(
|
||||
modifier = modifier,
|
||||
shape = dialogShape,
|
||||
color = dialogContainerColor,
|
||||
tonalElevation = dialogElevation,
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.padding(all = dimensionResource(id = R.dimen.big_margin))
|
||||
) {
|
||||
Text(
|
||||
modifier = Modifier.padding(bottom = dimensionResource(id = R.dimen.activity_margin)),
|
||||
text = stringResource(id = R.string.sleep_timer),
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
|
||||
Column(
|
||||
modifier = Modifier.padding(
|
||||
start = dimensionResource(id = R.dimen.activity_margin),
|
||||
end = dimensionResource(id = R.dimen.activity_margin),
|
||||
top = dimensionResource(id = R.dimen.activity_margin),
|
||||
)
|
||||
) {
|
||||
TextField(
|
||||
modifier = Modifier.padding(
|
||||
bottom = dimensionResource(id = R.dimen.normal_margin),
|
||||
),
|
||||
value = value,
|
||||
onValueChange = {
|
||||
value = it.filter { it.isDigit() }
|
||||
},
|
||||
label = {
|
||||
Text(stringResource(id = R.string.value))
|
||||
},
|
||||
keyboardOptions = KeyboardOptions(
|
||||
keyboardType = KeyboardType.Number
|
||||
)
|
||||
)
|
||||
|
||||
items.forEachIndexed { index, item ->
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
RadioButton(
|
||||
selected = index == selectedItem,
|
||||
onClick = {
|
||||
selectedItem = index
|
||||
}
|
||||
)
|
||||
Text(
|
||||
text = stringResource(id = item)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Row(
|
||||
Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.End
|
||||
) {
|
||||
TextButton(onClick = {
|
||||
onCancelClick()
|
||||
alertDialogState.hide()
|
||||
}) {
|
||||
Text(text = stringResource(id = R.string.cancel))
|
||||
}
|
||||
TextButton(onClick = {
|
||||
val enteredValue = Integer.valueOf(value.ifEmpty { "0" })
|
||||
val multiplier = getMultiplier(items[selectedItem])
|
||||
onConfirmClick(enteredValue * multiplier)
|
||||
alertDialogState.hide()
|
||||
}) {
|
||||
Text(text = stringResource(id = R.string.ok))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getMultiplier(id: Int) = when (id) {
|
||||
R.string.seconds_raw -> 1
|
||||
R.string.minutes_raw -> 60
|
||||
else -> 60
|
||||
}
|
||||
|
||||
@Composable
|
||||
@MyDevices
|
||||
private fun SleepTimerCustomAlertDialogPreview() {
|
||||
AppThemeSurface {
|
||||
SleepTimerCustomAlertDialog(
|
||||
alertDialogState = rememberAlertDialogState(),
|
||||
onConfirmClick = {},
|
||||
onCancelClick = {},
|
||||
)
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
package com.simplemobiletools.flashlight.dialogs
|
||||
|
||||
import android.app.Activity
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.flashlight.R
|
||||
import com.simplemobiletools.flashlight.databinding.DialogCustomSleepTimerPickerBinding
|
||||
|
||||
class SleepTimerCustomDialog(val activity: Activity, val callback: (seconds: Int) -> Unit) {
|
||||
private var dialog: AlertDialog? = null
|
||||
private val binding = DialogCustomSleepTimerPickerBinding.inflate(activity.layoutInflater)
|
||||
|
||||
init {
|
||||
binding.dialogRadioView.check(R.id.dialog_radio_minutes)
|
||||
binding.timerValue.setOnEditorActionListener { _, actionId, _ ->
|
||||
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
||||
dialogConfirmed()
|
||||
return@setOnEditorActionListener true
|
||||
}
|
||||
return@setOnEditorActionListener false
|
||||
}
|
||||
|
||||
activity.getAlertDialogBuilder()
|
||||
.setPositiveButton(R.string.ok) { _, _ -> dialogConfirmed() }
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.apply {
|
||||
activity.setupDialogStuff(binding.root, this, R.string.sleep_timer) { alertDialog ->
|
||||
dialog = alertDialog
|
||||
alertDialog.showKeyboard(binding.timerValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun dialogConfirmed() {
|
||||
val value = binding.timerValue.value
|
||||
val minutes = Integer.valueOf(value.ifEmpty { "0" })
|
||||
val multiplier = getMultiplier(binding.dialogRadioView.checkedRadioButtonId)
|
||||
callback(minutes * multiplier)
|
||||
activity.hideKeyboard()
|
||||
dialog?.dismiss()
|
||||
}
|
||||
|
||||
private fun getMultiplier(id: Int) = when (id) {
|
||||
R.id.dialog_radio_seconds -> 1
|
||||
R.id.dialog_radio_minutes -> 60
|
||||
else -> 60
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue