aboutsummaryrefslogtreecommitdiff
path: root/TalerWallet1/Views/HelperViews/Buttons.swift
blob: 945c86e80d9cacf45030481b15ed68b2e541b15c (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import SwiftUI
import Foundation

extension ShapeStyle where Self == Color {
    static var random: Color {
        Color(
            red: .random(in: 0...1),
            green: .random(in: 0...1),
            blue: .random(in: 0...1)
        )
    }
}

struct HamburgerButton : View  {
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            Image(systemName: "line.3.horizontal")
//            Image(systemName: "sidebar.squares.leading")
        }
        .accessibilityFont(.title)
        .accessibilityLabel("Main Menu")
    }
}

struct QRButton : View  {
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            Image(systemName: "qrcode.viewfinder")
        }
        .accessibilityFont(.title)
        .accessibilityLabel("Scan QR codes")
    }
}

struct PlusButton : View  {
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            Image(systemName: "plus")
        }
        .accessibilityFont(.title)
        .accessibilityLabel("Add...")
    }
}

struct ArrowUpButton : View  {
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            Image(systemName: "arrow.up.to.line")
        }
        .accessibilityFont(.title2)
        .accessibilityLabel("Scroll up")
    }
}

struct ArrowDownButton : View  {
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            Image(systemName: "arrow.down.to.line")
        }
        .accessibilityFont(.title2)
        .accessibilityLabel("Scroll down")
    }
}

struct ReloadButton : View  {
    let disabled: Bool
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            Image(systemName: "arrow.clockwise")
        }
        .accessibilityFont(.title)
        .accessibilityLabel("Reload")
        .disabled(disabled)
    }
}

struct TalerButtonStyle: ButtonStyle {
    @Environment(\.isEnabled) private var isEnabled: Bool
    func disabled() -> Bool { !isEnabled }

    enum TalerButtonStyleType {
        case plain
        case balance
        case bordered
        case prominent
    }
    var type: TalerButtonStyleType = .plain
    var dimmed: Bool = false
    var narrow: Bool = false
    var aligned: TextAlignment = .center

    public func makeBody(configuration: ButtonStyle.Configuration) -> some View {
            MyBigButton(//type: type,
                   foreColor: foreColor(type: type, pressed: configuration.isPressed),
                   backColor: backColor(type: type, pressed: configuration.isPressed),
                      dimmed: dimmed,
               configuration: configuration,
                    disabled: disabled(),
                      narrow: narrow,
                     aligned: aligned)
    }

    func foreColor(type: TalerButtonStyleType, pressed: Bool) -> Color {
//        return if type == .plain {
//            WalletColors().fieldForeground
//        } else {
//            WalletColors().buttonForeColor(pressed: pressed,
//                                          disabled: disabled(),
//                                         prominent: type == .prominent)
//        }
        return type == .plain ? WalletColors().fieldForeground :      // primary text color
            WalletColors().buttonForeColor(pressed: pressed,
                                          disabled: disabled(),
                                         prominent: type == .prominent,
                                           balance: type == .balance)
    }
    func backColor(type: TalerButtonStyleType, pressed: Bool) -> Color {
//        return if type == .plain {
//            Color.clear
//        } else {
//            WalletColors().buttonBackColor(pressed: pressed,
//                                           disabled: disabled(),
//                                           prominent: type == .prominent)
//        }
        return type == .plain && !pressed ? Color.clear :
            WalletColors().buttonBackColor(pressed: pressed,
                                          disabled: disabled(),
                                         prominent: type == .prominent,
                                           balance: type == .balance)
    }

    struct BackgroundView: View {
        let color: Color
        let dimmed: Bool
        var body: some View {
            RoundedRectangle(
                cornerRadius: 15,
                style: .continuous
            )
            .fill(color)
            .opacity(dimmed ? 0.6 : 1.0)
        }
    }

    struct MyBigButton: View {
//        var type: TalerButtonStyleType
        let foreColor: Color
        let backColor: Color
        let dimmed: Bool
        let configuration: ButtonStyle.Configuration
        let disabled: Bool
        let narrow: Bool
        let aligned: TextAlignment

        var body: some View {
            let aligned2: Alignment = (aligned == .center) ? Alignment.center
                                    : (aligned == .leading) ? Alignment.leading
                                    : Alignment.trailing
            configuration.label
                .multilineTextAlignment(aligned)
                .accessibilityFont(.title3)
//                                  narrow ? .title3 : .title2
                .frame(minWidth: 0, maxWidth: narrow ? nil : .infinity, alignment: aligned2)
                .padding(.vertical, 10)
                .padding(.horizontal, 6)
                .foregroundColor(foreColor)
                .background(BackgroundView(color: backColor, dimmed: dimmed))
                .contentShape(Rectangle())      // make sure the button can be pressed even if backgroundColor == clear
                .scaleEffect(configuration.isPressed ? 0.95 : 1)
                .animation(.spring(response: 0.1), value: configuration.isPressed)
                .disabled(disabled)
        }
    }
}


struct Buttons_Previews: PreviewProvider {
    static var previews: some View {
        VStack {
            HStack {
                Button("1000") {
                    Controller.shared.playSound(1000)
                }.padding()
                Button("1013") {
                    Controller.shared.playSound(1013)
                }.padding()
                Button("1008") {
                    Controller.shared.playSound(1008)
                }.padding()
                Button("1001") {
                    Controller.shared.playSound(1001)
                }.padding()
                Button("1018") {
                    Controller.shared.playSound(1018)
                }.padding()
            } // Single
            HStack {
                Button("1003") {
                    Controller.shared.playSound(1003)
                }.padding()
                Button("1004") {
                    Controller.shared.playSound(1004)
                }.padding()
                Button("1016") {
                    Controller.shared.playSound(1016)
                }.padding()
                Button("1022") {
                    Controller.shared.playSound(1022)
                }.padding()
                Button("1034") {
                    Controller.shared.playSound(1034)
                }.padding()
            } // Double
            HStack {
                Button("1023") {
                    Controller.shared.playSound(1023)
                }.padding()
                Button("1029") {
                    Controller.shared.playSound(1029)
                }.padding()
                Button("1014") {
                    Controller.shared.playSound(1014)
                }.padding()
                Button("1021") {
                    Controller.shared.playSound(1021)
                }.padding()
                Button("1031") {
                    Controller.shared.playSound(1031)
                }.padding()
            } // long single
            HStack {
                Button("1002") {
                    Controller.shared.playSound(1002) // == 7,12,15
                }.padding()
                Button("1006") {
                    Controller.shared.playSound(1006)
                }.padding()
                Button("1025") {
                    Controller.shared.playSound(1025)
                }.padding()
                Button("1026") {
                    Controller.shared.playSound(1026)
                }.padding()
                Button("1033") {
                    Controller.shared.playSound(1033)
                }.padding()
            } // Triple
            HStack {
                Button("1005") {
                    Controller.shared.playSound(1005) // 17
                }.padding()
                Button("1009") {
                    Controller.shared.playSound(1009)
                }.padding()
                Button("1010") {
                    Controller.shared.playSound(1010)
                }.padding()
            } // Double 2
            HStack {
                Button("1030") {
                    Controller.shared.playSound(1030)
                }.padding()
                Button("1035") {
                    Controller.shared.playSound(1035)
                }.padding()
                Button("1036") {
                    Controller.shared.playSound(1036)
                }.padding()
                Button("1027") {
                    Controller.shared.playSound(1027)
                }.padding()
            }
            HStack {
                Button("1020") {
                    Controller.shared.playSound(1020)
                }.padding()
                Button("1024") {
                    Controller.shared.playSound(1024)
                }.padding()
                Button("1032") {
                    Controller.shared.playSound(1032)
                }.padding()
                Button("1028") {
                    Controller.shared.playSound(1028)
                }.padding()
            }
            HStack {
                PlusButton() {
                }.padding()
                HamburgerButton() {
                }.padding()
                QRButton() {
                }.padding()
                ReloadButton(disabled: false) {
                }.padding()
                ReloadButton(disabled: true) {
                }.padding()
            }
            Button(String(localized: "Accept"), action: {
                Controller.shared.playSound(1028)
            })
                .buttonStyle(TalerButtonStyle(type: .prominent))
                .padding(.horizontal)
        }
    }
}

#if DEBUG
//fileprivate struct ContentView: View {
//    @State var isOn = false
//    //The better route is to have a separate variable to control the animations
//    // This prevents unpleasant side-effects.
//    @State private var animate = false
//
//    var body: some View {
//        VStack {
//            Text(verbatim: "I don't change.")
//                .padding()
//            Button("Press me, I do change") {
//                isOn.toggle()
//                animate = false
//                // Because .opacity is animated, we need to switch it
//                // back so the button shows.
//                DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
//                    animate = true
//                }
//            }
//            // In this case I chose to animate .opacity
//            .opacity(animate ? 1 : 0)
//            .animation(.easeIn, value: animate)
//            .frame(width: 300, height: 400)
//            // If you want the button to animate when the view appears, you need to change the value
//            .onAppear { animate = true }
//        }
//    }
//}
//fileprivate struct ContentView_Previews: PreviewProvider {
//    static var previews: some View {
//        ContentView()
//    }
//}
#endif