summaryrefslogtreecommitdiff
path: root/TalerWallet1/Views/Exchange/ManualWithdraw.swift
blob: dc982689e2448543d89ec9fb017b498fcbc06b80 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import SwiftUI
import taler_swift
import SymLog

// Will be called by the user tapping "Withdraw Coins" in the exchange list
struct ManualWithdraw: View {
    private let symLog = SymLogV(0)
    let stack: CallStack

    let exchange: Exchange
    @Binding var centsToTransfer: UInt64

    @EnvironmentObject private var model: WalletModel

    @State var withdrawalAmountDetails: WithdrawalAmountDetails? = nil

//    @State var ageMenuList: [Int] = []
//    @State var selectedAge = 0

    var body: some View {
#if DEBUG
        let _ = Self._printChanges()
        let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
#endif
        let currency = exchange.currency ?? String(localized: "Unknown", comment: "unknown currency")
        let navTitle = String(localized: "NavTitle_Withdraw (currency)", defaultValue: "Withdraw \(currency)")
        let currencyField = CurrencyField(value: $centsToTransfer, currency: currency) // becomeFirstResponder
//        let agePicker = AgePicker(ageMenuList: $ageMenuList, selectedAge: $selectedAge)

        ScrollView {
            VStack {
                CurrencyInputView(currencyField: currencyField,
                                  title: String(localized: "Amount to withdraw:"))
                let someCoins = SomeCoins(details: withdrawalAmountDetails)
                QuiteSomeCoins(someCoins: someCoins, shouldShowFee: true,
                               currency: currency, amountEffective: withdrawalAmountDetails?.amountEffective)
                Text(exchange.exchangeBaseUrl.trimURL())
                    .multilineTextAlignment(.center)
                    .accessibilityFont(.body)

                let disabled = (centsToTransfer == 0) || someCoins.invalid || someCoins.tooMany
                if !disabled {
//                    agePicker

                    if let tosAccepted = withdrawalAmountDetails?.tosAccepted {
                        if tosAccepted {
//                            let restrictAge: Int? = (selectedAge == 0) ? nil
//                                                                       : selectedAge
//let _ = print(selectedAge, restrictAge)
                            NavigationLink(destination: LazyView {
                                ManualWithdrawDone(stack: stack.push(),
                                                exchange: exchange,
                                         centsToTransfer: centsToTransfer)
//                                              restrictAge: restrictAge)
                            }) {
                                Text("Confirm Withdrawal")      // VIEW_WITHDRAW_ACCEPT
                            }
                            .buttonStyle(TalerButtonStyle(type: .prominent))
                            .padding(.horizontal)
                        } else {
                            ToSButtonView(stack: stack.push(),
                                exchangeBaseUrl: exchange.exchangeBaseUrl,
                                         viewID: VIEW_WITHDRAW_TOS,
                                            p2p: false)
                        }
                    }
                } // disabled
                Spacer()
            }
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding(.horizontal)
        .background(WalletColors().backgroundColor.edgesIgnoringSafeArea(.all))
        .navigationTitle(navTitle)
        .onAppear {
            symLog.log("onAppear")
            DebugViewC.shared.setViewID(VIEW_WITHDRAWAL, stack: stack.push())
        }
        .task(id: centsToTransfer) { // re-run this whenever centsToTransfer changes
            if centsToTransfer > 0 {
                let amount = Amount.amountFromCents(currency, centsToTransfer)
                do {
                    withdrawalAmountDetails = try await model.loadWithdrawalDetailsForAmountM(exchange.exchangeBaseUrl, amount: amount)
//                  agePicker.setAges(ages: withdrawalAmountDetails?.ageRestrictionOptions)
                } catch {    // TODO: error
                    symLog.log(error.localizedDescription)
                    withdrawalAmountDetails = nil
                }
            }
        }
    }
}
// MARK: -
#if DEBUG
struct ManualWithdraw_Container : View {
    @State private var centsToTransfer: UInt64 = 510
    @State private var details = WithdrawalAmountDetails(tosAccepted: false,
                                                           amountRaw: try! Amount(fromString: LONGCURRENCY + ":5.1"),
                                                     amountEffective: try! Amount(fromString: LONGCURRENCY + ":5.0"),
                                                           paytoUris: [],
                                               ageRestrictionOptions: [],
                                                            numCoins: 6)
    var body: some View {
        let exchange = Exchange(exchangeBaseUrl: DEMOEXCHANGE,
                                       currency: LONGCURRENCY,
                                      paytoUris: [],
                                      tosStatus: .pending,
                            exchangeEntryStatus: .preset,
                           exchangeUpdateStatus: .initial,
                          ageRestrictionOptions: [])
        ManualWithdraw(stack: CallStack("Preview"),
                    exchange: exchange,
             centsToTransfer: $centsToTransfer,
     withdrawalAmountDetails: details)
    }
}

struct ManualWithdraw_Previews: PreviewProvider {
    static var previews: some View {
        ManualWithdraw_Container()
    }
}
#endif