summaryrefslogtreecommitdiff
path: root/TalerWallet1/Views/Peer2peer/SendDoneV.swift
blob: 84b31ee03b5c3e740b5317a263cb4543e3658e98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import SwiftUI
import taler_swift
import SymLog

// Called when initiating a P2P transaction: Send coins or Send Request(Invoice)
struct SendDoneV: View {
    private let symLog = SymLogV()
    let stack: CallStack
    let navTitle = String(localized: "P2P Ready")
#if DEBUG
    @AppStorage("developerMode") var developerMode: Bool = true
#else
    @AppStorage("developerMode") var developerMode: Bool = false
#endif
    @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic

    let amountToSend: Amount?
    let amountToReceive: Amount?
    let summary: String
    let expireDays: UInt
    @Binding var transactionStarted: Bool

    @EnvironmentObject private var model: WalletModel

    @State private var transactionId: String? = nil

    func reloadOneAction(_ transactionId: String) async throws -> Transaction {
        return try await model.getTransactionByIdT(transactionId)
    }

    var body: some View {
#if DEBUG
        let _ = Self._printChanges()
        let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
#endif
        VStack {
            if let transactionId {
                TransactionDetailView(stack: stack.push(),
                              transactionId: transactionId,
                               reloadAction: reloadOneAction,
                                   navTitle: navTitle,
                                 doneAction: ViewState.shared.popToRootView,
                                abortAction: nil,
                               deleteAction: nil,
                                 failAction: nil,
                              suspendAction: nil,
                               resumeAction: nil)
                .navigationBarBackButtonHidden(true)
                .interactiveDismissDisabled()           // can only use "Done" button to dismiss
            } else {
                WithdrawProgressView(message: "Loading...")
            }
        }
//        .background(WalletColors().backgroundColor.edgesIgnoringSafeArea(.all))
        .navigationTitle(navTitle)
        .task {
            symLog.log(".task")
            do {
                guard transactionStarted == false else {
// TODO:               logger.warning("Try to start P2P a second time")
                    symLog.log("Yikes❗️ Try to start P2P a second time")
                    return
                }
                transactionStarted = true
                let timestamp = developerMode ? Timestamp.inSomeMinutes(expireDays > 20 ? (24*60)
                                                                      : expireDays > 5 ? 60 : 3)
                                              : Timestamp.inSomeDays(expireDays)
                if let amountToSend {
                    let terms = PeerContractTerms(amount: amountToSend,
                                                 summary: summary,
                                        purse_expiration: timestamp)
                    // TODO: user might choose baseURL
                    let response = try await model.initiatePeerPushDebitM(nil, terms: terms)
                    // will switch from WithdrawProgressView to TransactionDetailView
                    transactionId = response.transactionId
                } else if let amountToReceive {
                    let terms = PeerContractTerms(amount: amountToReceive,
                                                 summary: summary,
                                        purse_expiration: timestamp)
                    // TODO: user might choose baseURL
                    let response = try await model.initiatePeerPullCreditM(nil, terms: terms)
                    // will switch from WithdrawProgressView to TransactionDetailView
                    transactionId = response.transactionId
                } else { fatalError() }
            } catch {    // TODO: error
                symLog.log(error.localizedDescription)
            }
        } // task
    }
}
// MARK: -
//struct SendNow_Previews: PreviewProvider {
//    static var previews: some View {
//        Group {
//            SendDoneV(stack: CallStack("Preview"),
//              amountToSend: try! Amount(fromString: LONGCURRENCY + ":4.8"),
//           amountToReceive: nil,
//                   summary: "some subject/purpose",
//                expireDays: 0)
//        }
//    }
//}