diff --git a/Trust/Accounts/Coordinators/AccountsCoordinator.swift b/Trust/Accounts/Coordinators/AccountsCoordinator.swift index dc1eb6847..5c105115d 100644 --- a/Trust/Accounts/Coordinators/AccountsCoordinator.swift +++ b/Trust/Accounts/Coordinators/AccountsCoordinator.swift @@ -6,6 +6,7 @@ import UIKit protocol AccountsCoordinatorDelegate: class { func didCancel(in coordinator: AccountsCoordinator) func didSelectAccount(account: Account, in coordinator: AccountsCoordinator) + func didAddAccount(account: Account, in coordinator: AccountsCoordinator) func didDeleteAccount(account: Account, in coordinator: AccountsCoordinator) } @@ -23,13 +24,6 @@ class AccountsCoordinator: Coordinator { return controller }() - lazy var rootNavigationController: UINavigationController = { - let controller = self.accountsViewController - let nav = NavigationController(rootViewController: controller) - nav.modalPresentationStyle = .formSheet - return nav - }() - weak var delegate: AccountsCoordinatorDelegate? init(navigationController: UINavigationController) { @@ -37,7 +31,7 @@ class AccountsCoordinator: Coordinator { } func start() { - navigationController.present(rootNavigationController, animated: true, completion: nil) + navigationController.pushViewController(accountsViewController, animated: false) } @objc func dismiss() { @@ -53,7 +47,7 @@ class AccountsCoordinator: Coordinator { coordinator.delegate = self addCoordinator(coordinator) coordinator.start(.welcome) - rootNavigationController.present(coordinator.navigationController, animated: true, completion: nil) + navigationController.present(coordinator.navigationController, animated: true, completion: nil) } } @@ -69,6 +63,7 @@ extension AccountsCoordinator: AccountsViewControllerDelegate { extension AccountsCoordinator: WalletCoordinatorDelegate { func didFinish(with account: Account, in coordinator: WalletCoordinator) { + delegate?.didAddAccount(account: account, in: self) accountsViewController.fetch() coordinator.navigationController.dismiss(animated: true, completion: nil) removeCoordinator(coordinator) diff --git a/Trust/AppCoordinator.swift b/Trust/AppCoordinator.swift index 53f2388b7..abe66667e 100644 --- a/Trust/AppCoordinator.swift +++ b/Trust/AppCoordinator.swift @@ -5,7 +5,7 @@ import UIKit class AppCoordinator: NSObject, Coordinator { - let rootNavigationController: UINavigationController + let navigationController: UINavigationController lazy var welcomeViewController: WelcomeViewController = { let controller = WelcomeViewController() @@ -18,17 +18,21 @@ class AppCoordinator: NSObject, Coordinator { private var keystore: Keystore + lazy var storage: TransactionsStorage = { + return TransactionsStorage() + }() + var coordinators: [Coordinator] = [] init( window: UIWindow, keystore: Keystore = EtherKeystore(), - rootNavigationController: UINavigationController = NavigationController() + navigationController: UINavigationController = NavigationController() ) { self.keystore = keystore - self.rootNavigationController = rootNavigationController + self.navigationController = navigationController super.init() - window.rootViewController = rootNavigationController + window.rootViewController = navigationController window.makeKeyAndVisible() } @@ -42,6 +46,8 @@ class AppCoordinator: NSObject, Coordinator { if keystore.hasAccounts { showTransactions(for: keystore.recentlyUsedAccount ?? keystore.accounts.first!) } + + pushNotificationRegistrar.reRegister() } func performMigration() { @@ -63,18 +69,19 @@ class AppCoordinator: NSObject, Coordinator { ) let coordinator = TransactionCoordinator( session: session, - rootNavigationController: rootNavigationController + rootNavigationController: navigationController, + storage: storage ) coordinator.delegate = self - rootNavigationController.viewControllers = [coordinator.rootViewController] - rootNavigationController.setNavigationBarHidden(false, animated: false) + navigationController.viewControllers = [coordinator.rootViewController] + navigationController.setNavigationBarHidden(false, animated: false) addCoordinator(coordinator) keystore.recentlyUsedAccount = account } func showCreateWallet() { - let coordinator = WalletCoordinator(navigationController: self.rootNavigationController) + let coordinator = WalletCoordinator(navigationController: self.navigationController) coordinator.delegate = self coordinator.start(.createInstantWallet) addCoordinator(coordinator) @@ -84,20 +91,20 @@ class AppCoordinator: NSObject, Coordinator { let coordinator = WalletCoordinator() coordinator.delegate = self coordinator.start(.importWallet) - rootNavigationController.present(coordinator.navigationController, animated: true, completion: nil) + navigationController.present(coordinator.navigationController, animated: true, completion: nil) addCoordinator(coordinator) } func resetToWelcomeScreen() { - rootNavigationController.setNavigationBarHidden(true, animated: false) - rootNavigationController.viewControllers = [welcomeViewController] + navigationController.setNavigationBarHidden(true, animated: false) + navigationController.viewControllers = [welcomeViewController] } @objc func reset() { touchRegistrar.unregister() pushNotificationRegistrar.unregister() coordinators.removeAll() - rootNavigationController.dismiss(animated: true, completion: nil) + navigationController.dismiss(animated: true, completion: nil) resetToWelcomeScreen() } @@ -107,6 +114,19 @@ class AppCoordinator: NSObject, Coordinator { addresses: keystore.accounts.map { $0.address } ) } + + func showAccounts() { + let nav = NavigationController() + let coordinator = AccountsCoordinator(navigationController: nav) + coordinator.delegate = self + coordinator.start() + addCoordinator(coordinator) + navigationController.present(coordinator.navigationController, animated: true, completion: nil) + } + + func cleanStorage(for account: Account) { + storage.deleteAll() + } } extension AppCoordinator: WelcomeViewControllerDelegate { @@ -121,6 +141,7 @@ extension AppCoordinator: WelcomeViewControllerDelegate { extension AppCoordinator: TransactionCoordinatorDelegate { func didCancel(in coordinator: TransactionCoordinator) { + pushNotificationRegistrar.reRegister() coordinator.navigationController.dismiss(animated: true, completion: nil) coordinator.stop() removeCoordinator(coordinator) @@ -133,6 +154,10 @@ extension AppCoordinator: TransactionCoordinatorDelegate { removeCoordinator(coordinator) showTransactions(for: account) } + + func didPressAccounts(in coordinator: TransactionCoordinator) { + showAccounts() + } } extension AppCoordinator: WalletCoordinatorDelegate { @@ -152,3 +177,28 @@ extension AppCoordinator: WalletCoordinatorDelegate { removeCoordinator(coordinator) } } + +extension AppCoordinator: AccountsCoordinatorDelegate { + func didAddAccount(account: Account, in coordinator: AccountsCoordinator) { + pushNotificationRegistrar.reRegister() + } + + func didDeleteAccount(account: Account, in coordinator: AccountsCoordinator) { + pushNotificationRegistrar.reRegister() + guard !coordinator.accountsViewController.hasAccounts else { return } + coordinator.navigationController.dismiss(animated: true, completion: nil) + cleanStorage(for: account) + } + + func didCancel(in coordinator: AccountsCoordinator) { + coordinator.navigationController.dismiss(animated: true, completion: nil) + removeCoordinator(coordinator) + } + + func didSelectAccount(account: Account, in coordinator: AccountsCoordinator) { + coordinator.navigationController.dismiss(animated: true, completion: nil) + cleanStorage(for: account) + removeCoordinator(coordinator) + showTransactions(for: account) + } +} diff --git a/Trust/Settings/Coordinators/PushNotificationsRegistrar.swift b/Trust/Settings/Coordinators/PushNotificationsRegistrar.swift index e61ec7d8f..fd38f3b4d 100644 --- a/Trust/Settings/Coordinators/PushNotificationsRegistrar.swift +++ b/Trust/Settings/Coordinators/PushNotificationsRegistrar.swift @@ -9,6 +9,15 @@ class PushNotificationsRegistrar { let client = PushNotificationsClient() let config = Config() + var isRegisteredForRemoteNotifications: Bool { + return UIApplication.shared.isRegisteredForRemoteNotifications + } + + func reRegister() { + guard isRegisteredForRemoteNotifications else { return } + register() + } + func register() { if #available(iOS 10, *) { UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { _ in } @@ -42,6 +51,7 @@ class PushNotificationsRegistrar { wallets: addresses.map { $0.address }, chainID: config.chainID ) + client.register(device: device) } } diff --git a/Trust/Transactions/Coordinators/TransactionCoordinator.swift b/Trust/Transactions/Coordinators/TransactionCoordinator.swift index 2e17fe173..1f8156c81 100644 --- a/Trust/Transactions/Coordinators/TransactionCoordinator.swift +++ b/Trust/Transactions/Coordinators/TransactionCoordinator.swift @@ -7,19 +7,23 @@ import Result protocol TransactionCoordinatorDelegate: class { func didCancel(in coordinator: TransactionCoordinator) func didRestart(with account: Account, in coordinator: TransactionCoordinator) + func didPressAccounts(in coordinator: TransactionCoordinator) } class TransactionCoordinator: Coordinator { private let keystore: Keystore - + private let storage: TransactionsStorage lazy var rootViewController: TransactionsViewController = { let controller = self.makeTransactionsController(with: self.session.account) return controller }() lazy var dataCoordinator: TransactionDataCoordinator = { - let coordinator = TransactionDataCoordinator(account: self.session.account) + let coordinator = TransactionDataCoordinator( + account: self.session.account, + storage: self.storage + ) return coordinator }() @@ -29,21 +33,19 @@ class TransactionCoordinator: Coordinator { return SettingsCoordinator(navigationController: self.navigationController) }() - lazy var accountsCoordinator: AccountsCoordinator = { - return AccountsCoordinator(navigationController: self.navigationController) - }() - let session: WalletSession let navigationController: UINavigationController var coordinators: [Coordinator] = [] init( session: WalletSession, - rootNavigationController: UINavigationController + rootNavigationController: UINavigationController, + storage: TransactionsStorage = TransactionsStorage() ) { self.session = session self.keystore = EtherKeystore() self.navigationController = rootNavigationController + self.storage = storage NotificationCenter.default.addObserver(self, selector: #selector(didEnterForeground), name: .UIApplicationWillEnterForeground, object: nil) } @@ -61,8 +63,7 @@ class TransactionCoordinator: Coordinator { } @objc func showAccounts() { - accountsCoordinator.start() - accountsCoordinator.delegate = self + delegate?.didPressAccounts(in: self) } @objc func showSettings() { @@ -120,7 +121,6 @@ class TransactionCoordinator: Coordinator { } func restart(for account: Account) { - clean() delegate?.didRestart(with: account, in: self) } @@ -168,27 +168,6 @@ extension TransactionCoordinator: TransactionsViewControllerDelegate { func reset() { delegate?.didCancel(in: self) } - - func clean() { - dataCoordinator.storage.deleteAll() - } -} - -extension TransactionCoordinator: AccountsCoordinatorDelegate { - func didCancel(in coordinator: AccountsCoordinator) { - coordinator.navigationController.dismiss(animated: true, completion: nil) - } - - func didSelectAccount(account: Account, in coordinator: AccountsCoordinator) { - restart(for: account) - } - - func didDeleteAccount(account: Account, in coordinator: AccountsCoordinator) { - guard !coordinator.accountsViewController.hasAccounts else { return } - coordinator.navigationController.dismiss(animated: true, completion: nil) - clean() - reset() - } } extension TransactionCoordinator: PaymentCoordinatorDelegate { diff --git a/Trust/Transactions/Coordinators/TransactionDataCoordinator.swift b/Trust/Transactions/Coordinators/TransactionDataCoordinator.swift index ed6ad4d67..e0974de9c 100644 --- a/Trust/Transactions/Coordinators/TransactionDataCoordinator.swift +++ b/Trust/Transactions/Coordinators/TransactionDataCoordinator.swift @@ -17,7 +17,7 @@ protocol TransactionDataCoordinatorDelegate: class { class TransactionDataCoordinator { - let storage = TransactionsStorage() + let storage: TransactionsStorage let account: Account var viewModel: TransactionsViewModel { return .init(transactions: self.storage.objects) @@ -27,8 +27,12 @@ class TransactionDataCoordinator { weak var delegate: TransactionDataCoordinatorDelegate? - init(account: Account) { + init( + account: Account, + storage: TransactionsStorage = TransactionsStorage() + ) { self.account = account + self.storage = storage } func start() { diff --git a/Trust/Wallet/Coordinators/WalletCoordinator.swift b/Trust/Wallet/Coordinators/WalletCoordinator.swift index 9e7f255d5..e310523fc 100644 --- a/Trust/Wallet/Coordinators/WalletCoordinator.swift +++ b/Trust/Wallet/Coordinators/WalletCoordinator.swift @@ -23,7 +23,7 @@ class WalletCoordinator: Coordinator { ) { self.navigationController = navigationController self.navigationController.modalPresentationStyle = .formSheet - self.keystore = EtherKeystore() + self.keystore = keystore } func start(_ entryPoint: WalletEntryPoint) { diff --git a/TrustTests/Coordinators/AppCoordinatorTests.swift b/TrustTests/Coordinators/AppCoordinatorTests.swift index e5717ae8a..5948c1a85 100644 --- a/TrustTests/Coordinators/AppCoordinatorTests.swift +++ b/TrustTests/Coordinators/AppCoordinatorTests.swift @@ -13,7 +13,7 @@ class AppCoordinatorTests: XCTestCase { coordinator.start() - XCTAssertTrue(coordinator.rootNavigationController.viewControllers[0] is WelcomeViewController) + XCTAssertTrue(coordinator.navigationController.viewControllers[0] is WelcomeViewController) } func testStartWithAccounts() { @@ -27,7 +27,7 @@ class AppCoordinatorTests: XCTestCase { coordinator.start() XCTAssertEqual(1, coordinator.coordinators.count) - XCTAssertTrue(coordinator.rootNavigationController.viewControllers[0] is TransactionsViewController) + XCTAssertTrue(coordinator.navigationController.viewControllers[0] is TransactionsViewController) } func testReset() { @@ -41,20 +41,20 @@ class AppCoordinatorTests: XCTestCase { coordinator.reset() - XCTAssertTrue(coordinator.rootNavigationController.viewControllers[0] is WelcomeViewController) + XCTAssertTrue(coordinator.navigationController.viewControllers[0] is WelcomeViewController) } func testStartWelcomeWalletCoordinator() { let coordinator = AppCoordinator( window: UIWindow(), keystore: FakeKeystore(), - rootNavigationController: FakeNavigationController() + navigationController: FakeNavigationController() ) coordinator.start() coordinator.showCreateWallet() - XCTAssertTrue(coordinator.rootNavigationController.viewControllers[0] is WelcomeViewController) + XCTAssertTrue(coordinator.navigationController.viewControllers[0] is WelcomeViewController) } func testImportWalletCoordinator() { @@ -63,25 +63,38 @@ class AppCoordinatorTests: XCTestCase { keystore: FakeKeystore( accounts: [.make()] ), - rootNavigationController: FakeNavigationController() + navigationController: FakeNavigationController() ) coordinator.start() coordinator.presentImportWallet() - XCTAssertTrue((coordinator.rootNavigationController.presentedViewController as? UINavigationController)?.viewControllers[0] is ImportWalletViewController) + XCTAssertTrue((coordinator.navigationController.presentedViewController as? UINavigationController)?.viewControllers[0] is ImportWalletViewController) } func testShowTransactions() { let coordinator = AppCoordinator( window: UIWindow(), keystore: FakeKeystore(), - rootNavigationController: FakeNavigationController() + navigationController: FakeNavigationController() ) coordinator.start() coordinator.showTransactions(for: .make()) XCTAssertEqual(1, coordinator.coordinators.count) - XCTAssertTrue(coordinator.rootNavigationController.viewControllers[0] is TransactionsViewController) + XCTAssertTrue(coordinator.navigationController.viewControllers[0] is TransactionsViewController) + } + + func testShowAccounts() { + let coordinator = AppCoordinator( + window: UIWindow(), + keystore: FakeKeystore(), + navigationController: FakeNavigationController() + ) + coordinator.start() + + coordinator.showAccounts() + + XCTAssertTrue((coordinator.navigationController.presentedViewController as? UINavigationController)?.viewControllers[0] is AccountsViewController) } } diff --git a/TrustTests/Coordinators/TransactionCoordinatorTests.swift b/TrustTests/Coordinators/TransactionCoordinatorTests.swift index f941a5356..ef69aaa05 100644 --- a/TrustTests/Coordinators/TransactionCoordinatorTests.swift +++ b/TrustTests/Coordinators/TransactionCoordinatorTests.swift @@ -16,17 +16,6 @@ class TransactionCoordinatorTests: XCTestCase { XCTAssertTrue(coordinator.navigationController.viewControllers[0] is TokensViewController) } - func testShowAccounts() { - let coordinator = TransactionCoordinator( - session: .make(), - rootNavigationController: FakeNavigationController() - ) - - coordinator.showAccounts() - - XCTAssertTrue((coordinator.navigationController.presentedViewController as? UINavigationController)?.viewControllers[0] is AccountsViewController) - } - func testShowSettings() { let coordinator = TransactionCoordinator( session: .make(),