taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

AddBankAccountScreen.kt (3997B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2026 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.wallet.accounts
     18 
     19 import androidx.compose.foundation.layout.fillMaxSize
     20 import androidx.compose.foundation.layout.padding
     21 import androidx.compose.material3.Text
     22 import androidx.compose.runtime.Composable
     23 import androidx.compose.runtime.LaunchedEffect
     24 import androidx.compose.runtime.getValue
     25 import androidx.compose.runtime.mutableStateOf
     26 import androidx.compose.runtime.remember
     27 import androidx.compose.runtime.rememberCoroutineScope
     28 import androidx.compose.runtime.setValue
     29 import androidx.compose.ui.Modifier
     30 import androidx.compose.ui.res.stringResource
     31 import kotlinx.coroutines.launch
     32 import net.taler.wallet.R
     33 import net.taler.wallet.backend.TalerErrorInfo
     34 import net.taler.wallet.compose.GlobalScaffold
     35 import net.taler.wallet.compose.LoadingScreen
     36 import net.taler.wallet.compose.TalerSurface
     37 import net.taler.wallet.deposit.GetDepositWireTypesResponse
     38 import net.taler.wallet.main.MainViewModel
     39 
     40 @Composable
     41 fun AddBankAccountScreen(
     42     model: MainViewModel,
     43     bankAccountId: String?,
     44     onNavigateBack: () -> Unit,
     45     onShowError: (TalerErrorInfo) -> Unit,
     46 ) {
     47     val accountManager = model.accountManager
     48     val depositManager = model.depositManager
     49     val coroutineScope = rememberCoroutineScope()
     50 
     51     var depositWireTypes by remember { mutableStateOf<GetDepositWireTypesResponse?>(null) }
     52     var bankAccount by remember { mutableStateOf<KnownBankAccountInfo?>(null) }
     53 
     54     LaunchedEffect(bankAccountId) {
     55         if (bankAccountId != null) {
     56             bankAccount = accountManager.getBankAccountById(bankAccountId) { error ->
     57                 onShowError(error)
     58             }
     59         }
     60     }
     61 
     62     LaunchedEffect(Unit) {
     63         depositWireTypes = depositManager.getDepositWireTypes()
     64     }
     65 
     66     TalerSurface {
     67         GlobalScaffold(
     68             model = model,
     69             modifier = Modifier.fillMaxSize(),
     70             onNavigateBack = onNavigateBack,
     71             title = {
     72                 if (bankAccountId != null) {
     73                     Text(stringResource(R.string.send_deposit_account_edit))
     74                 } else {
     75                     Text(stringResource(R.string.send_deposit_account_add))
     76                 }
     77             },
     78         ) { paddingValues ->
     79             if (depositWireTypes == null || (bankAccountId != null && bankAccount == null)) {
     80                 LoadingScreen(Modifier.padding(paddingValues))
     81             } else {
     82                 AddAccountComposable(
     83                     modifier = Modifier.padding(paddingValues),
     84                     presetAccount = bankAccount,
     85                     depositWireTypes = depositWireTypes!!,
     86                     validateIban = depositManager::validateIban,
     87                     onSubmit = { paytoUri, label ->
     88                         coroutineScope.launch {
     89                             accountManager.addBankAccount(
     90                                 paytoUri = paytoUri,
     91                                 label = label,
     92                                 replaceBankAccountId = bankAccountId,
     93                             ) {
     94                                 onShowError(it)
     95                             }
     96                             onNavigateBack()
     97                         }
     98                     },
     99                     onClose = onNavigateBack,
    100                 )
    101             }
    102         }
    103     }
    104 }