taler-ios

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

TransactionButton.swift (3928B)


      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 taler_swift
     10 import AVFoundation
     11 
     12 struct WarningButton: View {
     13     let warningText: String?
     14     let buttonTitle: String
     15     let buttonIcon: String?
     16     let role: ButtonRole?
     17     @Binding var disabled: Bool
     18     let action: () -> Void
     19 
     20     @AppStorage("shouldShowWarning") var shouldShowWarning: Bool = true
     21     @State private var showAlert: Bool = false
     22 
     23     var body: some View {
     24         Button(//role: role,
     25              action: {
     26                 if !disabled {
     27                     if shouldShowWarning && (role == .destructive || role == .cancel) {
     28                         showAlert = true
     29                     } else {
     30                         action()
     31                     }
     32                 }
     33         }) {
     34             HStack(spacing: 20) {
     35                 if let buttonIcon {
     36                     Image(systemName: buttonIcon)
     37                 }
     38                 Text(buttonTitle)
     39             }
     40             .frame(maxWidth: .infinity)
     41             .foregroundColor(role == .destructive ? WalletColors().errorColor
     42                                                   : .accentColor)
     43         }
     44         .talerFont(.title1)
     45         .buttonStyle(.bordered)
     46         .controlSize(.large)
     47         .disabled(disabled)
     48         .alert(warningText ?? EMPTYSTRING, isPresented: $showAlert, actions: {
     49                 Button("Cancel", role: .cancel) {
     50                     showAlert = false
     51                 }
     52                 Button(buttonTitle) {
     53                     showAlert = false
     54                     action()
     55                 }
     56             }, message: { Text("This operation cannot be undone") }
     57         )
     58     }
     59 }
     60 // MARK: -
     61 struct TransactionButton: View {
     62     let transactionId: String
     63     let command: TxAction
     64     let warning: String?
     65     @Binding var didExecute: Bool
     66     let action: (_ transactionId: String, _ viewHandles: Bool) async throws -> Void
     67 
     68     @State private var disabled: Bool = false
     69     @State private var executed: Bool = false
     70     @State private var buttonTitle: String = EMPTYSTRING
     71 
     72     @MainActor
     73     private func doAction() {
     74         disabled = true     // don't try this more than once
     75         Task { // runs on MainActor
     76             if let _ = try? await action(transactionId, false) {
     77 //                symLog.log("\(executed) \(transactionId)")
     78                 executed = true             // change button text
     79                 didExecute = true
     80             }
     81         }
     82     }
     83 
     84     var body: some View {
     85         let isDestructive = (command == .delete) || (command == .fail)
     86         let isCancel = (command == .abort)
     87         let role: ButtonRole? = isDestructive ? .destructive
     88                               : isCancel      ? .cancel
     89                                               : nil
     90         let buttonTitle = executed ? command.localizedActionExecuted
     91                                    : command.localizedActionTitle
     92         WarningButton(warningText: warning,
     93                       buttonTitle: buttonTitle,
     94                        buttonIcon: command.localizedActionImage,
     95                              role: role,                                        // TODO: WalletColors().errorColor
     96                          disabled: $disabled,
     97                            action: doAction)
     98     }
     99 }
    100 // MARK: -
    101 #if DEBUG
    102 //struct TransactionButton_Previews: PreviewProvider {
    103 //
    104 //    static func action(_ transactionId: String, _ viewHandles: Bool) async throws {
    105 //        print(transactionId)
    106 //    }
    107 //
    108 //    static var previews: some View {
    109 //        List {
    110 //            TransactionButton(transactionId: "Button pressed", command: .abort,
    111 //                              warning: "Are you sure you want to abort this transaction?",
    112 //                              didExecute: <#Binding<Bool>#>,
    113 //                              action: action)
    114 //        }
    115 //    }
    116 //}
    117 #endif