summaryrefslogtreecommitdiff
path: root/TalerWallet1/Views/Banking/QuiteSomeCoins.swift
blob: 4bcfb88f6282bde2e3ccb3d6626f738f13fc60a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import SwiftUI
import taler_swift
import SymLog

struct SomeCoins {
    let numCoins: Int       // 0 == invalid, -1 == unknown
    var unknown:   Bool { numCoins < 0 }
    var invalid:   Bool { numCoins == 0 }
    var manyCoins: Bool { numCoins > 99 }
    var quiteSome: Bool { numCoins > 199 }
    var tooMany:   Bool { numCoins > 999 }

    let fee: Amount?
    func feeLabel(_ currencyInfo: CurrencyInfo?) -> String {
       return if let fee {
                invalid ? "Amount too small!"
              : tooMany ? "Amount too big for a single withdrawal!"
           : fee.isZero ? "No withdrawal fee"
                        : "- \(fee.string(currencyInfo)) fee"
       } else {
           EMPTYSTRING
       }
    }
}

extension SomeCoins {
    init(details: WithdrawalAmountDetails?) {
        do {
            if let details {
                // Incoming: fee = raw - effective
                let aFee = try details.amountRaw - details.amountEffective
                self.init(numCoins: details.numCoins ?? -1,    // either the number of coins, or unknown
                               fee: aFee)
                return
            }
        } catch {}
        self.init(numCoins: 0, fee: nil)      // invalid
    }

    init(details: CheckPeerPullCreditResponse?) {
        do {
            if let details {
                // Incoming: fee = raw - effective
                let aFee = try details.amountRaw - details.amountEffective
                self.init(numCoins: details.numCoins ?? -1,    // either the number of coins, or unknown
                               fee: aFee)
                return
            }
        } catch {}
        self.init(numCoins: 0, fee: nil)      // invalid
    }
}
// MARK: -
struct QuiteSomeCoins: View {
    private let symLog = SymLogV(0)
    let someCoins: SomeCoins
    let shouldShowFee: Bool
    let currency: String
    let currencyInfo: CurrencyInfo?
    let amountEffective: Amount?

    var body: some View {
        if !someCoins.invalid {
            if !someCoins.tooMany {
                if someCoins.manyCoins {
                    Text(someCoins.quiteSome ? "Note: It will take quite some time to withdraw this amount! Be more patient..."
                                             : "Note: It will take some time to withdraw this amount. Be patient...")
                    .foregroundColor(someCoins.quiteSome ? .red : .primary)
                    .talerFont(.body)
                    .multilineTextAlignment(.leading)
                    .padding(.vertical, 6)
                } // warnings
            }
        }
        if shouldShowFee {
            if let fee = someCoins.fee {
                Text(someCoins.feeLabel(currencyInfo))
                    .foregroundColor((someCoins.invalid || someCoins.tooMany || !fee.isZero) ? .red : .primary)
                    .talerFont(.body)
            }
        }
    }
}
// MARK: -
struct QuiteSomeCoins_Previews: PreviewProvider {
    static var previews: some View {
        QuiteSomeCoins(someCoins: SomeCoins(numCoins: 4, fee: Amount(currency: LONGCURRENCY, cent: 20) ),
                   shouldShowFee: true,
                        currency: LONGCURRENCY,
                    currencyInfo: nil,
                 amountEffective: nil)
    }
}