Cancelling current action when closing dialog
This commit is contained in:
parent
66d9214c4f
commit
8fde7985d8
2 changed files with 31 additions and 8 deletions
|
@ -13,9 +13,7 @@ import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.text.input.ImeAction
|
import androidx.compose.ui.text.input.ImeAction
|
||||||
import androidx.compose.ui.text.style.TextOverflow
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import net.codinux.banking.ui.IOorDefault
|
import net.codinux.banking.ui.IOorDefault
|
||||||
import net.codinux.banking.ui.composables.BankIcon
|
import net.codinux.banking.ui.composables.BankIcon
|
||||||
import net.codinux.banking.ui.config.Colors
|
import net.codinux.banking.ui.config.Colors
|
||||||
|
@ -47,20 +45,33 @@ fun AddAccountDialog(
|
||||||
|
|
||||||
var isAddingAccount by remember { mutableStateOf(false) }
|
var isAddingAccount by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
var addAccountJob: Job? = null
|
||||||
|
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
|
|
||||||
|
|
||||||
|
fun dismiss() {
|
||||||
|
addAccountJob?.cancel()
|
||||||
|
|
||||||
|
addAccountJob = null
|
||||||
|
|
||||||
|
onDismiss()
|
||||||
|
}
|
||||||
|
|
||||||
fun confirmCalled() {
|
fun confirmCalled() {
|
||||||
selectedBank?.let { bank ->
|
selectedBank?.let { bank ->
|
||||||
isAddingAccount = true
|
isAddingAccount = true
|
||||||
|
|
||||||
coroutineScope.launch(Dispatchers.IOorDefault) {
|
addAccountJob = coroutineScope.launch(Dispatchers.IOorDefault) {
|
||||||
val successful = DI.bankingService.addAccount(bank, loginName, password, retrieveAllTransactions)
|
val successful = DI.bankingService.addAccount(bank, loginName, password, retrieveAllTransactions)
|
||||||
|
|
||||||
|
addAccountJob = null
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
isAddingAccount = false
|
isAddingAccount = false
|
||||||
|
|
||||||
if (successful) {
|
if (successful) {
|
||||||
onDismiss()
|
dismiss()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,7 +85,7 @@ fun AddAccountDialog(
|
||||||
confirmButtonEnabled = isRequiredDataEntered && isAddingAccount == false,
|
confirmButtonEnabled = isRequiredDataEntered && isAddingAccount == false,
|
||||||
showProgressIndicatorOnConfirmButton = isAddingAccount,
|
showProgressIndicatorOnConfirmButton = isAddingAccount,
|
||||||
useMoreThanPlatformDefaultWidthOnMobile = true,
|
useMoreThanPlatformDefaultWidthOnMobile = true,
|
||||||
onDismiss = onDismiss,
|
onDismiss = { dismiss() },
|
||||||
onConfirm = {
|
onConfirm = {
|
||||||
confirmCalled()
|
confirmCalled()
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,13 +78,23 @@ fun TransferMoneyDialog(
|
||||||
|
|
||||||
var isInitialized by remember { mutableStateOf(false) }
|
var isInitialized by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
var transferMoneyJob: Job? = null
|
||||||
|
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
|
|
||||||
|
|
||||||
|
fun dismiss() {
|
||||||
|
transferMoneyJob?.cancel() // TODO: is it senseful to stop to ongoing process? as we cannot know its state, e.g. if the bank server already executed the transfer
|
||||||
|
|
||||||
|
transferMoneyJob = null
|
||||||
|
|
||||||
|
onDismiss()
|
||||||
|
}
|
||||||
|
|
||||||
fun confirmCalled() {
|
fun confirmCalled() {
|
||||||
isTransferringMoney = true
|
isTransferringMoney = true
|
||||||
|
|
||||||
coroutineScope.launch(Dispatchers.IOorDefault) {
|
transferMoneyJob = coroutineScope.launch(Dispatchers.IOorDefault) {
|
||||||
val successful = bankingService.transferMoney(
|
val successful = bankingService.transferMoney(
|
||||||
accountsToUser[senderAccount]!!, senderAccount,
|
accountsToUser[senderAccount]!!, senderAccount,
|
||||||
recipientName, recipientAccountIdentifier,
|
recipientName, recipientAccountIdentifier,
|
||||||
|
@ -94,11 +104,13 @@ fun TransferMoneyDialog(
|
||||||
// TODO: determine BIC to IBAN
|
// TODO: determine BIC to IBAN
|
||||||
)
|
)
|
||||||
|
|
||||||
|
transferMoneyJob = null
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
isTransferringMoney = false
|
isTransferringMoney = false
|
||||||
|
|
||||||
if (successful) {
|
if (successful) {
|
||||||
onDismiss()
|
dismiss()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue