taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

Buttons.swift (15168B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-25 Taler Systems S.A.
      3  * See LICENSE.md
      4  */
      5 /**
      6  * @author Marc Stibane
      7  */
      8 import SwiftUI
      9 import Foundation
     10 import AVFoundation
     11 
     12 extension ShapeStyle where Self == Color {
     13     static var random: Color {
     14         Color(
     15             red: .random(in: 0...1),
     16             green: .random(in: 0...1),
     17             blue: .random(in: 0...1)
     18         )
     19     }
     20 }
     21 
     22 struct HamburgerButton : View  {
     23     let action: () -> Void
     24 
     25     var body: some View {
     26         Button(action: action) {
     27             Image(systemName: HAMBURGER)
     28 //            Image(systemName: "sidebar.squares.leading")      // 􀱦
     29         }
     30         .talerFont(.title)
     31         .accessibilityLabel(Text("Main Menu", comment: "a11y"))
     32     }
     33 }
     34 
     35 struct LinkButton: View {
     36     let destination: URL
     37     let hintTitle: String
     38     let buttonTitle: String
     39     let a11yHint: String
     40     let badge: String
     41 
     42     @AppStorage("minimalistic") var minimalistic: Bool = false
     43 
     44     var body: some View {
     45         VStack(alignment: .leading) {
     46             if !minimalistic {      // show hint that the user should authorize on bank website
     47                 Text(hintTitle)
     48                     .fixedSize(horizontal: false, vertical: true)       // wrap in scrollview
     49                     .multilineTextAlignment(.leading)                   // otherwise
     50                     .listRowSeparator(.hidden)
     51             }
     52             Link(destination: destination) {
     53                 HStack(spacing: 8.0) {
     54                     Image(systemName: LINK)
     55                     Text(buttonTitle)
     56                 }
     57             }
     58             .buttonStyle(TalerButtonStyle(type: .prominent, badge: badge))
     59             .accessibilityHint(a11yHint)
     60         }
     61     }
     62 }
     63 
     64 struct QRButton : View  {
     65     let hideTitle: Bool
     66     let action: () -> Void
     67 
     68     @AppStorage("minimalistic") var minimalistic: Bool = false
     69     @State private var showCameraAlert: Bool = false
     70 
     71     private var openSettingsButton: some View {
     72         Button("Open Settings") {
     73             showCameraAlert = false
     74             UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
     75         }
     76     }
     77     let closingAnnouncement = String(localized: "Closing Camera", comment: "a11y")
     78 
     79     var defaultPriorityAnnouncement = String(localized: "Opening Camera", comment: "a11y")
     80 
     81     var highPriorityAnnouncement: AttributedString {
     82         var highPriorityString = AttributedString(localized: "Camera Active", comment: "a11y")
     83         if #available(iOS 17.0, *) {
     84             highPriorityString.accessibilitySpeechAnnouncementPriority = .high
     85         }
     86         return highPriorityString
     87     }
     88     @MainActor
     89     private func checkCameraAvailable() -> Void {
     90         // Open Camera when QR-Button was tapped
     91         announce(defaultPriorityAnnouncement)
     92 
     93         AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) -> Void in
     94             if granted {
     95                 action()
     96                 if #available(iOS 17.0, *) {
     97                     AccessibilityNotification.Announcement(highPriorityAnnouncement).post()
     98                 } else {
     99                     let cameraActive = String(localized: "Camera Active", comment: "a11y")
    100                     announce(cameraActive)
    101                 }
    102             } else {
    103                 showCameraAlert = true
    104             }
    105         })
    106     }
    107 
    108     var body: some View {
    109         let dismissAlertButton = Button("Cancel", role: .cancel) {
    110             announce(closingAnnouncement)
    111             showCameraAlert = false
    112         }
    113         let scanText = String(localized: "Scan QR code", comment: "Button title, a11y")
    114         let qrImage = Image(systemName: QRBUTTON)
    115         let qrText = Text(qrImage)
    116         Button(action: checkCameraAvailable) {
    117             if hideTitle {
    118                 qrImage
    119                     .resizable()
    120                     .scaledToFit()
    121                     .foregroundStyle(Color.accentColor)
    122             } else if minimalistic {
    123                 let width = UIScreen.screenWidth / 7
    124                 qrText.talerFont(.largeTitle)
    125                     .padding(.horizontal, width)
    126             } else {
    127                 HStack(spacing: 16) {
    128                     qrText.talerFont(.title)
    129                     Text(scanText)
    130                 }.padding(.horizontal)
    131             }
    132         }
    133         .accessibilityLabel(scanText)
    134         .alert("Scanning QR-codes requires access to the camera",
    135                isPresented: $showCameraAlert,
    136                    actions: {   openSettingsButton
    137                                 dismissAlertButton },
    138                    message: {   Text("Please allow camera access in Settings.") }) // Scanning QR-codes
    139     }
    140 }
    141 
    142 struct DoneButton : View  {
    143     let titleStr: String?
    144     let accessibilityLabelStr: String
    145     let action: () -> Void
    146 
    147     var body: some View {
    148         Button(action: action) {
    149             if let titleStr {
    150                 Text(titleStr)
    151                     .tint(.accentColor)
    152             } else {
    153                 Image(systemName: CHECKMARK)    // 􀆅
    154             }
    155         }
    156         .tint(.accentColor)
    157         .talerFont(.title)
    158         .accessibilityLabel(accessibilityLabelStr)
    159     }
    160 }
    161 
    162 struct PlusButton : View  {
    163     let accessibilityLabelStr: String
    164     let action: () -> Void
    165 
    166     var body: some View {
    167         Button(action: action) {
    168             Image(systemName: PLUS)
    169         }
    170         .tint(.accentColor)
    171         .talerFont(.title)
    172         .accessibilityLabel(accessibilityLabelStr)
    173     }
    174 }
    175 
    176 struct SettingsButton : View  {
    177     let accessibilityLabelStr: String
    178     let action: () -> Void
    179 
    180     var body: some View {
    181         let button = Button(action: action) {
    182             Image(systemName: SETTINGS)
    183         }
    184         .tint(.accentColor)
    185         .talerFont(.title)
    186         .accessibilityLabel(accessibilityLabelStr)
    187 //        if #available(iOS 26.0, *) {
    188 //            return button.buttonStyle(.glass)
    189 //        }
    190         return button
    191     }
    192 }
    193 struct PasteButton : View  {
    194     let accessibilityLabelStr: String
    195     let action: () -> Void
    196 
    197     var body: some View {
    198         let button = Button(action: action) {
    199             Image(systemName: PASTE)
    200         }
    201             .tint(.accentColor)
    202             .talerFont(.title)
    203             .accessibilityLabel(accessibilityLabelStr)
    204 //        if #available(iOS 26.0, *) {
    205 //            return button.buttonStyle(.glass)
    206 //        }
    207         return button
    208     }
    209 }
    210 
    211 struct ZoomInButton : View  {
    212     let accessibilityLabelStr: String
    213     let action: () -> Void
    214 
    215     var body: some View {
    216         Button(action: action) {
    217             let name = ICONNAME_ZOOM_IN
    218             if UIImage(named: name) != nil {
    219                 Image(name)
    220             } else {
    221                 Image(systemName: SYSTEM_ZOOM_IN)
    222             }
    223         }
    224         .tint(.accentColor)
    225         .talerFont(.title)
    226         .accessibilityLabel(accessibilityLabelStr)
    227     }
    228 }
    229 
    230 struct ZoomOutButton : View  {
    231     let accessibilityLabelStr: String
    232     let action: () -> Void
    233 
    234     var body: some View {
    235         Button(action: action) {
    236             let name = ICONNAME_ZOOM_OUT
    237             if UIImage(named: name) != nil {
    238                 Image(name)
    239             } else {
    240                 Image(systemName: SYSTEM_ZOOM_OUT)
    241             }
    242         }
    243         .tint(.accentColor)
    244         .talerFont(.title)
    245         .accessibilityLabel(accessibilityLabelStr)
    246     }
    247 }
    248 
    249 struct BackButton : View  {
    250     let action: () -> Void
    251 
    252     var body: some View {
    253         Button(action: action) {
    254             let name = ICONNAME_INCOMING + ICONNAME_FILL
    255             let sysName = SYSTEM_INCOMING4 + ICONNAME_FILL
    256             if UIImage(named: name) != nil {
    257                 Image(name)
    258             } else if UIImage(systemName: sysName) != nil {
    259                 Image(systemName: sysName)
    260             } else {
    261                 // TODO: ultralight vs black
    262                 Image(systemName: FALLBACK_INCOMING)
    263             }
    264         }
    265         .tint(.accentColor)
    266         .talerFont(.largeTitle)
    267         .accessibilityLabel(Text("Back", comment: "a11y"))
    268     }
    269 }
    270 
    271 struct ForwardButton : View  {
    272     let enabled: Bool
    273     let action: () -> Void
    274 
    275     var body: some View {
    276         let myAction = {
    277             if enabled {
    278                 action()
    279             }
    280         }
    281         Button(action: myAction) {
    282             let imageName = enabled ? ICONNAME_OUTGOING + ICONNAME_FILL
    283                                     : ICONNAME_OUTGOING
    284             let sysName   = enabled ? SYSTEM_OUTGOING4 + ICONNAME_FILL
    285                                     : SYSTEM_OUTGOING4
    286             if UIImage(named: imageName) != nil {
    287                 Image(imageName)
    288             } else if UIImage(systemName: sysName) != nil {
    289                 Image(systemName: sysName)
    290             } else {
    291                 // TODO: ultralight vs black
    292                 Image(systemName: FALLBACK_OUTGOING)
    293             }
    294         }
    295         .tint(.accentColor)
    296         .talerFont(.largeTitle)
    297         .accessibilityLabel(Text("Continue", comment: "a11y"))
    298     }
    299 }
    300 
    301 struct ArrowUpButton : View  {
    302     let action: () -> Void
    303 
    304     var body: some View {
    305         Button(action: action) {
    306             Image(systemName: ARROW_TOP)       // 􀄿
    307         }
    308         .tint(.accentColor)
    309         .talerFont(.title2)
    310         .accessibilityLabel(Text("Scroll up", comment: "a11y"))
    311     }
    312 }
    313 
    314 struct ArrowDownButton : View  {
    315     let action: () -> Void
    316 
    317     var body: some View {
    318         Button(action: action) {
    319             Image(systemName: ARROW_BOT)        // 􀅀
    320         }
    321         .tint(.accentColor)
    322         .talerFont(.title2)
    323         .accessibilityLabel(Text("Scroll down", comment: "a11y"))
    324     }
    325 }
    326 
    327 struct ReloadButton : View  {
    328     let disabled: Bool
    329     let action: () -> Void
    330 
    331     var body: some View {
    332         Button(action: action) {
    333             Image(systemName: RELOAD)           // 􀅈
    334         }
    335         .tint(.accentColor)
    336         .talerFont(.title)
    337         .accessibilityLabel(Text("Reload", comment: "a11y"))
    338         .disabled(disabled)
    339     }
    340 }
    341 
    342 enum TalerButtonStyleType {
    343     case plain
    344     case bordered
    345     case prominent
    346 }
    347 struct TalerButtonStyle: ButtonStyle {
    348     var type: TalerButtonStyleType = .plain
    349     var dimmed: Bool = false
    350     var narrow: Bool = false
    351     var disabled: Bool = false
    352     var aligned: TextAlignment = .center
    353     var badge: String = EMPTYSTRING
    354 
    355     @Environment(\.colorScheme) private var colorScheme
    356     @Environment(\.colorSchemeContrast) private var colorSchemeContrast
    357 
    358     public func makeBody(configuration: ButtonStyleConfiguration) -> some View {
    359         //        configuration.role = type == .prominent ? .primary : .normal          Only on macOS
    360         MyBigButton(foreColor: foreColor(type: type, pressed: configuration.isPressed,
    361                                        scheme: colorScheme, contrast: colorSchemeContrast, disabled: disabled),
    362                     backColor: backColor(type: type, pressed: configuration.isPressed, disabled: disabled),
    363                        dimmed: dimmed,
    364                 configuration: configuration,
    365                      disabled: disabled,
    366                        narrow: narrow,
    367                       aligned: aligned,
    368                         badge: badge,
    369                        scheme: colorScheme,
    370                      contrast: colorSchemeContrast)
    371     }
    372 
    373     func foreColor(type: TalerButtonStyleType,
    374                 pressed: Bool,
    375                  scheme: ColorScheme,
    376                contrast: ColorSchemeContrast,
    377                disabled: Bool) -> Color {
    378         if type == .plain {
    379             return WalletColors().fieldForeground      // primary text color
    380         }
    381         return WalletColors().buttonForeColor(pressed: pressed,
    382                                              disabled: disabled,
    383                                                scheme: scheme,
    384                                              contrast: contrast,
    385                                             prominent: type == .prominent)
    386     }
    387     func backColor(type: TalerButtonStyleType, pressed: Bool, disabled: Bool) -> Color {
    388         if type == .plain && !pressed {
    389             return Color.clear
    390         }
    391         return WalletColors().buttonBackColor(pressed: pressed,
    392                                              disabled: disabled,
    393                                             prominent: type == .prominent)
    394     }
    395 
    396     struct BackgroundView: View {
    397         let color: Color
    398         let dimmed: Bool
    399         var body: some View {
    400             RoundedRectangle(
    401                 cornerRadius: 15,
    402                 style: .continuous
    403             )
    404             .fill(color)
    405             .opacity(dimmed ? 0.6 : 1.0)
    406         }
    407     }
    408 
    409     struct MyBigButton: View {
    410 //        var type: TalerButtonStyleType
    411         let foreColor: Color
    412         let backColor: Color
    413         let dimmed: Bool
    414         let configuration: ButtonStyle.Configuration
    415         let disabled: Bool
    416         let narrow: Bool
    417         let aligned: TextAlignment
    418         var badge: String
    419         var scheme: ColorScheme
    420         var contrast: ColorSchemeContrast
    421 
    422         var body: some View {
    423             let aligned2: Alignment = (aligned == .center) ? Alignment.center
    424                                     : (aligned == .leading) ? Alignment.leading
    425                                     : Alignment.trailing
    426             let hasBadge = !badge.isEmpty
    427             let buttonLabel = configuration.label
    428                                 .multilineTextAlignment(aligned)
    429                                 .talerFont(.title3)         //   narrow ? .title3 : .title2
    430                                 .frame(maxWidth: narrow ? nil : .infinity, alignment: aligned2)
    431                                 .padding(.vertical, 10)
    432                                 .padding(.horizontal, hasBadge ? 0 : 6)
    433                                 .foregroundColor(foreColor)
    434                                 .background(BackgroundView(color: backColor, dimmed: dimmed))
    435                                 .contentShape(Rectangle())      // make sure the button can be pressed even if backgroundColor == clear
    436                                 .scaleEffect(configuration.isPressed ? 0.95 : 1)
    437                                 .animation(.spring(response: 0.1), value: configuration.isPressed)
    438                                 .disabled(disabled)
    439             if hasBadge {
    440                 let badgeColor: Color = (badge == CONFIRM_BANK) ? WalletColors().confirm
    441                                                                 : WalletColors().attention
    442                 let badgeV = Image(systemName: badge)
    443                                 .talerFont(.caption)
    444                 HStack(alignment: .top, spacing: 0) {
    445                     badgeV.foregroundColor(.clear)
    446                     buttonLabel
    447                     badgeV.foregroundColor(badgeColor)
    448                 }
    449             } else {
    450                 buttonLabel
    451             }
    452         }
    453     }
    454 }
    455 // MARK: -
    456 #if DEBUG
    457 fileprivate struct ContentView_Previews: PreviewProvider {
    458     static var previews: some View {
    459         let testButtonTitle = String("Placeholder")
    460         Button(testButtonTitle) {}
    461             .buttonStyle(TalerButtonStyle(type: .bordered, aligned: .trailing))
    462     }
    463 }
    464 #endif