taler-ios

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

P2PReadyV.swift (5078B)


      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 
     12 // Called when initiating a P2P transaction: Send or Request(Invoice)
     13 struct P2PReadyV: View {
     14     private let symLog = SymLogV(0)
     15     let stack: CallStack
     16     let scope: ScopeInfo
     17     let summary: String
     18     let iconID: String?
     19     let expireDays: UInt
     20     let outgoing: Bool
     21     let amountToTransfer: Amount
     22     @Binding var transactionStarted: Bool
     23 
     24     @EnvironmentObject private var controller: Controller
     25     @EnvironmentObject private var model: WalletModel
     26 #if DEBUG
     27     @AppStorage("developerMode") var developerMode: Bool = true
     28 #else
     29     @AppStorage("developerMode") var developerMode: Bool = false
     30 #endif
     31     @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic
     32 
     33     @State private var transactionId: String? = nil
     34     @State private var noTransaction: TalerTransaction? = nil
     35     @State private var talerTX = TalerTransaction(dummyCurrency: DEMOCURRENCY)
     36     @Namespace var namespace
     37 
     38     @MainActor
     39     private func viewDidLoad() async {
     40         symLog.log("viewDidLoad")
     41         guard transactionStarted == false else {
     42 // TODO:    logger.warning("Trying to start P2P a second time")
     43             symLog.log("Yikes❗️ Trying to start P2P a second time")
     44             return
     45         }
     46         transactionStarted = true
     47         let timestamp = developerMode ? Timestamp.inSomeMinutes(expireDays > 20 ? (24*60)   // 24h
     48                                                               : expireDays > 5  ? 60        //  1h
     49                                                                                 : 3)        //  3m
     50                                       : Timestamp.inSomeDays(expireDays)
     51         let purseExpiration = developerMode ? timestamp
     52                                  : outgoing ? nil : timestamp
     53         let terms = PeerContractTerms(amount: amountToTransfer,
     54                                      summary: summary,
     55                             purse_expiration: purseExpiration,
     56                                      icon_id: iconID)
     57         if outgoing {
     58             // TODO: let user choose baseURL
     59             if let response = try? await model.initiatePeerPushDebit(scope: scope, terms: terms) {
     60                 // will switch from WithdrawProgressView to TransactionSummaryList
     61                 transactionId = response.transactionId
     62             }
     63         } else {
     64             // TODO: let user choose baseURL
     65             if let response = try? await model.initiatePeerPullCredit(nil, terms: terms) {
     66                 // will switch from WithdrawProgressView to TransactionSummaryList
     67                 transactionId = response.transactionId
     68             }
     69         }
     70     }
     71 
     72     func doneAction() {
     73         dismissTop(stack.push())
     74     }
     75 
     76     var body: some View {
     77 #if PRINT_CHANGES
     78         let _ = Self._printChanges()
     79         let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
     80 #endif
     81         let navTitle = String(localized: "Ready")
     82         ZStack {
     83             if let transactionId {
     84                 TransactionSummaryList(stack: stack.push(),
     85 //                                     scope: scope,
     86                                transactionId: transactionId,
     87                                      talerTX: $talerTX,
     88                                     navTitle: navTitle,
     89                                      hasDone: true,
     90                                     showDone: .prominent,
     91                                          url: nil,
     92                                  withActions: false)
     93             } else {
     94 #if DEBUG
     95                 let message = amountToTransfer.currencyStr
     96 #else
     97                 let message: String? = nil
     98 #endif
     99                 LoadingView(stack: stack.push(), scopeInfo: scope, message: message)
    100                     .task { await viewDidLoad() }
    101             }
    102         }.onAppear {
    103             symLog.log("onAppear")
    104             DebugViewC.shared.setViewID(VIEW_P2P_READY, stack: stack.push())
    105         }
    106         .onDisappear {
    107 //            print("❗️ P2PReadyV onDisappear")
    108         }
    109 #if OIM
    110             .overlay { if #available(iOS 16.4, *) {
    111                 if controller.oimModeActive {
    112                     OIMp2pReadyView(stack: stack.push(),
    113                             transactionId: $transactionId,
    114                                   talerTX: talerTX,
    115                                     scope: scope,
    116                                    action: doneAction)
    117                     .environmentObject(NamespaceWrapper(namespace))             // keep OIMviews apart
    118                 }
    119             } }
    120 #endif
    121     }
    122 }
    123 // MARK: -
    124 //struct SendNow_Previews: PreviewProvider {
    125 //    static var previews: some View {
    126 //        Group {
    127 //            SendDoneV(stack: CallStack("Preview"),
    128 //               amountToSend: Amount(currency: LONGCURRENCY, cent: 480),
    129 //           amountToReceive: nil,
    130 //                   summary: "some subject/purpose",
    131 //                expireDays: 0)
    132 //        }
    133 //    }
    134 //}