aboutsummaryrefslogtreecommitdiff
path: root/TalerWallet1/Views/Balances/PendingRowView.swift
blob: a8b92939bd0220a6d86c0246cb27a59bbb682210 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import SwiftUI
import taler_swift

struct InOrOutView: View {
    let titles: (String, String?)
    let isHorizontal: Bool
    let incoming: Bool
    let imageName: String

    public static func width(image1: String, titles: (String, String?), isHorizontal: Bool) -> CGFloat {
        let imageFont = TalerFont.uiFont(.largeTitle)
        let uiFont = TalerFont.uiFont(.body)

        let image2 = "++"
        let imageWidth = image2.widthOfString(usingUIFont: imageFont) + 8.0     // spacing: 8
        let (title1, title2) = titles
        let title1Width = title1.widthOfString(usingUIFont: uiFont)
        var title2Width = 0.0
        var totalWidth = title1Width
        if let title2 {
            title2Width = title2.widthOfString(usingUIFont: uiFont)
            let totalStr = title1 + " " + title2
            totalWidth = totalStr.widthOfString(usingUIFont: uiFont)
        }

//    let logStr = String(format: "image: %.2f   title: %.2f   total: %.2f", imageWidth, max(title1Width, title2Width), totalWidth)
//    print(logStr)
        return imageWidth + max(title1Width, title2Width)
    }

    var body: some View {
        HStack(spacing: 8) {
            let pendingColor = WalletColors().pendingColor(incoming)
            Image(systemName: imageName)
                .foregroundColor(pendingColor)
                .accessibilityFont(.largeTitle)
                .accessibility(hidden: true)
            Text(title(titles, isHorizontal))
                .lineLimit(4)
                .accessibilityFont(.body)
        }
    }
}

/// This view shows a pending transaction row in a currency section
struct PendingRowView: View {
    let amount: Amount
    let currencyInfo: CurrencyInfo?
    let incoming: Bool

    @AppStorage("iconOnly") var iconOnly: Bool = false

    let inTitle0 = String(localized: "Incoming", comment: "Abbreviation <pending incoming>")
    let inTitle1 = String(localized: "Pending", comment: "Top of line <Pending incoming>")
    let inTitle2 = String(localized: "incoming", comment: "Bottom of line <pending incoming>")
    let outTitle0 = String(localized: "Outgoing", comment: "Abbreviation <pending outgoing>")
    let outTitle1 = String(localized: "Pending", comment: "Top of line <Pending outgoing>")
    let outTitle2 = String(localized: "outgoing", comment: "Bottom of line <pending outgoing>")

    func needVStack(available: CGFloat, inOrOutWidth: CGFloat, valueWidth: CGFloat) -> Bool {
        available < (inOrOutWidth + valueWidth + 40)
    }

    var body: some View {
        let imageName = incoming ? "plus.circle"        // .fill
                                 : "minus.circle"       // .fill
        let imageStr = String("\(Image(systemName: imageName))")
        let pendingColor = WalletColors().pendingColor(incoming)
        SingleAxisGeometryReader { width in
            Group {
                let amountStr = amount.string(currencyInfo)
                let amountWidth = amountStr.width(largeAmountFont: false)
                let inTitle = iconOnly ? (inTitle0, nil)
                                       : (inTitle1, inTitle2)
                let outTitle = iconOnly ? (outTitle0, nil)
                                        : (outTitle1, outTitle2)
                let title = incoming ? inTitle
                                     : outTitle
                let inOrOutWidth = InOrOutView.width(image1: imageStr,
                                                     titles: incoming ? inTitle : outTitle,
                                                     isHorizontal: true)

                let needVStack = needVStack(available: width, inOrOutWidth: inOrOutWidth, valueWidth: amountWidth)
                AmountRowV(amountStr: amountStr, amountColor: pendingColor, largeAmountFont: false,
                           fitsHorizontal: !needVStack, vertAlignment: .center) {
                    // isHorizontal=true to try to fit "- Pending outgoing" + amount in 1 line
                    InOrOutView(titles: title, isHorizontal: true, incoming: incoming, imageName: imageName)
                }
            }
        }
    }
}
// MARK: -
#if DEBUG

func PreviewCurrencyInfo(_ currency: String, digits: Int) -> CurrencyInfo {
    let unitName = digits == 0 ? "¥" : "€"
    let scope = ScopeInfo(type: .global, currency: currency)
    let specs = CurrencySpecification(name: TESTCURRENCY,
//                        decimalSeparator: ".", groupSeparator: "'",
                     fractionalInputDigits: digits,
                    fractionalNormalDigits: digits,
              fractionalTrailingZeroDigits: digits,
//                   isCurrencyNameLeading: true,
                              altUnitNames: [0 : unitName])
    let formatter = CurrencyFormatter.formatter(scope: scope, specs: specs)
    return CurrencyInfo(scope: scope, specs: specs, formatter: formatter)
}

struct PendingRowView_Previews: PreviewProvider {
    static var previews: some View {
        let testInfo = PreviewCurrencyInfo(TESTCURRENCY, digits: 0)
        let demoInfo = PreviewCurrencyInfo(TESTCURRENCY, digits: 2)
        let test = try! Amount(fromString: TESTCURRENCY + ":1.23")
        let demo = try! Amount(fromString: DEMOCURRENCY + ":1234.56")
        List {
            PendingRowView(amount: test, currencyInfo: testInfo, incoming: true)
            PendingRowView(amount: demo, currencyInfo: demoInfo, incoming: false)
        }
    }
}
#endif