P2pReceiveURIView.swift (9483B)
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 // Will be called either by the user scanning a QR code or tapping the provided link, 13 // from another user's Send. We show the P2P details - but first the ToS must be accepted. 14 struct P2pReceiveURIView: View { 15 private let symLog = SymLogV(0) 16 let stack: CallStack 17 let url: URL // the scanned URL 18 let oimEuro: Bool 19 20 @EnvironmentObject private var model: WalletModel 21 @EnvironmentObject private var controller: Controller 22 @Environment(\.colorScheme) private var colorScheme 23 @Environment(\.colorSchemeContrast) private var colorSchemeContrast 24 @AppStorage("minimalistic") var minimalistic: Bool = false 25 @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic 26 27 @StateObject private var cash: OIMcash 28 @State private var currencyInfo: CurrencyInfo = CurrencyInfo.zero(UNKNOWN) 29 @State private var peerPushCreditResponse: PreparePeerPushCreditResponse? = nil 30 @State private var accept: Bool = false 31 @State private var exchange: Exchange? = nil 32 @Namespace var namespace 33 34 let navTitle = String(localized: "Receive", comment: "Nav Title") 35 36 // Since there is no selectedBalance, we cannot know which currency we'll need to render at this time 37 init(stack: CallStack, url: URL, oimEuro: Bool) { 38 self.stack = stack 39 self.url = url 40 self.oimEuro = oimEuro 41 // let oimCurrency = oimCurrency(selectedBalance.wrappedValue?.scopeInfo) // might be nil ==> OIMeuros 42 let oimCurrency = oimCurrency(nil, oimEuro: oimEuro) // nil ==> OIMeuros 43 let oimCash = OIMcash(oimCurrency) 44 self._cash = StateObject(wrappedValue: { oimCash }()) 45 } 46 47 @MainActor 48 private func viewDidLoad() async { 49 symLog.log(".task") 50 if let ppResponse = try? await model.preparePeerPushCredit(url.trimmedString) { 51 let baseUrl = ppResponse.exchangeBaseUrl 52 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 53 // TODO: we should react on controller observation: @Observable needs iOS 17; or Point-Free's Perception library 54 // instead of just waiting for 0.1 seconds (animation runs 0.5 so nobody sees it anyway) 55 Task { 56 exchange = await controller.checkCurrencyInfo(for: baseUrl) 57 let oimCurrency = oimCurrency(ppResponse.scopeInfo, oimEuro: oimEuro) 58 cash.setCurrency(oimCurrency) 59 controller.removeURL(url) // tx is now saved by wallet-core 60 peerPushCreditResponse = ppResponse 61 } 62 } 63 } else { 64 peerPushCreditResponse = nil 65 } 66 } 67 68 var body: some View { 69 #if PRINT_CHANGES 70 let _ = Self._printChanges() 71 let _ = symLog.vlog() // just to get the # to compare it with .onAppear & onDisappear 72 #endif 73 VStack { 74 if let peerPushCreditResponse { 75 let scope = peerPushCreditResponse.scopeInfo 76 let balance = controller.balance(for: scope) 77 let tosAccepted = exchange?.tosStatus == .accepted 78 || exchange?.tosStatus == .missingTos // no ToS to accept 79 if !tosAccepted { 80 ToSButtonView(stack: stack.push(), 81 exchangeBaseUrl: peerPushCreditResponse.exchangeBaseUrl, 82 viewID: SHEET_RCV_P2P_TOS, 83 shouldDismiss: true, 84 p2p: true) 85 } 86 List { 87 PeerPushCreditView(stack: stack.push(), 88 raw: peerPushCreditResponse.amountRaw, 89 effective: peerPushCreditResponse.amountEffective, 90 isDone: false, 91 scope: peerPushCreditResponse.scopeInfo, 92 header: peerPushCreditResponse.contractTerms.summary) 93 ExpiresView(expiration: peerPushCreditResponse.contractTerms.purse_expiration) 94 } 95 .listStyle(myListStyle.style).anyView 96 .navigationTitle(navTitle) 97 .task(id: controller.currencyTicker) { 98 let currency = peerPushCreditResponse.amountRaw.currencyStr 99 currencyInfo = controller.info2(for: currency, controller.currencyTicker) 100 } 101 .safeAreaInset(edge: .bottom) { 102 if peerPushCreditResponse.txState.isConfirmed { 103 Button("Done") { dismissTop(stack.push()) } 104 .buttonStyle(TalerButtonStyle(type: .prominent)) 105 .padding(.horizontal) 106 } else { 107 if tosAccepted { 108 PeerPushCreditAccept(stack: stack.push(), url: url, 109 transactionId: peerPushCreditResponse.transactionId, 110 accept: $accept) 111 } 112 } 113 } 114 #if OIM && DEBUG 115 .overlay { if #available(iOS 16.4, *) { 116 if controller.oimSheetActive { 117 OIMp2pReceiveView(stack: stack.push(), 118 cash: cash, 119 available: balance?.available, 120 peerPushCreditResponse: $peerPushCreditResponse, 121 fwdButtonTapped: $accept) 122 .environmentObject(NamespaceWrapper(namespace)) // keep OIMviews apart 123 } 124 } } 125 #endif 126 } else { 127 #if DEBUG 128 let message = url.host 129 #else 130 let message: String? = nil 131 #endif 132 LoadingView(stack: stack.push(), scopeInfo: nil, message: message) 133 } 134 } 135 // must be here and not at LoadingView(), because this needs to run a 2nd time after ToS was accepted 136 .task { await viewDidLoad() } 137 .onAppear() { 138 symLog.log("onAppear") 139 DebugViewC.shared.setSheetID(SHEET_RCV_P2P, stack: stack.push()) 140 } 141 } 142 } 143 // MARK: - 144 struct PeerPushCreditAccept: View { 145 let stack: CallStack 146 let url: URL? 147 let transactionId: String 148 @Binding var accept: Bool // triggers 'destination' when set true 149 150 var body: some View { 151 let destination = P2pAcceptDone(stack: stack.push(), 152 url: url, 153 transactionId: transactionId, 154 incoming: true) 155 let actions = Group { 156 NavLink($accept) { destination } 157 } 158 Button("Accept and receive") { // SHEET_RCV_P2P_ACCEPT 159 accept = true 160 } 161 .background(actions) 162 .buttonStyle(TalerButtonStyle(type: .prominent)) 163 .padding(.horizontal) 164 } 165 } 166 // MARK: - 167 struct PeerPushCreditView: View { 168 let stack: CallStack 169 let raw: Amount 170 let effective: Amount 171 let isDone: Bool 172 let scope: ScopeInfo? 173 let header: String? 174 175 var body: some View { 176 let fee = try! Amount.diff(raw, effective) 177 ThreeAmountsSection(stack: stack.push(), 178 scope: scope, 179 topTitle: String(localized: "Gross Amount to receive:"), 180 topAbbrev: String(localized: "Receive gross:", comment: "mini"), 181 topAmount: raw, 182 noFees: nil, // TODO: check baseURL for fees 183 fee: fee, 184 bottomTitle: String(localized: "Net Amount to receive:"), 185 bottomAbbrev: String(localized: "Receive net:", comment: "mini"), 186 bottomAmount: effective, 187 large: false, 188 pendingDialog: false, 189 isDone: isDone, 190 incoming: true, 191 baseURL: nil, 192 txStateLcl: nil, 193 header: header) 194 } 195 } 196 // MARK: - 197 struct ExpiresView: View { 198 let expiration: Timestamp? 199 200 @Environment(\.colorScheme) private var colorScheme 201 @Environment(\.colorSchemeContrast) private var colorSchemeContrast 202 @AppStorage("minimalistic") var minimalistic: Bool = false 203 var body: some View { 204 if let expiration { 205 let (dateString, date) = TalerDater.dateString(expiration, minimalistic) 206 let a11yDate = TalerDater.accessibilityDate(date) ?? dateString 207 let a11yLabel = String(localized: "Expires: \(a11yDate)", comment: "a11y") 208 Text("Expires: \(dateString)") 209 .talerFont(.body) 210 .accessibilityLabel(a11yLabel) 211 .foregroundColor(WalletColors().secondary2(colorScheme, colorSchemeContrast)) 212 } else { // should never happen 213 Text("Unknown Expiration") 214 .talerFont(.body) 215 .foregroundColor(WalletColors().secondary2(colorScheme, colorSchemeContrast)) 216 } 217 } 218 }