summaryrefslogtreecommitdiff
path: root/TalerWallet1/Views/Balances/BalanceRowView.swift
blob: 9c36bbd8b6c782c9eb07db24a7f7653277435047 (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
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import SwiftUI
import taler_swift

struct BalanceButton: View {
    let amountStr: String
    let sizeCategory: ContentSizeCategory
    let rowAction: () -> Void

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

    var body: some View {
        let balanceTitle = String(localized: "Balance:", comment: "Main view")
        Button(action: rowAction) {
            SingleAxisGeometryReader { width in
                Group {
                    let titles = [(balanceTitle, TalerFont.uiFont(.title2)),
                                  (amountStr,    TalerFont.uiFont(.title))]
                    let needVStack = !iconOnly && Self.needVStack(titles, width: width,
                                                                  sizeCategory: sizeCategory,
                                                                  padding: 20, sameSize: false)
                    AmountRowV(amountStr: amountStr, largeAmountFont: true, fitsHorizontal: !needVStack) {
                        Text(iconOnly ? "" : balanceTitle)
                            .accessibilityFont(.title2)
                            .foregroundColor(.secondary)
                    }
                }
            }
        }   .disabled(false)
            .buttonStyle(TalerButtonStyle(type: iconOnly ? .plain : .balance, aligned: .trailing))
            .accessibilityElement(children: /*@START_MENU_TOKEN@*/.ignore/*@END_MENU_TOKEN@*/)
            .accessibilityLabel("\(balanceTitle) \(amountStr)")    // TODO: CurrencyFormatter!
    }
}


/// This view shows the currency row in a currency section
/// [Send Coins]  [Receive Coins]     Balance
struct BalanceRowView: View {
    let amount: Amount
    let currencyInfo: CurrencyInfo?
    let sendAction: () -> Void
    let recvAction: () -> Void
    let rowAction: () -> Void
    @Environment(\.sizeCategory) var sizeCategory
    @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic

    let sendTitle1 = String(localized: "Send", comment: "Top of button <Send Money>")
    let sendTitle2 = String(localized: "Money", comment: "Bottom of button <Send Money>")
    let requestTitle1 = String(localized: "Request", comment: "Top of button <Request Payment>")
    let requestTitle2 = String(localized: "Payment", comment: "Bottom of button <Request Payment>")

    var body: some View {
        let correctForSize = Self.correctForSize(sizeCategory)
        SingleAxisGeometryReader { width in
            VStack (alignment: .trailing) {
                let amountStr = amount.string(currencyInfo)
                BalanceButton(amountStr: amountStr,
                              sizeCategory: sizeCategory,
                              rowAction: rowAction)
                let uiFont = TalerFont.uiFont(.title3)
                let titles = [(requestTitle1, uiFont), (requestTitle2, uiFont), (sendTitle1, uiFont), (sendTitle2, uiFont)]
                let needVStack = Self.needVStack(titles, width: width, sizeCategory: sizeCategory, padding: 10)
                let twoRowButtons = TwoRowButtons(sendTitles: (sendTitle1, sendTitle2),
                                                  recvTitles: (requestTitle1, requestTitle2),
                                                  horizontal: needVStack,
                                                   lineLimit: 5, sendDisabled: amount.isZero,
                                                  sendAction: sendAction,
                                                  recvAction: recvAction)
                if needVStack {
                    VStack { twoRowButtons }
                } else {
                    HStack(spacing: HSPACING) { twoRowButtons }
                }
            }
        }
    }
}
// MARK: -
#if  DEBUG

struct SomeBalanceRows: View {
    var body: 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 + ":123.12")
//        let testStr = test.string(testInfo)
//        let demoStr = demo.string(demoInfo)

        List {
            Section {
                BalanceRowView(amount: demo, currencyInfo: demoInfo, sendAction: {}, recvAction: {}, rowAction: {})
            }
            BalanceRowView(amount: test, currencyInfo: testInfo, sendAction: {}, recvAction: {}, rowAction: {})
        }

    }
}

struct BalanceRowView_Previews: PreviewProvider {
    static var previews: some View {
        SomeBalanceRows()
//            .environment(\.sizeCategory, .extraExtraLarge)    Canvas Device Settings
    }
}
#endif