taler-android

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

AccountManager.kt (4132B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2020 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.util.Log
     20 import androidx.annotation.UiThread
     21 import kotlinx.coroutines.CoroutineScope
     22 import kotlinx.coroutines.flow.MutableStateFlow
     23 import kotlinx.coroutines.flow.asStateFlow
     24 import kotlinx.coroutines.launch
     25 import net.taler.wallet.main.TAG
     26 import net.taler.wallet.accounts.ListBankAccountsResult.Error
     27 import net.taler.wallet.accounts.ListBankAccountsResult.None
     28 import net.taler.wallet.accounts.ListBankAccountsResult.Success
     29 import net.taler.wallet.backend.TalerErrorInfo
     30 import net.taler.wallet.backend.WalletBackendApi
     31 import org.json.JSONArray
     32 
     33 class AccountManager(
     34     private val api: WalletBackendApi,
     35     private val scope: CoroutineScope,
     36 ) {
     37     private val mBankAccounts = MutableStateFlow<ListBankAccountsResult>(None)
     38     internal val bankAccounts = mBankAccounts.asStateFlow()
     39 
     40     @UiThread
     41     fun listBankAccounts(currency: String? = null) = scope.launch {
     42         mBankAccounts.value = None
     43         api.request("listBankAccounts", ListBankAccountsResponse.serializer()) {
     44             if (currency != null) put("currency", currency)
     45             this
     46         }.onError { error ->
     47             Log.e(TAG, "Error listKnownBankAccounts $error")
     48             mBankAccounts.value = Error(error)
     49         }.onSuccess { response ->
     50             mBankAccounts.value = Success(
     51                 accounts = response.accounts.map { acc ->
     52                     acc.copy(
     53                         // FIXME: workaround for malformed Cyclos payto
     54                         //   remove when fixed in libeufin
     55                         paytoUri = acc.paytoUri.replace(
     56                             "^payto://cyclos".toRegex(),
     57                             "payto://cyclos/",
     58                         ),
     59                     )
     60                 },
     61                 currency = currency,
     62             )
     63         }
     64     }
     65 
     66     suspend fun addBankAccount(
     67         paytoUri: String,
     68         label: String,
     69         currencies: List<String>? = null,
     70         replaceBankAccountId: String? = null,
     71         onError: (error: TalerErrorInfo) -> Unit,
     72     ) {
     73         api.request<Unit>("addBankAccount") {
     74             currencies?.let { put("currencies", JSONArray(it)) }
     75             replaceBankAccountId?.let { put("replaceBankAccountId", it) }
     76             put("paytoUri", paytoUri)
     77             put("label", label)
     78         }.onError { error ->
     79             Log.e(TAG, "Error addKnownBankAccount $error")
     80             onError(error)
     81         }.onSuccess {
     82             listBankAccounts()
     83         }
     84     }
     85 
     86     fun forgetBankAccount(
     87         id: String,
     88         onError: (error: TalerErrorInfo) -> Unit,
     89     ) = scope.launch {
     90         api.request<Unit>("forgetBankAccount") {
     91             put("bankAccountId", id)
     92         }.onError { error ->
     93             Log.e(TAG, "Error addKnownBankAccount $error")
     94             onError(error)
     95         }.onSuccess {
     96             listBankAccounts()
     97         }
     98     }
     99 
    100     suspend fun getBankAccountById(
    101         id: String,
    102         onError: (error: TalerErrorInfo) -> Unit,
    103     ): KnownBankAccountInfo? {
    104         var response: KnownBankAccountInfo? = null
    105         api.request("getBankAccountById", KnownBankAccountInfo.serializer()) {
    106             put("bankAccountId", id)
    107         }.onError { error ->
    108             Log.e(TAG, "Error getBankAccountById $error")
    109             onError(error)
    110         }.onSuccess {
    111             response = it
    112         }
    113 
    114         return response
    115     }
    116 }