-
Notifications
You must be signed in to change notification settings - Fork 38
/
DependencyContainer.swift
511 lines (402 loc) · 26.6 KB
/
DependencyContainer.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//
// DependencyContainer.swift
// Wallet
//
// Created by Cake Technologies 11/30/17.
// Copyright © 2017 Cake Technologies.
//
import Foundation
import Dip
import SwiftKeychainWrapper
let container = DependencyContainer.configure()
extension DependencyContainer {
static func configure() -> DependencyContainer {
return DependencyContainer { container in
// MARK: KeychainStorage
container.register(.singleton) {
KeychainStorageImpl(keychain: KeychainWrapper.standard) as KeychainStorage
}
// MARK: WalletProxy
container.register(.singleton) {
WalletProxy(origin: EmptyWallet())
}.implements(WalletProxy.self)
// MARK: AuthenticationProtocol, Account, AccountSettingsConfigurable
container.register(.singleton) {
AccountImpl(keychainStorage: try! container.resolve(), proxyWallet: try! container.resolve() as WalletProxy)
}.implements(
AuthenticationProtocol.self,
Account.self,
AccountSettingsConfigurable.self,
CurrencySettingsConfigurable.self)
// MARK: Wallets
container.register { moneroWalletGateway, account, keychainStorage in
Wallets(moneroWalletGateway: moneroWalletGateway, account: account, keychainStorage: keychainStorage)
}.implements(
WalletsCreating.self,
WalletsRecoverable.self,
WalletsLoadable.self,
WalletsRemovable.self)
.implements(EstimatedFeeCalculable.self)
// MARK: RateTicker
container.register(.singleton) { MoneroRateTicker(account: try! container.resolve() as AccountImpl) }
.implements(RateTicker.self)
// MARK: WelcomeViewController
container.register { WelcomeViewController() }
// MARK: PinPasswordViewController
container.register { canClose in PinPasswordViewController(canClose: canClose) }
// MARK: SetupPinPasswordViewController
container.register {
SetupPinPasswordViewController(
account: try! container.resolve() as AccountImpl,
pinPasswordViewController: try! container.resolve(arguments: false) as PinPasswordViewController)
}
// MARK: ExchangeResultViewController
container.register { (trade: ExchangeTrade, amountStr: String) in
ExchangeResultViewController(
account: try! container.resolve() as AccountImpl,
trade: trade,
amountStr: amountStr)
}.resolvingProperties { (container, vc: ExchangeResultViewController) in
vc.presentVerifyPinScreen = { handler in
let verifyPinPasswordViewController = try! container.resolve() as VerifyPinPasswordViewController
verifyPinPasswordViewController.modalPresentationStyle = .overFullScreen
verifyPinPasswordViewController.onVerified = {
verifyPinPasswordViewController.dismiss(animated: true) {
handler()
}
}
vc.present(verifyPinPasswordViewController, animated: true)
}
}
// MARK: AddWalletViewController
container.register { AddWalletViewController() }
// MARK: NewWalletViewController
container.register { (wallets: Wallets) in NewWalletViewController(wallets: wallets as WalletsCreating) }
// MARK: RecoveryViewController
container.register { (wallets: Wallets) in RecoveryViewController(wallets: wallets as WalletsRecoverable) }
// MARK: SeedViewController
container.register { SeedViewController(wallet: try! container.resolve() as WalletProxy) }
container.register { (wallet: WalletProtocol) in SeedViewController(wallet: wallet) }
container.register { (seed: String, name: String) in SeedViewController(seed: seed, name: name) }
// MARK: SummaryViewController
container.register { (wallet: WalletProtocol) in
DashboardViewController(account: try! container.resolve() as AccountImpl, wallet: wallet, rateTicker: try! container.resolve() as RateTicker)
}
container.register {
DashboardViewController(
account: try! container.resolve() as AccountImpl,
wallet: (try! container.resolve() as WalletProxy) as WalletProtocol,
rateTicker: try! container.resolve() as RateTicker)
}.resolvingProperties { (conteiner, vc: DashboardViewController) in
vc.presentTransactionDetails = { [weak vc] transaction in
let transactionDetailsViewController = TransactionDetailsViewController(transaction: transaction)
vc?.navigationController?.pushViewController(transactionDetailsViewController, animated: true)
}
vc.presentTransactionsList = { [weak vc] in
let transactionsList = try! container.resolve() as TransactionsListViewController
vc?.navigationController?.pushViewController(transactionsList, animated: true)
}
}
// MARK: ReceiveViewController
container.register { (wallet: WalletProtocol) in ReceiveViewController(wallet: wallet) }
container.register { ReceiveViewController(wallet: try! container.resolve() as WalletProxy) }
// MARK: SendViewController
container.register {
SendViewController(
account: try! container.resolve() as AccountImpl,
estimatedFeeCalculation: (try! container.resolve() as Account).wallets(),
transactionCreation: (try! container.resolve() as Account).currentWallet,
rateTicker: try! container.resolve() as RateTicker)
}
container.register { (address: String, amount: Amount) in
SendViewController(
address: address,
amount: amount,
account: try! container.resolve() as AccountImpl,
estimatedFeeCalculation: (try! container.resolve() as Account).wallets(),
transactionCreation: (try! container.resolve() as Account).currentWallet,
rateTicker: try! container.resolve() as RateTicker)
}
// MARK: UnlockViewController
container.register { (account: Account & AuthenticationProtocol) in
LoginViewController(account: account)
}
// MARK: AuthenticateViewController
container.register { (account: Account & AuthenticationProtocol) in
AuthenticateViewController(account: account)
}
// MARK: WalletKeyViewController
container.register { WalletKeyViewController(wallet: try! container.resolve() as WalletProxy) }
container.register { (wallet: WalletProtocol) in WalletKeyViewController(wallet: wallet) }
container.register { (wallet) in WalletKeyViewController(wallet: wallet) }
container.register { (name: String, viewKey: WalletKey, spendKey: WalletKey) in WalletKeyViewController(name: name, viewKey: viewKey, spendKey: spendKey) }
// MARK: SettingsViewController
container.register {
SettingsViewController(
accountSettings: try! container.resolve() as AccountImpl,
showSeedIsAllow: !(try! container.resolve() as WalletProxy).isWatchOnly)
}.resolvingProperties { (container: DependencyContainer, vc: SettingsViewController) in
vc.presentWalletsScreen = { [weak vc] in
let walletsListViewController = try! container.resolve() as WalletsViewController
vc?.navigationController?.pushViewController(walletsListViewController, animated: true)
}
vc.presentChangePasswordScreen = { [weak vc] in
let changePasswordViewController = try! container.resolve() as ChangePasswordViewController
changePasswordViewController.onPasswordChanged = { [weak changePasswordViewController] in
changePasswordViewController?.dismiss(animated: true)
}
vc?.present(changePasswordViewController, animated: true)
}
vc.presentNodeSettingsScreen = { [weak vc] in
let nodeSettingsViewController = try! container.resolve() as NodesListViewController
vc?.navigationController?.pushViewController(nodeSettingsViewController, animated: true)
}
vc.presentWalletKeys = { [weak vc] in
let verifyPinPasswordViewController = try! container.resolve() as VerifyPinPasswordViewController
if verifyPinPasswordViewController.canBePresented {
verifyPinPasswordViewController.onVerified = {
let walletKeyViewController = try! container.resolve() as WalletKeyViewController
let navController = UINavigationController(rootViewController: walletKeyViewController)
verifyPinPasswordViewController.dismiss(animated: false) {
vc?.present(navController, animated: true)
}
}
vc?.present(verifyPinPasswordViewController, animated: true)
} else {
let walletKeyViewController = try! container.resolve() as WalletKeyViewController
let navController = UINavigationController(rootViewController: walletKeyViewController)
vc?.present(navController, animated: true)
}
}
vc.presentWalletSeed = {
let verifyPinPasswordViewController = try! container.resolve() as VerifyPinPasswordViewController
if verifyPinPasswordViewController.canBePresented {
let navController = UINavigationController(rootViewController: verifyPinPasswordViewController)
verifyPinPasswordViewController.onVerified = {
let seedViewController = try! container.resolve() as SeedViewController
seedViewController.finishHandler = { [weak seedViewController] in
seedViewController?.dismiss(animated: true) {
navController.viewControllers = []
}
}
navController.setNavigationBarHidden(false, animated: false)
navController.pushViewController(seedViewController, animated: true)
}
vc.present(navController, animated: true)
} else {
let seedViewController = try! container.resolve() as SeedViewController
let navController = UINavigationController(rootViewController: seedViewController)
seedViewController.finishHandler = { [weak seedViewController] in
seedViewController?.dismiss(animated: true) {
navController.viewControllers = []
}
}
vc.present(navController, animated: true)
}
}
vc.presentDonation = {
let donationViewController = try! container.resolve() as DonationViewController
vc.navigationController?.pushViewController(donationViewController, animated: true)
}
}
// MARK: WalletsViewController
container.register { WalletsViewController(account: try! container.resolve()) }
.resolvingProperties { (container: DependencyContainer, vc: WalletsViewController) in
let back = { [weak vc] in
if let rootVc = vc?.navigationController?.viewControllers.first {
rootVc.tabBarController?.selectedIndex = 0
vc?.navigationController?.popToRootViewController(animated: false)
}
}
vc.presentLoadWalletScreen = { index in
let account = try! container.resolve() as Account
let wallets = account.wallets()
let name = index.name
let laodWalletViewController = try! container.resolve(arguments: name, wallets) as LoadWalletViewController
if laodWalletViewController.canBePresented {
laodWalletViewController.onLogined = { [weak laodWalletViewController] in
laodWalletViewController?.dismiss(animated: true) {
back()
}
}
vc.present(laodWalletViewController, animated: true)
} else {
let alert = UIAlertController.showSpinner(message: "Loading wallet - \(index.name)")
vc.present(alert, animated: true)
wallets.loadWallet(withName: name)
.then {
alert.dismiss(animated: true) {
back()
}
}.catch { [weak vc] error in
alert.dismiss(animated: true) {
vc?.showError(error)
}
}
}
}
vc.presentNewWalletScreen = { [weak vc] in
let navController = UINavigationController()
let account = try! container.resolve() as Account
let signUpFlow = try! container.resolve(arguments: navController, account.wallets()) as SignUpFlow
signUpFlow.finalHandler = {
navController.dismiss(animated: true) {
back()
}
}
signUpFlow.changeRoute(.addNewWallet)
vc?.present(signUpFlow.currentViewController, animated: true)
}
vc.presentSeedWalletScreen = { index in
let verifyPinPasswordViewController = try! container.resolve() as VerifyPinPasswordViewController
if verifyPinPasswordViewController.canBePresented {
let navController = UINavigationController(rootViewController: verifyPinPasswordViewController)
verifyPinPasswordViewController.onVerified = { [weak verifyPinPasswordViewController] in
let account = try! container.resolve() as Account
let wallets = account.wallets()
wallets.fetchSeed(for: index)
.then { seed -> Void in
let seedViewController = try! container.resolve(arguments: seed, index.name) as SeedViewController
seedViewController.finishHandler = { [weak seedViewController] in
seedViewController?.dismiss(animated: true) {
navController.viewControllers = []
}
}
navController.setNavigationBarHidden(false, animated: false)
navController.pushViewController(seedViewController, animated: true)
}.catch { error in
verifyPinPasswordViewController?.showError(error)
}
}
vc.present(navController, animated: true)
} else {
let account = try! container.resolve() as Account
let wallets = account.wallets()
wallets.fetchSeed(for: index)
.then { seed -> Void in
let seedViewController = try! container.resolve(arguments: seed, index.name) as SeedViewController
let navController = UINavigationController(rootViewController: seedViewController)
seedViewController.finishHandler = { [weak seedViewController] in
seedViewController?.dismiss(animated: true) {
navController.viewControllers = []
}
}
vc.present(navController, animated: true)
}.catch { error in
vc.showError(error)
}
}
}
vc.presentRemoveWalletScreen = { [weak vc] index, completionHandler in
let verifyPinPasswordViewController = try! container.resolve() as VerifyPinPasswordViewController
verifyPinPasswordViewController.onVerified = {
let account = try! container.resolve() as Account
let wallets = account.wallets()
let alert = UIAlertController.showSpinner(message: "Removing wallet")
if verifyPinPasswordViewController.canBePresented {
verifyPinPasswordViewController.present(alert, animated: true)
} else {
vc?.present(alert, animated: true)
}
wallets.removeWallet(withIndex: index)
.then { _ in
alert.dismiss(animated: false) {
verifyPinPasswordViewController.dismiss(animated: true)
completionHandler?()
}
}.catch { error in
alert.dismiss(animated: false) {
verifyPinPasswordViewController.showError(error)
completionHandler?()
}
}
}
vc?.present(verifyPinPasswordViewController, animated: true)
}
}
// MARK: LoadWalletViewController
container.register { (name: String, wallets: Wallets) in
LoadWalletViewController(
walletName: name,
wallets: wallets as WalletsLoadable,
verifyPasswordViewController: try! container.resolve() as VerifyPinPasswordViewController)
}
// MARK: VerifyPinPasswordViewController
container.register {
VerifyPinPasswordViewController(
account: try! container.resolve() as AccountImpl,
pinPasswordViewController: try! container.resolve(arguments: true) as PinPasswordViewController)
}
// MARK: ChangePasswordViewController
container.register { ChangePasswordViewController(
account: try! container.resolve() as AccountImpl,
pinPasswordViewController: try! container.resolve(arguments: true) as PinPasswordViewController)
}
// MARK: TransactionDetailsViewController
container.register { (tx: TransactionDescription) in TransactionDetailsViewController(transaction: tx) }
// MARK: NodeSettingsViewController
container.register { NodeSettingsViewController(account: try! container.resolve() as Account) }
// MARK: DisclaimerViewController
container.register { DisclaimerViewController() }
// MARK: RecoveryWalletOptionsViewController
container.register { RecoveryWalletOptionsViewController() }
// MARK: RecoveryWalletFromKeysViewController
container.register { (wallets: Wallets) in RecoveryWalletFromKeysViewController(wallets: wallets) }
// MARK: DonationViewController
container.register { DonationViewController(canSend: !(try! container.resolve () as WalletProxy).isWatchOnly) }
// MARK: TransactionsListViewController
container.register {
TransactionsListViewController(wallet: try! container.resolve() as WalletProxy)
}.resolvingProperties { (container, vc: TransactionsListViewController) in
vc.presentTransactionDetails = { [weak vc] transaction in
let transactionDetailsViewController = TransactionDetailsViewController(transaction: transaction)
vc?.navigationController?.pushViewController(transactionDetailsViewController, animated: true)
}
}
// MARK: ExchangeViewController
container.register { ExchangeViewController(
account: try! container.resolve() as AccountImpl,
wallet: try! container.resolve() as WalletProxy,
transactionCreation: try! container.resolve() as WalletProxy)
}
// MARK: BuyViewController
container.register { BuyViewController(wallet: try! container.resolve() as WalletProxy) }
// MARK: ServicesViewController
container.register { ServicesViewController(
exchangeViewController: try! container.resolve() as ExchangeViewController,
buyViewController: try! container.resolve() as BuyViewController)
}
// MARK: NewNodeSettingsViewController
container.register {
NewNodeSettingsViewController(nodesList: $0)
}
// MARK: NodesListViewController
container.register {
NodesListViewController(
account: try! container.resolve() as AccountImpl,
nodesList: NodesList.shared)
}.resolvingProperties({ (container, vc: NodesListViewController) in
vc.presentNewNodeScreen = { [weak vc] in
if let _vc = vc {
let newNodeVC = try! container.resolve(arguments: _vc.nodesList) as NewNodeSettingsViewController
_vc.navigationController?.pushViewController(newNodeVC, animated: true)
}
}
})
// MARK: NodeConnectionControl
container.register {
NodeConnectionControl(account: try! container.resolve() as AccountImpl, nodesList: NodesList.shared)
}
// Flows
container.register { rootViewController, wallets in SignUpFlow(rootViewController: rootViewController, wallets: wallets) }
container.register { MainFlow(rootViewController: UITabBarController(viewControllers: [
UINavigationController(rootViewController: try! container.resolve() as DashboardViewController),
UINavigationController(rootViewController: try! container.resolve() as SendViewController),
UINavigationController(rootViewController: try! container.resolve() as ReceiveViewController),
UINavigationController(rootViewController: try! container.resolve() as ServicesViewController),
UINavigationController(rootViewController: try! container.resolve() as SettingsViewController)
])) }
container.register { RootFlow(window: $0, account: try! container.resolve() as AccountImpl) }
}
}
}