DonauManager.kt (3591B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2025 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.donau 18 19 import android.util.Log 20 import kotlinx.coroutines.CoroutineScope 21 import kotlinx.coroutines.flow.MutableStateFlow 22 import kotlinx.coroutines.flow.asStateFlow 23 import kotlinx.coroutines.launch 24 import kotlinx.coroutines.runBlocking 25 import net.taler.wallet.main.TAG 26 import net.taler.wallet.backend.TalerErrorInfo 27 import net.taler.wallet.backend.WalletBackendApi 28 import net.taler.wallet.exchanges.ExchangeManager 29 30 class DonauManager( 31 private val api: WalletBackendApi, 32 private val scope: CoroutineScope, 33 private val exchangeManager: ExchangeManager, 34 ) { 35 private val mDonauStatus = MutableStateFlow<GetDonauStatus>(GetDonauStatus.None) 36 val donauStatus = mDonauStatus.asStateFlow() 37 38 private val mDonauStatementsStatus = 39 MutableStateFlow<GetDonauStatementsStatus>(GetDonauStatementsStatus.None) 40 val donauStatementsStatus = mDonauStatementsStatus.asStateFlow() 41 42 fun setDonau( 43 info: DonauInfo, 44 onSuccess: () -> Unit, 45 onError: (error: TalerErrorInfo) -> Unit, 46 ) = scope.launch { 47 api.request<Unit>("setDonau") { 48 put("donauBaseUrl", info.donauBaseUrl) 49 put("taxPayerId", info.taxPayerId) 50 }.onError { error -> 51 Log.e(TAG, "Error setDonau $error") 52 onError(error) 53 }.onSuccess { 54 getDonau() 55 onSuccess() 56 } 57 } 58 59 fun getDonau() = scope.launch { 60 mDonauStatus.value = GetDonauStatus.Loading 61 api.request("getDonau", GetDonauResponse.serializer()) 62 .onError { error -> 63 Log.e(TAG, "Error getDonau $error") 64 mDonauStatus.value = GetDonauStatus.Error(error) 65 }.onSuccess { res -> 66 mDonauStatus.value = GetDonauStatus.Success(res.currentDonauInfo) 67 } 68 } 69 70 fun getDonauStatements( 71 donauBaseUrl: String? = null, 72 ) = scope.launch { 73 mDonauStatementsStatus.value = GetDonauStatementsStatus.Loading 74 api.request("getDonauStatements", GetDonauStatementsResponse.serializer()) { 75 donauBaseUrl?.let { put("donauBaseUrl", it) } 76 this 77 }.onError { error -> 78 Log.e(TAG, "Error retrieving donau statements: $error") 79 mDonauStatementsStatus.value = GetDonauStatementsStatus.Error(error) 80 }.onSuccess { res -> 81 val statements = res.statements.map { statement -> 82 val spec = runBlocking { exchangeManager 83 .getSpecForCurrency(statement.total.currency) } 84 statement.copy( 85 total = statement.total.withSpec(spec) 86 ) 87 }.sortedByDescending { 88 it.year 89 } 90 91 mDonauStatementsStatus.value = GetDonauStatementsStatus.Success( 92 statements = statements, 93 ) 94 } 95 } 96 }