taler-ios

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

P2PReadyV.swift (5435B)


      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 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     let navTitle = String(localized: "Ready")
     34     @State private var transactionId: String? = nil
     35     @State private var noTransaction: TalerTransaction? = nil
     36     @State private var talerTX = TalerTransaction(dummyCurrency: DEMOCURRENCY)
     37     @Namespace var namespace
     38 
     39     @MainActor
     40     private func initiateP2P() async {
     41         symLog.log(".task")
     42         guard transactionStarted == false else {
     43 // TODO:    logger.warning("Trying to start P2P a second time")
     44             symLog.log("Yikes❗️ Trying to start P2P a second time")
     45             return
     46         }
     47         transactionStarted = true
     48         let timestamp = developerMode ? Timestamp.inSomeMinutes(expireDays > 20 ? (24*60)   // 24h
     49                                                               : expireDays > 5  ? 60        //  1h
     50                                                                                 : 3)        //  3m
     51                                       : Timestamp.inSomeDays(expireDays)
     52         let terms = PeerContractTerms(amount: amountToTransfer,
     53                                      summary: summary,
     54                             purse_expiration: timestamp,
     55                                      icon_id: iconID)
     56         if outgoing {
     57             // TODO: let user choose baseURL
     58             if let response = try? await model.initiatePeerPushDebit(scope: scope, terms: terms) {
     59                 // will switch from WithdrawProgressView to TransactionSummaryV
     60                 transactionId = response.transactionId
     61             }
     62         } else {
     63             // TODO: let user choose baseURL
     64             if let response = try? await model.initiatePeerPullCredit(nil, terms: terms) {
     65                 // will switch from WithdrawProgressView to TransactionSummaryV
     66                 transactionId = response.transactionId
     67             }
     68         }
     69     }
     70 
     71     func doneAction() {
     72         dismissTop(stack.push())
     73     }
     74 
     75     var body: some View {
     76 #if PRINT_CHANGES
     77         let _ = Self._printChanges()
     78         let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
     79 #endif
     80         Group {
     81             if let transactionId {
     82                 TransactionSummaryV(stack: stack.push(),
     83 //                                    scope: scope,
     84                             transactionId: transactionId,
     85                                   talerTX: $talerTX,
     86                                  navTitle: navTitle,
     87                                   hasDone: true,
     88                               abortAction: nil,
     89                              deleteAction: nil,
     90                                failAction: nil,
     91                             suspendAction: nil,
     92                              resumeAction: nil)
     93                 .navigationBarBackButtonHidden(true)
     94                 .interactiveDismissDisabled()           // can only use "Done" button to dismiss
     95                 .safeAreaInset(edge: .bottom) {
     96                     Button("Done", action: doneAction)
     97                         .buttonStyle(TalerButtonStyle(type: .prominent))
     98                         .padding(.horizontal)
     99                 }
    100             } else {
    101 #if DEBUG
    102                 let message = amountToTransfer.currencyStr
    103 #else
    104                 let message: String? = nil
    105 #endif
    106                 LoadingView(stack: stack.push(), scopeInfo: scope, message: message)
    107             }
    108         }
    109         .navigationTitle(navTitle)
    110         .task { await initiateP2P() }
    111         .onAppear {
    112             DebugViewC.shared.setViewID(VIEW_P2P_READY, stack: stack.push())
    113 //            print("❗️ P2PReadyV onAppear")
    114         }
    115         .onDisappear {
    116 //            print("❗️ P2PReadyV onDisappear")
    117         }
    118 #if OIM
    119             .overlay { if #available(iOS 16.4, *) {
    120                 if controller.oimModeActive {
    121                     OIMp2pReadyView(stack: stack.push(),
    122                             transactionId: $transactionId,
    123                                   talerTX: talerTX,
    124                                     scope: scope,
    125                                    action: doneAction)
    126                     .environmentObject(NamespaceWrapper(namespace))             // keep OIMviews apart
    127                 }
    128             } }
    129 #endif
    130     }
    131 }
    132 // MARK: -
    133 //struct SendNow_Previews: PreviewProvider {
    134 //    static var previews: some View {
    135 //        Group {
    136 //            SendDoneV(stack: CallStack("Preview"),
    137 //               amountToSend: Amount(currency: LONGCURRENCY, cent: 480),
    138 //           amountToReceive: nil,
    139 //                   summary: "some subject/purpose",
    140 //                expireDays: 0)
    141 //        }
    142 //    }
    143 //}