An advanced Ethereum/EVM mobile wallet
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
alpha-wallet-ios/Trust/Wallet/Coordinators/WalletCoordinator.swift

102 lines
3.5 KiB

// Copyright SIX DAY LLC. All rights reserved.
7 years ago
import Foundation
import UIKit
protocol WalletCoordinatorDelegate: class {
func didFinish(with account: Account, in coordinator: WalletCoordinator)
func didFail(with error: Error, in coordinator: WalletCoordinator)
func didCancel(in coordinator: WalletCoordinator)
}
class WalletCoordinator {
7 years ago
let presenterViewController: UINavigationController
let navigationViewController: UINavigationController
7 years ago
weak var delegate: WalletCoordinatorDelegate?
var entryPoint: WalletEntryPoint?
private let keystore: EtherKeystore
init(
presenterViewController: UINavigationController,
navigationViewController: UINavigationController = NavigationController(),
keystore: EtherKeystore = EtherKeystore()
) {
self.presenterViewController = presenterViewController
self.navigationViewController = navigationViewController
self.keystore = EtherKeystore()
}
func start(_ entryPoint: WalletEntryPoint) {
self.entryPoint = entryPoint
switch entryPoint {
case .createWallet:
presentCreateWallet()
case .importWallet:
presentImportWallet()
case .createInstantWallet:
createInstantWallet()
}
7 years ago
}
func presentCreateWallet() {
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)
7 years ago
}
func presentImportWallet() {
let controller: ImportWalletViewController = .make(delegate: self)
7 years ago
controller.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(dismiss))
navigationViewController.viewControllers = [controller]
presenterViewController.present(navigationViewController, animated: true, completion: nil)
}
func pushImportWallet() {
let controller: ImportWalletViewController = .make(delegate: self)
navigationViewController.pushViewController(controller, animated: true)
}
func createInstantWallet() {
presenterViewController.displayLoading(animated: false)
let password = UUID().uuidString
let account = keystore.createAccout(password: password)
presenterViewController.hideLoading(animated: false)
didCreateAccount(account: account)
}
7 years ago
@objc func dismiss() {
delegate?.didCancel(in: self)
}
7 years ago
func didCreateAccount(account: Account) {
delegate?.didFinish(with: account, in: self)
}
}
extension WalletCoordinator: WelcomeViewControllerDelegate {
func didPressImportWallet(in viewController: WelcomeViewController) {
pushImportWallet()
7 years ago
}
func didPressCreateWallet(in viewController: WelcomeViewController) {
createInstantWallet()
7 years ago
}
}
extension WalletCoordinator: ImportWalletViewControllerDelegate {
func didImportAccount(account: Account, in viewController: ImportWalletViewController) {
didCreateAccount(account: account)
}
}
extension ImportWalletViewController {
static func make(delegate: ImportWalletViewControllerDelegate? = .none) -> ImportWalletViewController {
let controller = ImportWalletViewController()
controller.delegate = delegate
return controller
}
}