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
|
/*
* This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
* See LICENSE.md
*/
import SwiftUI
import taler_swift
// calculate the width of the amountStr (with Font)
// calculate the width of 'content' in compact form
// if it fits side by side, then render HStack(content(compact), Spacer(), amountStr)
// else render VStack(content(wide), HStack(Spacer(), amountStr))
struct AmountRowV<Content: View>: View {
let amountStr: String
let amountColor: Color
let largeAmountFont: Bool
let fitsHorizontal: Bool
let vertAlignment: VerticalAlignment
var content: () -> Content
var body: some View {
if fitsHorizontal {
HStack(alignment: vertAlignment, spacing: 0) {
content()
Spacer(minLength: 0)
Text(amountStr)
.foregroundColor(amountColor)
.accessibilityFont(largeAmountFont ? .title : .title2)
.monospacedDigit()
}
} else {
VStack(alignment: .leading, spacing: 0) {
content()
HStack {
Spacer(minLength: 0)
Text(amountStr)
.foregroundColor(amountColor)
.accessibilityFont(largeAmountFont ? .title : .title2)
.monospacedDigit()
}
}
}
}
}
// MARK: -
#if DEBUG
struct SectionWithAmountRow: View {
@Environment(\.sizeCategory) var sizeCategory
var body: some View {
let testInfo = PreviewCurrencyInfo(TESTCURRENCY, digits: 0)
let demoInfo = PreviewCurrencyInfo(DEMOCURRENCY, digits: 2)
let test = try! Amount(fromString: TESTCURRENCY + ":1.23")
let demo = try! Amount(fromString: DEMOCURRENCY + ":1234.12")
let testStr = test.string(testInfo)
let demoStr = demo.string(demoInfo)
List {
Section {
AmountRowV(amountStr: demoStr, amountColor: .primary, largeAmountFont: true,
fitsHorizontal: true, vertAlignment: .lastTextBaseline) {
Text("Balance")
.accessibilityFont(.title2)
}
AmountRowV(amountStr: demoStr, amountColor: .primary, largeAmountFont: true,
fitsHorizontal: false, vertAlignment: .lastTextBaseline) {
Text("Balance")
.accessibilityFont(.title2)
}
}
Section {
AmountRowV(amountStr: testStr, amountColor: .secondary, largeAmountFont: false,
fitsHorizontal: true, vertAlignment: .lastTextBaseline) {
Text("Balance")
.accessibilityFont(.title2)
}
AmountRowV(amountStr: testStr, amountColor: .secondary, largeAmountFont: false,
fitsHorizontal: false, vertAlignment: .lastTextBaseline) {
Text("Balance")
.accessibilityFont(.title2)
}
}
}
}
}
#Preview {
SectionWithAmountRow()
}
#endif
|