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/AppCoordinator.swift

130 lines
3.9 KiB

// Copyright SIX DAY LLC. All rights reserved.
7 years ago
import Foundation
import UIKit
class AppCoordinator: NSObject, Coordinator {
let rootNavigationController: UINavigationController
7 years ago
lazy var welcomeViewController: WelcomeViewController = {
let controller = WelcomeViewController()
controller.delegate = self
return controller
7 years ago
}()
7 years ago
lazy var walletCoordinator: WalletCoordinator = {
return WalletCoordinator(rootNavigationController: self.rootNavigationController)
}()
let touchRegistrar = TouchRegistrar()
let pushNotificationRegistrar = PushNotificationsRegistrar()
private var keystore: Keystore
var coordinators: [Coordinator] = []
init(
window: UIWindow,
keystore: Keystore = EtherKeystore(),
rootNavigationController: UINavigationController = NavigationController()
) {
self.keystore = keystore
self.rootNavigationController = rootNavigationController
super.init()
7 years ago
window.rootViewController = rootNavigationController
window.makeKeyAndVisible()
7 years ago
}
7 years ago
func start() {
performMigration()
inializers()
7 years ago
rootNavigationController.viewControllers = [welcomeViewController]
7 years ago
if keystore.hasAccounts {
showTransactions(for: keystore.recentlyUsedAccount ?? keystore.accounts.first!)
7 years ago
}
}
func performMigration() {
MigrationInitializer().perform()
LokaliseInitializer().perform()
}
func inializers() {
touchRegistrar.register()
}
7 years ago
func showTransactions(for account: Account) {
let session = WalletSession(
account: account
)
let coordinator = TransactionCoordinator(
session: session,
rootNavigationController: rootNavigationController
)
coordinator.delegate = self
rootNavigationController.viewControllers = [coordinator.rootViewController]
addCoordinator(coordinator)
keystore.recentlyUsedAccount = account
7 years ago
}
7 years ago
func showCreateWallet() {
walletCoordinator.start()
walletCoordinator.delegate = self
}
7 years ago
@objc func reset() {
touchRegistrar.unregister()
pushNotificationRegistrar.unregister()
coordinators.removeAll()
rootNavigationController.dismiss(animated: true, completion: nil)
7 years ago
rootNavigationController.viewControllers = [welcomeViewController]
}
func didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: Data) {
pushNotificationRegistrar.didRegister(
with: deviceToken,
addresses: keystore.accounts.map { $0.address }
)
}
7 years ago
}
extension AppCoordinator: WelcomeViewControllerDelegate {
func didPressStart(in viewController: WelcomeViewController) {
showCreateWallet()
}
}
extension AppCoordinator: TransactionCoordinatorDelegate {
func didCancel(in coordinator: TransactionCoordinator) {
coordinator.navigationController.dismiss(animated: true, completion: nil)
7 years ago
coordinator.stop()
removeCoordinator(coordinator)
reset()
}
func didRestart(with account: Account, in coordinator: TransactionCoordinator) {
coordinator.navigationController.dismiss(animated: true, completion: nil)
7 years ago
coordinator.stop()
removeCoordinator(coordinator)
showTransactions(for: account)
}
7 years ago
}
extension AppCoordinator: WalletCoordinatorDelegate {
func didFinish(with account: Account, in coordinator: WalletCoordinator) {
showTransactions(for: account)
coordinator.navigationViewController.dismiss(animated: true, completion: nil)
}
7 years ago
func didFail(with error: Error, in coordinator: WalletCoordinator) {
coordinator.navigationViewController.dismiss(animated: true, completion: nil)
}
7 years ago
func didCancel(in coordinator: WalletCoordinator) {
coordinator.navigationViewController.dismiss(animated: true, completion: nil)
}
}