Conform to Coordinator protocol

pull/2/head
Michael Scoff 7 years ago
parent 5c9a7a5041
commit cda733e27d
  1. 98
      Trust/Wallet/Coordinators/WalletCoordinator.swift

@ -9,63 +9,60 @@ protocol WalletCoordinatorDelegate: class {
func didCancel(in coordinator: WalletCoordinator)
}
class WalletCoordinator {
class WalletCoordinator: Coordinator {
let presenterViewController: UINavigationController
let navigationViewController: UINavigationController
let navigationController: UINavigationController
weak var delegate: WalletCoordinatorDelegate?
var entryPoint: WalletEntryPoint?
private let keystore: EtherKeystore
var coordinators: [Coordinator] = []
init(
presenterViewController: UINavigationController,
navigationViewController: UINavigationController = NavigationController(),
navigationController: UINavigationController = NavigationController(),
keystore: EtherKeystore = EtherKeystore()
) {
self.presenterViewController = presenterViewController
self.navigationViewController = navigationViewController
self.navigationViewController.modalPresentationStyle = .formSheet
self.navigationController = navigationController
self.navigationController.modalPresentationStyle = .formSheet
self.keystore = EtherKeystore()
}
func start(_ entryPoint: WalletEntryPoint) {
self.entryPoint = entryPoint
switch entryPoint {
case .createWallet:
presentCreateWallet()
case .importWallet:
presentImportWallet()
case .createInstantWallet:
createInstantWallet()
}
}
func presentCreateWallet() {
case .welcome:
let controller = WelcomeViewController()
controller.delegate = self
controller.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(dismiss))
navigationViewController.viewControllers = [controller]
presenterViewController.present(navigationViewController, animated: true, completion: nil)
}
func presentImportWallet() {
navigationController.viewControllers = [controller]
case .importWallet:
let controller: ImportWalletViewController = .make(delegate: self)
controller.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(dismiss))
navigationViewController.viewControllers = [controller]
presenterViewController.present(navigationViewController, animated: true, completion: nil)
navigationController.viewControllers = [controller]
case .createInstantWallet:
createInstantWallet()
}
}
func pushImportWallet() {
let controller: ImportWalletViewController = .make(delegate: self)
navigationViewController.pushViewController(controller, animated: true)
navigationController.pushViewController(controller, animated: true)
}
func createInstantWallet() {
presenterViewController.displayLoading(animated: false)
navigationController.displayLoading(animated: false)
let password = UUID().uuidString
let account = keystore.createAccout(password: password)
presenterViewController.hideLoading(animated: false)
didCreateAccount(account: account)
navigationController.hideLoading(animated: false)
pushBackup(for: account)
}
func pushBackup(for account: Account) {
let controller = BackupViewController(account: account)
controller.delegate = self
controller.navigationItem.backBarButtonItem = nil
controller.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
navigationController.pushViewController(controller, animated: true)
}
@objc func dismiss() {
@ -75,6 +72,16 @@ class WalletCoordinator {
func didCreateAccount(account: Account) {
delegate?.didFinish(with: account, in: self)
}
func backup(account: Account) {
let coordinator = BackupCoordinator(
navigationController: navigationController,
account: account
)
coordinator.delegate = self
addCoordinator(coordinator)
coordinator.start()
}
}
extension WalletCoordinator: WelcomeViewControllerDelegate {
@ -93,6 +100,39 @@ extension WalletCoordinator: ImportWalletViewControllerDelegate {
}
}
extension WalletCoordinator: BackupViewControllerDelegate {
func didPressBackup(account: Account, in viewController: BackupViewController) {
backup(account: account)
}
func didPressLater(account: Account, in viewController: BackupViewController) {
navigationController.confirm(
title: "Watch out!",
message: "If this device is replaced or this app is deleted, neither you nor Trust Wallet can recover your funds without a backup",
okTitle: "I understand",
okStyle: .destructive
) { result in
switch result {
case .success:
self.delegate?.didFinish(with: account, in: self)
case .failure:
break
}
}
}
}
extension WalletCoordinator: BackupCoordinatorDelegate {
func didCancel(coordinator: BackupCoordinator) {
removeCoordinator(coordinator)
}
func didFinish(account: Account, in coordinator: BackupCoordinator) {
removeCoordinator(coordinator)
didCreateAccount(account: account)
}
}
extension ImportWalletViewController {
static func make(delegate: ImportWalletViewControllerDelegate? = .none) -> ImportWalletViewController {
let controller = ImportWalletViewController()

Loading…
Cancel
Save