forked from trustwallet/trust-wallet-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InCoordinator.swift
372 lines (323 loc) · 13.1 KB
/
InCoordinator.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
// Copyright DApps Platform Inc. All rights reserved.
import Foundation
import TrustCore
import UIKit
import RealmSwift
import URLNavigator
import TrustWalletSDK
import Result
protocol InCoordinatorDelegate: class {
func didCancel(in coordinator: InCoordinator)
func didUpdateAccounts(in coordinator: InCoordinator)
}
class InCoordinator: Coordinator {
let navigationController: NavigationController
var coordinators: [Coordinator] = []
let initialWallet: WalletInfo
var keystore: Keystore
let config: Config
let appTracker: AppTracker
let navigator: Navigator
weak var delegate: InCoordinatorDelegate?
private var pendingTransactionsObserver: NotificationToken?
var browserCoordinator: BrowserCoordinator? {
return self.coordinators.compactMap { $0 as? BrowserCoordinator }.first
}
var settingsCoordinator: SettingsCoordinator? {
return self.coordinators.compactMap { $0 as? SettingsCoordinator }.first
}
var tokensCoordinator: TokensCoordinator? {
return self.coordinators.compactMap { $0 as? TokensCoordinator }.first
}
var tabBarController: UITabBarController? {
return self.navigationController.viewControllers.first as? UITabBarController
}
var localSchemeCoordinator: LocalSchemeCoordinator?
lazy var helpUsCoordinator: HelpUsCoordinator = {
return HelpUsCoordinator(
navigationController: navigationController,
appTracker: appTracker
)
}()
let events: [BranchEvent] = []
lazy var walletsCoordinator: WalletsCoordinator = {
let coordinator = WalletsCoordinator(keystore: keystore)
coordinator.delegate = self
return coordinator
}()
init(
navigationController: NavigationController = NavigationController(),
wallet: WalletInfo,
keystore: Keystore,
config: Config = .current,
appTracker: AppTracker = AppTracker(),
navigator: Navigator = Navigator(),
events: [BranchEvent] = []
) {
self.navigationController = navigationController
self.initialWallet = wallet
self.keystore = keystore
self.config = config
self.appTracker = appTracker
self.navigator = navigator
self.register(with: navigator)
}
func start() {
showTabBar(for: initialWallet)
checkDevice()
helpUsCoordinator.start()
addCoordinator(helpUsCoordinator)
walletsCoordinator.start()
}
func showTabBar(for account: WalletInfo) {
var configS = config
configS.chainID = account.chainID
let migration = MigrationInitializer(account: account, chainID: config.chainID)
migration.perform()
let sharedMigration = SharedMigrationInitializer()
sharedMigration.perform()
let realm = self.realm(for: migration.config)
let sharedRealm = self.realm(for: sharedMigration.config)
let walletStorage = WalletStorage(realm: sharedRealm)
let tokensStorage = TokensDataStore(realm: realm, config: config)
let balanceCoordinator = TokensBalanceService()
let viewModel = InCoordinatorViewModel(config: config)
let trustNetwork = TrustNetwork(
provider: TrustProviderFactory.makeProvider(),
APIProvider: TrustProviderFactory.makeAPIProvider(),
balanceService: balanceCoordinator,
address: account.currentAccount.address,
config: config
)
let balance = BalanceCoordinator(account: account, config: config, storage: tokensStorage)
let transactionsStorage = TransactionsStorage(
realm: realm,
account: account
)
let nonceProvider = GetNonceProvider(storage: transactionsStorage)
let session = WalletSession(
account: account,
config: config,
balanceCoordinator: balance,
nonceProvider: nonceProvider
)
transactionsStorage.removeTransactions(for: [.failed, .unknown])
let tabBarController = TabBarController()
tabBarController.tabBar.isTranslucent = false
let browserCoordinator = BrowserCoordinator(session: session, keystore: keystore, navigator: navigator, sharedRealm: sharedRealm)
browserCoordinator.delegate = self
browserCoordinator.start()
browserCoordinator.rootViewController.tabBarItem = viewModel.browserBarItem
addCoordinator(browserCoordinator)
let walletCoordinator = TokensCoordinator(
session: session,
keystore: keystore,
tokensStorage: tokensStorage,
network: trustNetwork,
transactionsStore: transactionsStorage
)
walletCoordinator.rootViewController.tabBarItem = viewModel.walletBarItem
walletCoordinator.delegate = self
walletCoordinator.start()
addCoordinator(walletCoordinator)
let settingsCoordinator = SettingsCoordinator(
keystore: keystore,
session: session,
storage: transactionsStorage,
walletStorage: walletStorage,
balanceCoordinator: balanceCoordinator,
sharedRealm: sharedRealm
)
settingsCoordinator.rootViewController.tabBarItem = viewModel.settingsBarItem
settingsCoordinator.delegate = self
settingsCoordinator.start()
addCoordinator(settingsCoordinator)
tabBarController.viewControllers = [
browserCoordinator.navigationController.childNavigationController,
walletCoordinator.navigationController.childNavigationController,
settingsCoordinator.navigationController.childNavigationController,
]
navigationController.setViewControllers([tabBarController], animated: false)
navigationController.setNavigationBarHidden(true, animated: false)
showTab(.wallet(.none))
keystore.recentlyUsedWallet = account
let localSchemeCoordinator = LocalSchemeCoordinator(
navigationController: navigationController,
keystore: keystore,
session: session
)
localSchemeCoordinator.delegate = self
addCoordinator(localSchemeCoordinator)
self.localSchemeCoordinator = localSchemeCoordinator
observePendingTransactions(from: transactionsStorage)
}
func changeWallet() {
walletsCoordinator.walletController.tableView.scrollOnTop()
navigationController.present(walletsCoordinator.navigationController, animated: true)
}
func showTab(_ selectTab: Tabs) {
guard let viewControllers = tabBarController?.viewControllers else { return }
guard let nav = viewControllers[selectTab.index] as? UINavigationController else { return }
switch selectTab {
case .browser(let url):
if let url = url {
browserCoordinator?.openURL(url)
}
case .wallet(let action):
switch action {
case .none: break
case .addToken(let address):
tokensCoordinator?.addTokenContract(for: address)
}
case .settings:
break
}
tabBarController?.selectedViewController = nav
}
func restart(for account: WalletInfo) {
settingsCoordinator?.rootViewController.navigationItem.leftBarButtonItem = nil
settingsCoordinator?.rootViewController.networkStateView = nil
localSchemeCoordinator?.delegate = nil
localSchemeCoordinator = nil
navigationController.dismiss(animated: false, completion: nil)
removeAllCoordinators()
navigationController.viewControllers.removeAll()
showTabBar(for: account)
}
func checkDevice() {
let deviceChecker = CheckDeviceCoordinator(
navigationController: navigationController,
jailbreakChecker: DeviceChecker()
)
deviceChecker.start()
}
func showPaymentFlow(for type: PaymentFlow) {
guard let tokensCoordinator = tokensCoordinator else { return }
let nav = tokensCoordinator.navigationController
let session = tokensCoordinator.session
let tokenStorage = tokensCoordinator.store
switch (type, session.account.type) {
case (.send(let type), .privateKey(let account)),
(.send(let type), .hd(let account)):
let coordinator = SendCoordinator(
transferType: type,
navigationController: nav,
session: session,
keystore: keystore,
storage: tokenStorage,
account: session.account.currentAccount
)
coordinator.delegate = self
addCoordinator(coordinator)
nav.pushCoordinator(coordinator: coordinator, animated: true)
case (.request(let token), _):
let coordinator = RequestCoordinator(
session: session,
token: token
)
addCoordinator(coordinator)
nav.pushCoordinator(coordinator: coordinator, animated: true)
case (.send, .address):
nav.displayError(error: InCoordinatorError.onlyWatchAccount)
}
}
private func handlePendingTransaction(transaction: SentTransaction) {
guard let address = initialWallet.address as? EthereumAddress else { return }
let transaction = SentTransaction.from(from: address, transaction: transaction)
tokensCoordinator?.transactionsStore.add([transaction])
}
private func realm(for config: Realm.Configuration) -> Realm {
return try! Realm(configuration: config)
}
@discardableResult
func handleEvent(_ event: BranchEvent) -> Bool {
switch event {
case .openURL(let url):
showTab(.browser(openURL: url))
case .newToken(let address):
showTab(.wallet(.addToken(address)))
}
return true
}
private func observePendingTransactions(from storage: TransactionsStorage) {
pendingTransactionsObserver = storage.transactions.observe { [weak self] _ in
let items = storage.pendingObjects
self?.tabBarController?.tabBar.items?[Tabs.wallet(.none).index].badgeValue = items.isEmpty ? nil : String(items.count)
}
}
deinit {
pendingTransactionsObserver?.invalidate()
pendingTransactionsObserver = nil
}
}
extension InCoordinator: LocalSchemeCoordinatorDelegate {
func didCancel(in coordinator: LocalSchemeCoordinator) {
coordinator.navigationController.dismiss(animated: true, completion: nil)
removeCoordinator(coordinator)
}
}
extension InCoordinator: SettingsCoordinatorDelegate {
func didCancel(in coordinator: SettingsCoordinator) {
removeCoordinator(coordinator)
coordinator.navigationController.dismiss(animated: true, completion: nil)
delegate?.didCancel(in: self)
}
func didRestart(with account: WalletInfo, in coordinator: SettingsCoordinator) {
restart(for: account)
}
func didPressURL(_ url: URL, in coordinator: SettingsCoordinator) {
showTab(.browser(openURL: url))
}
}
extension InCoordinator: TokensCoordinatorDelegate {
func didPress(for type: PaymentFlow, in coordinator: TokensCoordinator) {
showPaymentFlow(for: type)
}
func didPressDiscover(in coordinator: TokensCoordinator) {
guard let url = Config().openseaURL else { return }
showTab(.browser(openURL: url))
}
func didPress(url: URL, in coordinator: TokensCoordinator) {
showTab(.browser(openURL: url))
}
func didPressChangeWallet(in coordinator: TokensCoordinator) {
changeWallet()
}
}
extension InCoordinator: SendCoordinatorDelegate {
func didFinish(_ result: Result<ConfirmResult, AnyError>, in coordinator: SendCoordinator) {
switch result {
case .success(let confirmResult):
switch confirmResult {
case .sentTransaction(let transaction):
handlePendingTransaction(transaction: transaction)
// TODO. Pop 2 view controllers
coordinator.navigationController.childNavigationController.popToRootViewController(animated: true)
removeCoordinator(coordinator)
case .signedTransaction:
break
}
case .failure(let error):
coordinator.navigationController.topViewController?.displayError(error: error)
}
}
}
extension InCoordinator: BrowserCoordinatorDelegate {
func didSentTransaction(transaction: SentTransaction, in coordinator: BrowserCoordinator) {
handlePendingTransaction(transaction: transaction)
}
}
extension InCoordinator: WalletsCoordinatorDelegate {
func didUpdateAccounts(in coordinator: WalletsCoordinator) {
delegate?.didUpdateAccounts(in: self)
}
func didSelect(wallet: WalletInfo, in coordinator: WalletsCoordinator) {
coordinator.navigationController.dismiss(animated: true)
// removeCoordinator(coordinator)
restart(for: wallet)
}
func didCancel(in coordinator: WalletsCoordinator) {
navigationController.dismiss(animated: true)
// removeCoordinator(coordinator)
}
}