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/Accounts/Coordinators/AccountsCoordinator.swift

130 lines
5.0 KiB

// Copyright SIX DAY LLC. All rights reserved.
7 years ago
import Foundation
import UIKit
protocol AccountsCoordinatorDelegate: class {
func didCancel(in coordinator: AccountsCoordinator)
func didSelectAccount(account: Account, in coordinator: AccountsCoordinator)
func didAddAccount(account: Account, in coordinator: AccountsCoordinator)
7 years ago
func didDeleteAccount(account: Account, in coordinator: AccountsCoordinator)
}
class AccountsCoordinator: Coordinator {
7 years ago
let navigationController: UINavigationController
let keystore: Keystore
var coordinators: [Coordinator] = []
7 years ago
lazy var accountsViewController: AccountsViewController = {
let controller = AccountsViewController(keystore: keystore)
controller.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismiss))
7 years ago
controller.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(add))
controller.allowsAccountDeletion = true
controller.delegate = self
return controller
}()
7 years ago
weak var delegate: AccountsCoordinatorDelegate?
init(
navigationController: UINavigationController,
keystore: Keystore
) {
7 years ago
self.navigationController = navigationController
self.navigationController.modalPresentationStyle = .formSheet
self.keystore = keystore
7 years ago
}
7 years ago
func start() {
navigationController.pushViewController(accountsViewController, animated: false)
7 years ago
}
7 years ago
@objc func dismiss() {
delegate?.didCancel(in: self)
}
7 years ago
@objc func add() {
showCreateWallet()
}
7 years ago
func showCreateWallet() {
let coordinator = WalletCoordinator(keystore: keystore)
coordinator.delegate = self
addCoordinator(coordinator)
coordinator.start(.welcome)
navigationController.present(coordinator.navigationController, animated: true, completion: nil)
7 years ago
}
func showInfoSheet(for account: Account, sender: UIView) {
let controller = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
controller.popoverPresentationController?.sourceView = sender
controller.popoverPresentationController?.sourceRect = sender.centerRect
let actionTitle = NSLocalizedString("wallets.backup.alertSheet.title", value: "Backup Keystore", comment: "The title of the backup button in the wallet's action sheet")
let action = UIAlertAction(title: actionTitle, style: .default) { _ in
let coordinator = BackupCoordinator(
navigationController: self.navigationController,
keystore: self.keystore,
account: account
)
coordinator.delegate = self
coordinator.start()
self.addCoordinator(coordinator)
}
let copyAction = UIAlertAction(
title: NSLocalizedString("Copy Address", value: "Copy Address", comment: ""),
style: .default
) { _ in
UIPasteboard.general.string = account.address.address
}
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", value: "Cancel", comment: ""), style: .cancel) { _ in }
controller.addAction(action)
controller.addAction(copyAction)
controller.addAction(cancelAction)
navigationController.present(controller, animated: true, completion: nil)
}
7 years ago
}
extension AccountsCoordinator: AccountsViewControllerDelegate {
func didSelectAccount(account: Account, in viewController: AccountsViewController) {
delegate?.didSelectAccount(account: account, in: self)
}
7 years ago
func didDeleteAccount(account: Account, in viewController: AccountsViewController) {
delegate?.didDeleteAccount(account: account, in: self)
}
func didSelectInfoForAccount(account: Account, sender: UIView, in viewController: AccountsViewController) {
showInfoSheet(for: account, sender: sender)
}
7 years ago
}
extension AccountsCoordinator: WalletCoordinatorDelegate {
func didFinish(with account: Account, in coordinator: WalletCoordinator) {
delegate?.didAddAccount(account: account, in: self)
7 years ago
accountsViewController.fetch()
coordinator.navigationController.dismiss(animated: true, completion: nil)
removeCoordinator(coordinator)
7 years ago
}
7 years ago
func didFail(with error: Error, in coordinator: WalletCoordinator) {
coordinator.navigationController.dismiss(animated: true, completion: nil)
removeCoordinator(coordinator)
7 years ago
}
7 years ago
func didCancel(in coordinator: WalletCoordinator) {
coordinator.navigationController.dismiss(animated: true, completion: nil)
removeCoordinator(coordinator)
7 years ago
}
}
extension AccountsCoordinator: BackupCoordinatorDelegate {
func didCancel(coordinator: BackupCoordinator) {
removeCoordinator(coordinator)
}
func didFinish(account: Account, in coordinator: BackupCoordinator) {
removeCoordinator(coordinator)
}
}