From 65b366a388b5cb5eaa426d8d35e3c26ec51ec6be Mon Sep 17 00:00:00 2001 From: FunkyMuse Date: Wed, 20 Sep 2023 21:42:38 +0200 Subject: [PATCH] feat: migrate add/edit blocked number dialog to Compose --- .../ManageBlockedNumbersActivity.kt | 42 ++++-- .../commons/dialogs/AddBlockedNumberDialog.kt | 131 +++++++++++++----- 2 files changed, 125 insertions(+), 48 deletions(-) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/activities/ManageBlockedNumbersActivity.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/activities/ManageBlockedNumbersActivity.kt index 5bc7a4314..c3beee6d2 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/activities/ManageBlockedNumbersActivity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/activities/ManageBlockedNumbersActivity.kt @@ -9,19 +9,18 @@ import android.os.Bundle import android.widget.Toast import androidx.activity.compose.setContent import androidx.activity.viewModels -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.remember +import androidx.compose.runtime.* import androidx.compose.ui.platform.LocalContext import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.viewModelScope import com.simplemobiletools.commons.R +import com.simplemobiletools.commons.compose.alert_dialog.rememberAlertDialogState import com.simplemobiletools.commons.compose.extensions.enableEdgeToEdgeSimple import com.simplemobiletools.commons.compose.extensions.onEventValue import com.simplemobiletools.commons.compose.screens.ManageBlockedNumbersScreen import com.simplemobiletools.commons.compose.theme.AppThemeSurface -import com.simplemobiletools.commons.dialogs.AddBlockedNumberDialog +import com.simplemobiletools.commons.dialogs.AddOrEditBlockedNumberAlertDialog import com.simplemobiletools.commons.dialogs.ExportBlockedNumbersDialog import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.extensions.* @@ -74,10 +73,33 @@ class ManageBlockedNumbersActivity : BaseSimpleActivity() { val isDefaultDialer: Boolean = onEventValue { context.isDefaultDialer() } + AppThemeSurface { + var clickedBlockedNumber by remember { mutableStateOf(null) } + val addBlockedNumberDialogState = rememberAlertDialogState() + + addBlockedNumberDialogState.DialogMember { + AddOrEditBlockedNumberAlertDialog( + blockedNumber = clickedBlockedNumber, + alertDialogState = addBlockedNumberDialogState, + deleteBlockedNumber = { + deleteBlockedNumber(it) + updateBlockedNumbers() + }, + addBlockedNumber = { + addBlockedNumber(it) + clickedBlockedNumber = null + updateBlockedNumbers() + } + ) + } + ManageBlockedNumbersScreen( goBack = ::finish, - onAdd = ::addOrEditBlockedNumber, + onAdd = { + clickedBlockedNumber = null + addBlockedNumberDialogState.show() + }, onImportBlockedNumbers = ::tryImportBlockedNumbers, onExportBlockedNumbers = ::tryExportBlockedNumbers, setAsDefault = ::maybeSetDefaultCallerIdApp, @@ -98,8 +120,8 @@ class ManageBlockedNumbersActivity : BaseSimpleActivity() { deleteBlockedNumbers(blockedNumbers, selectedKeys) }, onEdit = { blockedNumber -> - addOrEditBlockedNumber(currentNumber = blockedNumber) - manageBlockedNumbersViewModel.updateBlockedNumbers() + clickedBlockedNumber = blockedNumber + addBlockedNumberDialogState.show() }, onCopy = { blockedNumber -> copyToClipboard(blockedNumber.number) @@ -187,12 +209,6 @@ class ManageBlockedNumbersActivity : BaseSimpleActivity() { } } - private fun addOrEditBlockedNumber(currentNumber: BlockedNumber? = null) { - AddBlockedNumberDialog(this, currentNumber) { - updateBlockedNumbers() - } - } - private fun updateBlockedNumbers() { manageBlockedNumbersViewModel.updateBlockedNumbers() } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/AddBlockedNumberDialog.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/AddBlockedNumberDialog.kt index f771d1d78..d73604757 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/AddBlockedNumberDialog.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/AddBlockedNumberDialog.kt @@ -1,44 +1,105 @@ package com.simplemobiletools.commons.dialogs -import android.app.Activity -import androidx.appcompat.app.AlertDialog +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.* +import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.platform.LocalSoftwareKeyboardController +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.window.DialogProperties import com.simplemobiletools.commons.R -import com.simplemobiletools.commons.databinding.DialogAddBlockedNumberBinding -import com.simplemobiletools.commons.extensions.* +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.compose.theme.Shapes import com.simplemobiletools.commons.models.BlockedNumber +import kotlinx.coroutines.android.awaitFrame -class AddBlockedNumberDialog(val activity: Activity, val originalNumber: BlockedNumber? = null, val callback: () -> Unit) { - init { - val view = DialogAddBlockedNumberBinding.inflate(activity.layoutInflater, null, false).apply { - if (originalNumber != null) { - addBlockedNumberEdittext.setText(originalNumber.number) - } - } - - activity.getAlertDialogBuilder() - .setPositiveButton(R.string.ok, null) - .setNegativeButton(R.string.cancel, null) - .apply { - activity.setupDialogStuff(view.root, this) { alertDialog -> - alertDialog.showKeyboard(view.addBlockedNumberEdittext) - alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { - var newBlockedNumber = view.addBlockedNumberEdittext.value - if (originalNumber != null && newBlockedNumber != originalNumber.number) { - activity.deleteBlockedNumber(originalNumber.number) - } - - if (newBlockedNumber.isNotEmpty()) { - // in case the user also added a '.' in the pattern, remove it - if (newBlockedNumber.contains(".*")) { - newBlockedNumber = newBlockedNumber.replace(".*", "*") - } - activity.addBlockedNumber(newBlockedNumber) - } - - callback() - alertDialog.dismiss() - } +@Composable +fun AddOrEditBlockedNumberAlertDialog( + blockedNumber: BlockedNumber?, + alertDialogState: AlertDialogState = rememberAlertDialogState(), + deleteBlockedNumber: (String) -> Unit, + addBlockedNumber: (String) -> Unit +) { + val keyboardController = LocalSoftwareKeyboardController.current + val focusRequester = remember { FocusRequester() } + var textFieldValue by remember { mutableStateOf(blockedNumber?.number.orEmpty()) } + AlertDialog( + modifier = Modifier.fillMaxWidth(0.9f), + properties = DialogProperties(usePlatformDefaultWidth = false), + onDismissRequest = alertDialogState::hide, + confirmButton = { + TextButton(onClick = { + var newBlockedNumber = textFieldValue + if (blockedNumber != null && newBlockedNumber != blockedNumber.number) { + deleteBlockedNumber(blockedNumber.number) } + + if (newBlockedNumber.isNotEmpty()) { + // in case the user also added a '.' in the pattern, remove it + if (newBlockedNumber.contains(".*")) { + newBlockedNumber = newBlockedNumber.replace(".*", "*") + } + addBlockedNumber(newBlockedNumber) + } + alertDialogState.hide() + }) { + Text(text = stringResource(id = R.string.ok)) } + }, + dismissButton = { + TextButton(onClick = { + alertDialogState.hide() + }) { + Text(text = stringResource(id = R.string.cancel)) + } + }, + shape = Shapes.medium, + text = { + OutlinedTextField( + modifier = Modifier + .fillMaxWidth() + .focusRequester(focusRequester), + value = textFieldValue, + onValueChange = { + textFieldValue = it + }, + supportingText = { + Text(text = stringResource(id = R.string.add_blocked_number_helper_text)) + }, + label = { + Text(text = stringResource(id = R.string.number)) + }, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone) + ) + }, + ) + LaunchedEffect(Unit) { + //await two frames to render the scrim and the dialog + awaitFrame() + awaitFrame() + keyboardController?.show() + focusRequester.requestFocus() + } +} + +@Composable +@MyDevices +private fun AddOrEditBlockedNumberAlertDialogPreview() { + AppThemeSurface { + AddOrEditBlockedNumberAlertDialog( + blockedNumber = null, + deleteBlockedNumber = {}, + addBlockedNumber = {} + ) } }