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

145 lines
4.4 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(presenterViewController: self.rootNavigationController)
7 years ago
}()
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()
handleNotifications()
resetToWelcomeScreen()
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()
}
func handleNotifications() {
UIApplication.shared.applicationIconBadgeNumber = 0
}
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]
rootNavigationController.setNavigationBarHidden(false, animated: false)
addCoordinator(coordinator)
keystore.recentlyUsedAccount = account
7 years ago
}
func showCreateWallet(entryPoint: WalletEntryPoint) {
7 years ago
walletCoordinator.delegate = self
walletCoordinator.start(entryPoint)
}
func resetToWelcomeScreen() {
rootNavigationController.setNavigationBarHidden(true, animated: false)
rootNavigationController.viewControllers = [welcomeViewController]
7 years ago
}
7 years ago
@objc func reset() {
touchRegistrar.unregister()
pushNotificationRegistrar.unregister()
coordinators.removeAll()
rootNavigationController.dismiss(animated: true, completion: nil)
resetToWelcomeScreen()
7 years ago
}
func didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: Data) {
pushNotificationRegistrar.didRegister(
with: deviceToken,
addresses: keystore.accounts.map { $0.address }
)
}
7 years ago
}
extension AppCoordinator: WelcomeViewControllerDelegate {
func didPressCreateWallet(in viewController: WelcomeViewController) {
showCreateWallet(entryPoint: .createInstantWallet)
}
func didPressImportWallet(in viewController: WelcomeViewController) {
showCreateWallet(entryPoint: .importWallet)
7 years ago
}
}
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) {
coordinator.navigationViewController.dismiss(animated: true, completion: nil)
showTransactions(for: account)
7 years ago
}
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)
}
}