This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathCryptoView.swift
380 lines (367 loc) · 13 KB
/
CryptoView.swift
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* Copyright 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import UIKit
import SwiftUI
import BraveCore
import Introspect
import BraveUI
import Preferences
public struct CryptoView: View {
var walletStore: WalletStore
@ObservedObject var keyringStore: KeyringStore
var webImageDownloader: WebImageDownloaderType
var presentingContext: PresentingContext
@Environment(\.presentationMode) @Binding private var presentationMode
var openWalletURLAction: ((URL) -> Void)?
var appRatingRequestAction: (() -> Void)?
@ObservedObject var isOnboardingCompleted = Preferences.Wallet.isOnboardingCompleted
public init(
walletStore: WalletStore,
keyringStore: KeyringStore,
webImageDownloader: WebImageDownloaderType,
presentingContext: PresentingContext
) {
self.walletStore = walletStore
self.keyringStore = keyringStore
self.webImageDownloader = webImageDownloader
self.presentingContext = presentingContext
}
private enum VisibleScreen: Equatable {
case crypto
case onboarding
case unlock
}
private var visibleScreen: VisibleScreen {
if !keyringStore.isWalletCreated || keyringStore.isOnboardingVisible {
return .onboarding
}
if keyringStore.isWalletLocked || keyringStore.isRestoreFromUnlockBiometricsPromptVisible {
return .unlock
}
return .crypto
}
@ToolbarContentBuilder
private var dismissButtonToolbarContents: some ToolbarContent {
ToolbarItemGroup(placement: .cancellationAction) {
Button(action: {
if case .requestPermissions(let request, let onPermittedAccountsUpdated) = presentingContext {
request.decisionHandler(.rejected)
onPermittedAccountsUpdated([])
}
dismissAction()
}) {
Image("wallet-dismiss", bundle: .module)
.renderingMode(.template)
.foregroundColor(Color(.braveBlurpleTint))
}
}
}
public var body: some View {
ZStack {
switch visibleScreen {
case .crypto:
if let store = walletStore.cryptoStore {
Group {
switch presentingContext {
case .`default`:
CryptoContainerView(
keyringStore: keyringStore,
cryptoStore: store,
toolbarDismissContent: dismissButtonToolbarContents
)
case .pendingRequests:
RequestContainerView(
keyringStore: keyringStore,
cryptoStore: store,
toolbarDismissContent: dismissButtonToolbarContents,
onDismiss: {
dismissAction()
}
)
.onDisappear {
// onDisappear allows us to catch all cases (swipe, cancel, confirm/approve/sign)
store.isPresentingPendingRequest = false
store.prepare()
}
case .requestPermissions(let request, let onPermittedAccountsUpdated):
NewSiteConnectionView(
origin: request.requestingOrigin,
accounts: request.requestingAccounts,
coin: request.coinType,
keyringStore: keyringStore,
onConnect: {
request.decisionHandler(.granted(accounts: $0))
onPermittedAccountsUpdated($0)
dismissAction()
},
onDismiss: {
request.decisionHandler(.rejected)
onPermittedAccountsUpdated([])
dismissAction()
}
)
case .panelUnlockOrSetup:
EmptyView()
case .accountSelection:
NavigationView {
AccountSelectionView(
keyringStore: keyringStore,
networkStore: store.networkStore,
onDismiss: {
dismissAction()
}
)
}
.navigationViewStyle(.stack)
case .transactionHistory:
NavigationView {
AccountTransactionListView(
activityStore: store.accountActivityStore(
for: walletStore.keyringStore.selectedAccount,
observeAccountUpdates: true
),
networkStore: store.networkStore
)
.onDisappear {
store.closeAccountActivityStore(for: walletStore.keyringStore.selectedAccount)
}
.toolbar {
dismissButtonToolbarContents
}
}
.navigationViewStyle(.stack)
case .buySendSwap(let destination):
switch destination.kind {
case .buy:
BuyTokenView(
keyringStore: keyringStore,
networkStore: store.networkStore,
buyTokenStore: store.openBuyTokenStore(destination.initialToken),
onDismiss: {
store.closeBSSStores()
dismissAction()
}
)
case .send:
SendTokenView(
keyringStore: keyringStore,
networkStore: store.networkStore,
sendTokenStore: store.openSendTokenStore(destination.initialToken),
completion: { success in
if success {
store.closeBSSStores()
dismissAction()
}
},
onDismiss: {
store.closeBSSStores()
dismissAction()
}
)
case .swap:
SwapCryptoView(
keyringStore: keyringStore,
networkStore: store.networkStore,
swapTokensStore: store.openSwapTokenStore(destination.initialToken),
completion: { success in
if success {
store.closeBSSStores()
dismissAction()
}
},
onDismiss: {
store.closeBSSStores()
dismissAction()
}
)
}
case .settings:
NavigationView {
Web3SettingsView(
settingsStore: store.settingsStore,
networkStore: store.networkStore,
keyringStore: keyringStore
)
.toolbar {
dismissButtonToolbarContents
}
}
.navigationViewStyle(.stack)
case .editSiteConnection(let origin, let handler):
EditSiteConnectionView(
keyringStore: keyringStore,
origin: origin,
coin: keyringStore.selectedAccount.coin,
onDismiss: { accounts in
handler(accounts)
dismissAction()
}
)
case .createAccount(let request):
NavigationView {
AddAccountView(
keyringStore: keyringStore,
networkStore: store.networkStore,
preSelectedCoin: request.coinType,
onCreate: {
// request is fullfilled.
request.responseHandler(.created)
dismissAction()
},
onDismiss: {
// request get declined by clicking `Cancel`
request.responseHandler(.rejected)
dismissAction()
}
)
}
.navigationViewStyle(.stack)
}
}
.transition(.asymmetric(insertion: .identity, removal: .opacity))
}
case .unlock:
UIKitNavigationView {
UnlockWalletView(keyringStore: keyringStore, dismissAction: dismissAction)
.toolbar {
dismissButtonToolbarContents
}
}
.transition(.move(edge: .bottom))
.zIndex(1) // Needed or the dismiss animation messes up
case .onboarding:
if isOnboardingCompleted.value {
UIKitNavigationView {
OnboardingCompletedView(keyringStore: keyringStore)
}
.transition(.move(edge: .bottom))
.zIndex(2) // Needed or the dismiss animation messes up
} else {
UIKitNavigationView {
SetupCryptoView(keyringStore: keyringStore, dismissAction: dismissAction)
.toolbar {
ToolbarItemGroup(placement: .destructiveAction) {
Button(action: {
dismissAction()
}) {
Text(Strings.CancelString)
}
}
}
}
.transition(.move(edge: .bottom))
.zIndex(3) // Needed or the dismiss animation messes up
}
}
}
.animation(.default, value: visibleScreen) // Animate unlock dismiss (required for some reason)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.environment(\.openURL, .init(handler: { [openWalletURLAction] url in
openWalletURLAction?(url)
return .handled
}))
.environment(
\.appRatingRequestAction,
.init(action: { [appRatingRequestAction] in
appRatingRequestAction?()
}))
.environment(\.webImageDownloader, webImageDownloader)
.onChange(of: visibleScreen) { newValue in
if case .panelUnlockOrSetup = presentingContext, newValue == .crypto {
dismissAction()
}
}
}
private func dismissAction() {
presentationMode.dismiss()
}
}
private struct CryptoContainerView<DismissContent: ToolbarContent>: View {
var keyringStore: KeyringStore
@ObservedObject var cryptoStore: CryptoStore
var toolbarDismissContent: DismissContent
@ToolbarContentBuilder
// This toolbar content is for `PendingRequestView` which is presented on top of full screen wallet
private var pendingRequestToolbarDismissContent: some ToolbarContent {
ToolbarItemGroup(placement: .cancellationAction) {
Button(action: {
cryptoStore.isPresentingPendingRequest = false
}) {
Image("wallet-dismiss", bundle: .module)
.renderingMode(.template)
.foregroundColor(Color(.braveBlurpleTint))
}
}
}
var body: some View {
CryptoTabsView(
cryptoStore: cryptoStore,
keyringStore: keyringStore,
toolbarDismissContent: toolbarDismissContent
)
.background(
Color.clear
.sheet(item: $cryptoStore.buySendSwapDestination) { action in
switch action.kind {
case .buy:
BuyTokenView(
keyringStore: keyringStore,
networkStore: cryptoStore.networkStore,
buyTokenStore: cryptoStore.openBuyTokenStore(action.initialToken),
onDismiss: { cryptoStore.buySendSwapDestination = nil }
)
case .send:
SendTokenView(
keyringStore: keyringStore,
networkStore: cryptoStore.networkStore,
sendTokenStore: cryptoStore.openSendTokenStore(action.initialToken),
onDismiss: { cryptoStore.buySendSwapDestination = nil }
)
case .swap:
SwapCryptoView(
keyringStore: keyringStore,
networkStore: cryptoStore.networkStore,
swapTokensStore: cryptoStore.openSwapTokenStore(action.initialToken),
onDismiss: { cryptoStore.buySendSwapDestination = nil }
)
}
}
)
.background(
Color.clear
.sheet(isPresented: $cryptoStore.isPresentingPendingRequest) {
if cryptoStore.pendingRequest != nil {
RequestContainerView(
keyringStore: keyringStore,
cryptoStore: cryptoStore,
toolbarDismissContent: pendingRequestToolbarDismissContent,
onDismiss: {
cryptoStore.isPresentingPendingRequest = false
}
)
.onDisappear {
cryptoStore.prepare()
}
}
}
)
.environment(
\.buySendSwapDestination,
Binding(
get: { [weak cryptoStore] in cryptoStore?.buySendSwapDestination },
set: { [weak cryptoStore] destination in
if cryptoStore?.isPresentingAssetSearch == true {
cryptoStore?.isPresentingAssetSearch = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
cryptoStore?.buySendSwapDestination = destination
}
} else {
cryptoStore?.buySendSwapDestination = destination
}
})
)
}
}