taler-android

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

AddAccountFragment.kt (4439B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2024 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 android.os.Bundle
     20 import android.view.LayoutInflater
     21 import android.view.ViewGroup
     22 import androidx.appcompat.app.AppCompatActivity
     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.platform.ComposeView
     30 import androidx.fragment.app.Fragment
     31 import androidx.fragment.app.activityViewModels
     32 import androidx.navigation.fragment.findNavController
     33 import kotlinx.coroutines.launch
     34 import net.taler.wallet.main.MainViewModel
     35 import net.taler.wallet.R
     36 import net.taler.wallet.compose.LoadingScreen
     37 import net.taler.wallet.compose.TalerSurface
     38 import net.taler.wallet.deposit.GetDepositWireTypesResponse
     39 import net.taler.wallet.showError
     40 
     41 class AddAccountFragment: Fragment() {
     42     private val model: MainViewModel by activityViewModels()
     43     private val accountManager by lazy { model.accountManager }
     44     private val depositManager by lazy { model.depositManager }
     45     private var bankAccountId: String? = null
     46 
     47     override fun onCreateView(
     48         inflater: LayoutInflater,
     49         container: ViewGroup?,
     50         savedInstanceState: Bundle?
     51     ) = ComposeView(requireContext()).apply {
     52         bankAccountId = arguments?.getString("bankAccountId")
     53 
     54         val supportActionBar = (requireActivity() as? AppCompatActivity)?.supportActionBar
     55         if (bankAccountId == null) {
     56             supportActionBar?.setTitle(R.string.send_deposit_account_add)
     57         } else {
     58             supportActionBar?.setTitle(R.string.send_deposit_account_edit)
     59         }
     60 
     61         setContent {
     62             TalerSurface {
     63                 var depositWireTypes by remember { mutableStateOf<GetDepositWireTypesResponse?>(null) }
     64                 var bankAccount by remember { mutableStateOf<KnownBankAccountInfo?>(null) }
     65                 val coroutineScope = rememberCoroutineScope()
     66 
     67                 LaunchedEffect(bankAccountId) {
     68                     if (bankAccountId == null) return@LaunchedEffect
     69                     bankAccount = accountManager.getBankAccountById(bankAccountId!!) { error ->
     70                         showError(error)
     71                     }
     72                 }
     73 
     74                 LaunchedEffect(Unit) {
     75                     depositWireTypes = depositManager.getDepositWireTypes()
     76                 }
     77 
     78                 if (depositWireTypes == null || (bankAccountId != null && bankAccount == null)) {
     79                     LoadingScreen()
     80                 } else {
     81                     AddAccountComposable(
     82                         presetAccount = bankAccount,
     83                         depositWireTypes = depositWireTypes!!,
     84                         validateIban = depositManager::validateIban,
     85                         onSubmit = { paytoUri, label ->
     86                             coroutineScope.launch {
     87                                 accountManager.addBankAccount(
     88                                     paytoUri = paytoUri,
     89                                     label = label,
     90                                     replaceBankAccountId = bankAccountId,
     91                                 ) {
     92                                     showError(it)
     93                                 }
     94                                 // TODO: should we return on error?
     95                                 findNavController().popBackStack()
     96                             }
     97                         },
     98                         onClose = {
     99                             findNavController().popBackStack()
    100                         },
    101                     )
    102                 }
    103             }
    104         }
    105     }
    106 }