taler-ios

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

DebugViewC.swift (9980B)


      1 /* MIT License
      2  * Copyright (c) 2023 Marc Stibane
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a copy
      5  * of this software and associated documentation files (the "Software"), to deal
      6  * in the Software without restriction, including without limitation the rights
      7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8  * copies of the Software, and to permit persons to whom the Software is
      9  * furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice shall be included in all
     12  * copies or substantial portions of the Software.
     13  *
     14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     20  * SOFTWARE.
     21  */
     22 import SwiftUI
     23 import SymLog
     24 import os.log
     25 
     26 // Numbering Scheme for Views
     27 // MARK: - Main View
     28 public let VIEW_EMPTY_WALLET = 10                                   // 10 WalletEmptyView
     29 public let VIEW_BALANCES = VIEW_EMPTY_WALLET + 1                    // 11 BalancesListView
     30 public let VIEW_SETTINGS = VIEW_BALANCES + 1                        // 12 SettingsView
     31 public let VIEW_ABOUT = VIEW_SETTINGS + 1                           // 13 AboutView
     32 public let VIEW_PAYMENT_SERVICES = VIEW_ABOUT + 1                   // 14 ExchangeListView
     33 public let VIEW_BANK_ACCOUNTS = VIEW_PAYMENT_SERVICES + 1           // 15 BankListView
     34 public let VIEW_BACKUP = VIEW_BANK_ACCOUNTS + 1                     // 16 BackupView
     35 public let VIEW_CAROUSEL = VIEW_BACKUP + 1                          // 17 CarouselView
     36 
     37 // MARK: Transactions
     38 public let VIEW_EMPTY_HISTORY = VIEW_EMPTY_WALLET + 10              // 20 TransactionsEmptyView
     39 public let VIEW_TRANSACTIONLIST = VIEW_EMPTY_HISTORY + 1            // 21 TransactionsListView
     40 public let VIEW_TRANSACTIONSUMMARY = VIEW_TRANSACTIONLIST + 1       // 22 TransactionSummary
     41 public let VIEW_TRANSACTIONDETAIL = VIEW_TRANSACTIONSUMMARY + 1     // 23 TransactionDetail
     42 
     43 
     44 // MARK: - (Manual) Withdrawal (from Actions)
     45 // receive coins from bank ==> shows payee, IBAN + Purpose/Subject for manual wire transfer
     46 public let VIEW_WITHDRAWAL = VIEW_EMPTY_HISTORY + 10                // 30 WithdrawAmount
     47 public let VIEW_WITHDRAW_TOS = VIEW_WITHDRAWAL + 1                  // 31 WithdrawTOSView
     48 public let VIEW_WITHDRAW_ACCEPT = VIEW_WITHDRAW_TOS + 1             // 32 ManualWithdrawDone
     49 public let VIEW_WITHDRAW_INSTRUCTIONS = VIEW_WITHDRAW_ACCEPT + 1    // 33 ManualDetailsWireV
     50 public let VIEW_WITHDRAW_QRCODES = VIEW_WITHDRAW_INSTRUCTIONS + 1   // 34 QRcodesForPayto
     51 
     52 // MARK: (Manual) Deposit (from Actions)
     53 // send coins back to customer ==> instruct exchange to make the wire transfer to the customer's bank account
     54 public let VIEW_DEPOSIT = VIEW_WITHDRAWAL + 10                      // 40 Deposit Coins
     55 //public let VIEW_DEPOSIT_TOS                                       // - user already accepted the ToS at withdrawal, invoice or receive
     56 public let VIEW_DEPOSIT_ACCEPT = VIEW_DEPOSIT + 2                   // 42
     57 
     58 // MARK: Send (from Actions)
     59 // push debit to another wallet ==> shows QR code to be scanned / link to be sent by mail or messenger
     60 public let VIEW_P2P_SEND = VIEW_DEPOSIT + 10                        // 50 Send Coins
     61 //public let VIEW_SEND_TOS                                          // - user already accepted the ToS at withdrawal, invoice or receive
     62 public let VIEW_P2P_SUBJECT = VIEW_P2P_SEND + 2                     // 52 Send / Request Subject
     63 public let VIEW_P2P_READY = VIEW_P2P_SUBJECT + 1                    // 53 Send / Request Ready
     64 
     65 // MARK: Request (from Actions)
     66 // pull credit from another wallet ==> shows QR code to be scanned / link to be sent by mail or messenger
     67 public let VIEW_P2P_REQUEST = VIEW_P2P_SEND + 10                    // 60 Request Amount
     68 public let VIEW_P2P_TOS = VIEW_P2P_REQUEST + 1                      // 61 Request ToS
     69 
     70 // MARK: Business Invoice (from Actions)
     71 // TBD
     72 //public let VIEW_INVOICE_P2P = VIEW_REQUEST_P2P + 10                 // 70 Invoice Amount
     73 //public let VIEW_INVOICE_TOS = VIEW_INVOICE_P2P + 1                  // 71 Invoice ToS
     74 //public let VIEW_INVOICE_PURPOSE = VIEW_INVOICE_TOS + 1              // 72 Invoice Purpose
     75 
     76 
     77 // MARK: - Bank-Integrated Withdrawal
     78 // openURL (Link or scan QR) ==> obtains coins from bank
     79 public let SHEET_WITHDRAWAL = VIEW_WITHDRAWAL + 100                 // 130 WithdrawURIView
     80 public let SHEET_WITHDRAW_TOS = SHEET_WITHDRAWAL + 1                // 131 WithdrawTOSView
     81 public let SHEET_WITHDRAW_ACCEPT = SHEET_WITHDRAW_TOS + 1           // 132 WithdrawAcceptView
     82 public let SHEET_WITHDRAW_AUTHORIZE = SHEET_WITHDRAW_ACCEPT + 1     // 133 waiting for bank confirmation
     83 
     84 public let SHEET_WITHDRAW_EXCHANGE = SHEET_WITHDRAWAL + 5           // 135 WithdrawExchangeV
     85 
     86 
     87 // MARK: Merchant Payment
     88 // openURL (Link, NFC or scan QR) ==> pays merchant
     89 public let SHEET_PAYMENT = SHEET_WITHDRAWAL + 10                    // 140 Pay Merchant
     90 public let SHEET_PAY_TEMPLATE = SHEET_PAYMENT + 1                   // 141 Pay Merchant Template
     91 public let SHEET_PAY_TEMPL_AMOUNT = SHEET_PAY_TEMPLATE + 1          // 142 Pay Template Amount
     92 public let SHEET_PAY_TEMPL_SUBJECT = SHEET_PAY_TEMPL_AMOUNT + 1     // 143 Pay Template Subject
     93 public let SHEET_PAY_CONFIRM = SHEET_PAY_TEMPL_SUBJECT + 1          // 144 Pay Confirm
     94 
     95 // MARK: Pay Request
     96 // peer pull debit - openURL (Link or scan QR)
     97 public let SHEET_PAY_P2P = SHEET_PAYMENT + 10                       // 150 Pay P2P Request
     98 public let SHEET_PAY_P2P_CONFIRM = SHEET_PAY_P2P + 2                // 152 Pay P2P Confirm
     99 
    100 // MARK: Receive
    101 // peer push credit - openURL (Link or scan QR)
    102 public let SHEET_RCV_P2P = SHEET_PAY_P2P + 10                       // 160 Receive Money
    103 public let SHEET_RCV_P2P_TOS = SHEET_RCV_P2P + 1                    // 161 Receive TOSView
    104 public let SHEET_RCV_P2P_ACCEPT = SHEET_RCV_P2P_TOS + 1             // 162 Receive AcceptView
    105 
    106 // MARK: Refund - Get Coins back from merchant
    107 // openURL (Link, NFC or scan QR) ==> receive coins from merchant
    108 public let SHEET_REFUND = SHEET_RCV_P2P + 10                        // 170 Receive Refunds
    109 
    110 
    111 // MARK: -
    112 struct DebugViewV: View {
    113     private let symLog = SymLogV(0)
    114     @EnvironmentObject private var debugViewC: DebugViewC
    115 
    116     var body: some View {
    117 #if PRINT_CHANGES
    118 //        let _ = Self._printChanges()
    119 //        let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
    120 #endif
    121         let viewIDString = debugViewC.viewID > 0 ? String(debugViewC.viewID)
    122                                                  : EMPTYSTRING
    123         HStack {
    124             Spacer()
    125             Spacer()
    126             if UIScreen.hasNotch {
    127                 Spacer()
    128                 Spacer()
    129             }
    130             Text(viewIDString)
    131                 .foregroundColor(.red)
    132                 .font(.system(size: 11))        // no talerFont
    133                 .monospacedDigit()
    134                 .id("viewID")
    135             Spacer()
    136         }
    137         .accessibilityHidden(true)
    138         .edgesIgnoringSafeArea(.top)
    139     }
    140 }
    141 // MARK: -
    142 class DebugViewC: ObservableObject {
    143     private let symLog = SymLogC(0)                         // 0 to switch off viewID change logging
    144     public static let shared = DebugViewC()
    145 #if DEBUG
    146     @AppStorage("developerMode") var developerMode: Bool = true
    147 #else
    148     @AppStorage("developerMode") var developerMode: Bool = false
    149 #endif
    150     let logger = Logger(subsystem: "net.taler.gnu", category: "DebugView")
    151 
    152     @Published var viewID: Int = 0
    153     @Published var sheetID: Int = 0
    154 
    155 //    func announce(_ this: String) {
    156 //        if UIAccessibility.isVoiceOverRunning {
    157 //            UIAccessibility.post(notification: .announcement, argument: this)
    158 //        }
    159 //    }
    160 
    161     @MainActor func setViewID(_ newID: Int, stack: CallStack) -> Void {
    162         if developerMode {
    163             if viewID == 0 {
    164                 logger.log("switching ON, \(newID, privacy: .public)")
    165                 viewID = newID                              // publish ON
    166 //                announce("Current view is: \(newID).")
    167             } else if viewID != newID {
    168                 logger.log("switching from \(self.viewID, privacy: .public) to \(newID, privacy: .public)")
    169 //                announce("View switched from \(self.viewID) to \(newID).")
    170                 viewID = newID                              // publish new viewID
    171             } else {
    172                 logger.log("\(newID, privacy: .public) stays")
    173                 // don't set viewID to the same value, it would just trigger an unneccessary redraw
    174             }
    175         } else {
    176             logger.log("view \(newID, privacy: .public)")
    177             if viewID > 0 {
    178                 viewID = 0                                      // publish OFF
    179             }
    180         }
    181     }
    182     
    183     @MainActor func setSheetID(_ newID: Int) -> Void {
    184         if developerMode {
    185             if sheetID != newID {
    186                 logger.log("sheet switching from \(self.sheetID, privacy: .public) to \(newID, privacy: .public)")
    187                 sheetID = newID                             // publish new sheetID
    188             } else {
    189                 logger.log("sheet \(newID, privacy: .public) stays")
    190                 // don't set sheetID to the same value, it would just trigger an unneccessary redraw
    191             }
    192         } else {
    193             logger.log("sheet \(newID, privacy: .public)")
    194             if sheetID > 0 {
    195                 // might happen after switching DevMode off, if sheetID still has the old value of the last sheet
    196                 sheetID = 0                                     // publish OFF
    197             }
    198         }
    199     }
    200 }