Balances.kt (2820B)
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.balances 18 19 import kotlinx.serialization.SerialName 20 import kotlinx.serialization.Serializable 21 import net.taler.common.Amount 22 import net.taler.wallet.PrefsScopeInfo 23 import net.taler.wallet.ScopeInfoType 24 25 @Serializable 26 data class BalanceItem( 27 val scopeInfo: ScopeInfo, 28 val available: Amount, 29 val pendingIncoming: Amount, 30 val pendingOutgoing: Amount, 31 val disablePeerPayments: Boolean, 32 val shoppingUrls: List<String> = emptyList(), 33 ) { 34 val currency: String get() = available.currency 35 } 36 37 @Serializable 38 sealed class ScopeInfo { 39 abstract val currency: String 40 41 @Serializable 42 @SerialName("global") 43 data class Global( 44 override val currency: String 45 ): ScopeInfo() 46 47 @Serializable 48 @SerialName("exchange") 49 data class Exchange( 50 override val currency: String, 51 val url: String, 52 ): ScopeInfo() 53 54 @Serializable 55 @SerialName("auditor") 56 data class Auditor( 57 override val currency: String, 58 val url: String, 59 ): ScopeInfo() 60 61 fun toPrefs(): PrefsScopeInfo { 62 val type = when (this) { 63 is Global -> ScopeInfoType.GLOBAL 64 is Exchange -> ScopeInfoType.EXCHANGE 65 is Auditor -> ScopeInfoType.AUDITOR 66 } 67 68 val url = when (this) { 69 is Exchange -> url 70 is Auditor -> url 71 else -> null 72 } 73 74 return PrefsScopeInfo 75 .newBuilder() 76 .setType(type) 77 .setCurrency(currency) 78 .apply { 79 if (url != null) 80 setUrl(url) 81 }.build() 82 } 83 84 companion object { 85 fun fromPrefs(scope: PrefsScopeInfo) = when (scope.type) { 86 ScopeInfoType.GLOBAL -> Global(currency = scope.currency) 87 88 ScopeInfoType.EXCHANGE -> Exchange( 89 currency = scope.currency, 90 url = scope.url, 91 ) 92 93 ScopeInfoType.AUDITOR -> Auditor( 94 currency = scope.currency, 95 url = scope.url, 96 ) 97 98 else -> null 99 } 100 } 101 }