taler-ios

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

DebugSettingsView.swift (15594B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-26 Taler Systems S.A.
      3  * See LICENSE.md
      4  */
      5 /**
      6  * @author Marc Stibane
      7  */
      8 import SwiftUI
      9 import taler_swift
     10 import SymLog
     11 import LocalConsole
     12 
     13 struct DebugSettingsView: View {
     14     private let symLog = SymLogV(0)
     15     let stack: CallStack
     16     let navTitle: String
     17 
     18     @EnvironmentObject private var controller: Controller
     19     @EnvironmentObject private var model: WalletModel
     20 //    @Environment(\.colorSchemeContrast) private var colorSchemeContrast
     21 #if DEBUG
     22     @AppStorage("developerMode") var developerMode: Bool = true
     23 #else
     24     @AppStorage("developerMode") var developerMode: Bool = false
     25 #endif
     26     @AppStorage("logTransactions") var logTransactions: Bool = false
     27     @AppStorage("useHaptics") var useHaptics: Bool = true
     28     @AppStorage("developDelay") var developDelay: Bool = false
     29     @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic
     30     @AppStorage("minimalistic") var minimalistic: Bool = false
     31     @AppStorage("localConsoleL") var localConsoleL: Bool = false                // for Logs
     32     @AppStorage("localConsoleO") var localConsoleO: Int = 0                     // for Observability
     33     @AppStorage("debugViews") var debugViews: Bool = false
     34 
     35     @State private var checkDisabled = false
     36     @State private var withDrawDisabled = false
     37     @State private var showDevelopItems = false
     38     @State private var showResetAlert: Bool = false
     39     @State private var didReset: Bool = false
     40 
     41     private var dismissAlertButton: some View {
     42         Button("Cancel", role: .cancel) {
     43             showResetAlert = false
     44         }
     45     }
     46     private var resetButton: some View {
     47         Button("Reset", role: .destructive) {                                   // TODO: WalletColors().errorColor
     48             didReset = true
     49             showResetAlert = false
     50             Task { // runs on MainActor
     51                 symLog.log("❗️Reset wallet-core❗️")
     52                 try? await model.resetWalletCore()
     53                 controller.balances.removeAll()
     54             }
     55         }
     56     }
     57     @State private var listID = UUID()
     58 
     59     func setDelay(_ doDelay: Bool, for walletCore: WalletCore) async {
     60         symLog.log(doDelay ? "wallet-core delaying by 5 seconds"
     61                            : "wallet-core stop delaying")
     62         let delayTime = doDelay ? TCDELAY : 0
     63         try? await model.devExperimentT("taler://dev-experiment/start-tc?delay_resp=\(delayTime)")
     64     }
     65 
     66     var body: some View {
     67 #if PRINT_CHANGES
     68         let _ = Self._printChanges()
     69         let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
     70 #endif
     71         let walletCore = WalletCore.shared
     72         let list = List {
     73                 SettingsToggle(name: String("Developer Mode"), value: $developerMode,
     74                                 id1: "devMode",
     75                         description: String("More information intended for debugging")) { newVal in
     76                     withAnimation(Animation.linear.delay(0.8)) { showDevelopItems = developerMode }
     77                     Task {
     78                         symLog.log((newVal ? "enabling" : "disabling") + " applyDevExperiment")
     79                         try? await model.setConfig(setTesting: newVal)
     80                         if newVal && developDelay == true {
     81                             await setDelay(true, for: walletCore)
     82                         }
     83                     }
     84                 }
     85                 if showDevelopItems {
     86 #if DEBUG
     87                     SettingsToggle(name: String("Log Transactions"), value: $logTransactions,
     88                                     id1: "logTransactions",
     89                             description: String("full log with all tx"))
     90 #endif
     91                     if #available (iOS 16.0, *) {
     92                         let localConsStr = String("on LocalConsole")
     93                         let observability = String("Observe walletCore")
     94                         SettingsTriState(name: observability, value: $localConsoleO.onChange({ isObserving in
     95                             walletCore.isObserving = isObserving}),
     96                                           id1: "observe",
     97                                   description: localConsStr) { _ in
     98                             let consoleManager = LCManager.shared
     99                             consoleManager.isVisible = localConsoleO != 0 || localConsoleL
    100                             consoleManager.clear()
    101                         }
    102                         let showLogs = String("Show logs")
    103                         SettingsToggle(name: showLogs, value: $localConsoleL.onChange({ isLogging in
    104                             walletCore.isLogging = isLogging}),
    105                                         id1: "localConsoleL",
    106                                 description: localConsStr) { _ in
    107                             let consoleManager = LCManager.shared
    108                             consoleManager.isVisible = localConsoleO != 0 || localConsoleL
    109                             consoleManager.clear()
    110                         }
    111                     }
    112                     SettingsToggle(name: String("Set 5 seconds delay"),
    113                                   value: $developDelay.onChange({ delay in
    114                         Task {
    115                             try await setDelay(delay, for: walletCore)
    116                         }
    117                     }),
    118                                     id1: "delay",
    119                             description: String("After each network call"))
    120                         .id("delay")
    121                     SettingsToggle(name: String(localized: "Show view sizes"), value: $debugViews,
    122                                     id1: "debugViews",
    123                             description: String(localized: "Debug view layout and flickering"))
    124                         .id("debugViews")
    125 #if DEBUG
    126                     let banks = [//"stage.taler-ops.ch",    <= never has a bank!
    127 //                                 "glstest.taler.net",
    128                                  "taler.fdold.eu", "regio-taler.fdold.eu",
    129                                  "taler.grothoff.org", "taler.ar",
    130                                  "head.taler.net", "test.taler.net", "demo.taler.net", "kyctest.taler.net"]
    131                     ForEach(banks, id: \.self) { bank in
    132                         let urlStr = "https://bank." + bank
    133                         Link(bank, destination: URL(string: urlStr)!)
    134                     }
    135 #endif
    136                 } // showDevelopItems
    137                     SettingsItem(name: String("DEMO"), id1: "demo1with",
    138                           description: String("Get money for testing")) {
    139                         let title = "Withdraw"
    140                         Button(title) {
    141                             withDrawDisabled = true    // don't run twice
    142                             Task { // runs on MainActor
    143                                 symLog.log("Withdraw DEMO KUDOS")
    144                                 let amount = Amount(currency:  DEMOCURRENCY, cent: 11100)
    145                                 try? await model.loadTestKudos(0, amount: amount)
    146                             }
    147                         }
    148                         .buttonStyle(.bordered)
    149                         .disabled(withDrawDisabled)
    150                     }.id("demo1withdraw")
    151                     SettingsItem(name: String("TEST"), id1: "test1with",
    152                           description: String("Get money for testing")) {
    153                         let title = "Withdraw"
    154                         Button(title) {
    155                             withDrawDisabled = true    // don't run twice
    156                             Task { // runs on MainActor
    157                                 symLog.log("Withdraw TESTKUDOS")
    158                                 let cent = UInt64.random(in: 110...195) * 100
    159                                 let amount = Amount(currency:  TESTCURRENCY, cent: cent)
    160                                 try? await model.loadTestKudos(1, amount: amount)
    161                             }
    162                         }
    163                         .buttonStyle(.bordered)
    164                         .disabled(withDrawDisabled)
    165                     }.id("test1withdraw")
    166                 if showDevelopItems {
    167                     SettingsItem(name: String("HEAD"), id1: "head1with",
    168                           description: String("Get money for testing")) {
    169                         let title = "Withdraw"
    170                         Button(title) {
    171                             withDrawDisabled = true    // don't run twice
    172                             Task { // runs on MainActor
    173                                 symLog.log("Withdraw HEAD KUDOS")
    174                                 let amount = Amount(currency:  DEMOCURRENCY, cent: 1100)
    175                                 try? await model.loadTestKudos(2, amount: amount)
    176                             }
    177                         }
    178                         .buttonStyle(.bordered)
    179                         .disabled(withDrawDisabled)
    180                     }.id("head1withdraw")
    181 #if DEBUG
    182                     SettingsItem(name: String("Run Dev Experiment Refresh"),
    183                                   id1: "applyDevExperiment",
    184                           description: "dev-experiment/insert-pending-refresh") {
    185                         let title = "Refresh"
    186                         Button(title) {
    187                             Task { // runs on MainActor
    188                                 symLog.log("running applyDevExperiment Refresh")
    189                                 try await model.setConfig(setTesting: true)
    190                                 try await model.devExperimentT("taler://dev-experiment/start-block-refresh")
    191                                 try await model.devExperimentT("taler://dev-experiment/insert-pending-refresh")
    192                             }
    193                         }
    194                         .buttonStyle(.bordered)
    195                     }.id("Refresh")
    196 #endif
    197                     SettingsItem(name: String("Run Integration Test"),
    198                                   id1: "demo1test",
    199                           description: String("Perform basic test transactions")) {
    200                         let title = "Demo 1"
    201                         Button(title) {
    202                             checkDisabled = true    // don't run twice
    203                             Task { // runs on MainActor
    204                                 symLog.log("running integration test on demo")
    205                                 try? await model.runIntegrationTest(newVersion: false, test: false)
    206                             }
    207                         }
    208                         .buttonStyle(.bordered)
    209                         .disabled(checkDisabled)
    210                     }.id("demo1runTest")
    211                     SettingsItem(name: String("Run Integration Test"),
    212                                   id1: "test1test",
    213                           description: "Perform basic test transactions") {
    214                         let title = "Test 1"
    215                         Button(title) {
    216                             checkDisabled = true    // don't run twice
    217                             Task { // runs on MainActor
    218                                 symLog.log("running integration test on test")
    219                                 try? await model.runIntegrationTest(newVersion: false, test: true)
    220                             }
    221                         }
    222                         .buttonStyle(.bordered)
    223                         .disabled(checkDisabled)
    224                     }.id("test1runTest")
    225                     SettingsItem(name: String("Run Integration Test V2"),
    226                                   id1: "demo2test",
    227                           description: String("Perform more test transactions")) {
    228                         let title = "Demo 2"
    229                         Button(title) {
    230                             checkDisabled = true    // don't run twice
    231                             Task { // runs on MainActor
    232                                 symLog.log("running integration test V2 on demo")
    233                                 try? await model.runIntegrationTest(newVersion: true, test: false)
    234                             }
    235                         }
    236                         .buttonStyle(.bordered)
    237                         .disabled(checkDisabled)
    238                     }.id("demo2runTest")
    239                     SettingsItem(name: String("Run Integration Test V2"),
    240                                   id1: "test2test",
    241                           description: String("Perform more test transactions")) {
    242                         let title = "Test 2"
    243                         Button(title) {
    244                             checkDisabled = true    // don't run twice
    245                             Task { // runs on MainActor
    246                                 symLog.log("running integration test V2 on test")
    247                                 try? await model.runIntegrationTest(newVersion: true, test: true)
    248                             }
    249                         }
    250                         .buttonStyle(.bordered)
    251                         .disabled(checkDisabled)
    252                     }.id("test2runTest")
    253                     SettingsItem(name: String("Run Infinite Transaction Loop"),
    254                                   id1: "runInfinite",
    255                           description: String("Check DB in background")) {
    256                         let title = "Loop"
    257                         Button(title) {
    258                             checkDisabled = true    // don't run twice
    259                             Task { // runs on MainActor
    260                                 symLog.log("Running Infinite Transaction Loop")
    261                                 try? await model.testingInfiniteTransaction(delayMs: 10_000, shouldFetch: true)
    262                             }
    263                         }
    264                         .buttonStyle(.bordered)
    265                         .disabled(checkDisabled)
    266                     }.id("runInfiniteLoop")
    267 #if false
    268                     SettingsItem(name: String("Save Logfile"), id1: "save",
    269                           description: String("Help debugging wallet-core")) {
    270                         Button("Save") {
    271                             symLog.log("Saving Log")
    272                             // FIXME: Save Logfile
    273                         }
    274                         .buttonStyle(.bordered)
    275                         .disabled(true)
    276                     }.id("saveLog")
    277 #endif
    278                     SettingsItem(name: String("Reset Wallet"), id1: "reset",
    279                           description: String("Throw away all your money")) {
    280                         Button("Reset") {
    281                             showResetAlert = true
    282                         }
    283                         .buttonStyle(.bordered)
    284 //                        .disabled(didReset)
    285                     }.id("resetWallet")
    286                 }
    287             }
    288             .id(listID)
    289             .listStyle(myListStyle.style).anyView
    290             .navigationTitle(navTitle)
    291             .onAppear() {
    292                 showDevelopItems = developerMode
    293                 DebugViewC.shared.setViewID(VIEW_SETTINGS3, stack: stack.push())
    294             }
    295             .onDisappear() {
    296                 checkDisabled = false    // reset
    297                 withDrawDisabled = false
    298             }
    299             .alert("Reset Wallet", isPresented: $showResetAlert,
    300                 actions: { dismissAlertButton
    301                            resetButton },
    302                 message: { Text(verbatim: "Are you sure you want to reset your wallet?\nThis cannot be reverted, all money will be lost.") })
    303         if #available(iOS 26.0, *) {
    304             list
    305         } else {
    306             list
    307                 .padding(.bottom)
    308         }
    309     } // body
    310 }