feat: migrate add/edit blocked number dialog to Compose

This commit is contained in:
FunkyMuse 2023-09-20 21:42:38 +02:00
parent 5598de895b
commit 65b366a388
2 changed files with 125 additions and 48 deletions

View file

@ -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<BlockedNumber?>(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()
}

View file

@ -1,30 +1,47 @@
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)
@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()) {
@ -32,13 +49,57 @@ class AddBlockedNumberDialog(val activity: Activity, val originalNumber: Blocked
if (newBlockedNumber.contains(".*")) {
newBlockedNumber = newBlockedNumber.replace(".*", "*")
}
activity.addBlockedNumber(newBlockedNumber)
}
callback()
alertDialog.dismiss()
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 = {}
)
}
}