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/ViewControllers/AccountsViewController.swift

121 lines
3.6 KiB

// Copyright SIX DAY LLC. All rights reserved.
7 years ago
import UIKit
protocol AccountsViewControllerDelegate: class {
func didSelectAccount(account: Account, in viewController: AccountsViewController)
func didDeleteAccount(account: Account, in viewController: AccountsViewController)
}
class AccountsViewController: UITableViewController {
weak var delegate: AccountsViewControllerDelegate?
var allowsAccountDeletion: Bool = false
7 years ago
var headerTitle: String?
7 years ago
var viewModel: AccountsViewModel {
return AccountsViewModel(
accounts: accounts
)
}
7 years ago
var hasAccounts: Bool {
return !accounts.isEmpty
7 years ago
}
7 years ago
var accounts: [Account] = [] {
didSet {
tableView.reloadData()
configure(viewModel: viewModel)
}
}
let current = EtherKeystore.current
7 years ago
private lazy var keystore = EtherKeystore()
7 years ago
init() {
super.init(style: .grouped)
fetch()
}
7 years ago
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
7 years ago
fetch()
}
7 years ago
func fetch() {
accounts = keystore.accounts.map {
Account(address: Address(address: $0.address.address))
}
7 years ago
}
7 years ago
func configure(viewModel: AccountsViewModel) {
title = headerTitle ?? viewModel.title
}
7 years ago
func account(for indexPath: IndexPath) -> Account {
return viewModel.accounts[indexPath.row]
}
7 years ago
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
7 years ago
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModel.accounts.count
}
7 years ago
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let account = self.account(for: indexPath)
let cell = AccountViewCell(style: .default, reuseIdentifier: AccountViewCell.identifier)
cell.configure(viewModel: AccountViewModel(account: account, current: current))
7 years ago
return cell
}
7 years ago
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return allowsAccountDeletion && (current != viewModel.accounts[indexPath.row] || viewModel.accounts.count == 1)
7 years ago
}
7 years ago
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
7 years ago
let account = self.account(for: indexPath)
confirmDelete(account: account)
}
}
7 years ago
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
7 years ago
let account = self.account(for: indexPath)
delegate?.didSelectAccount(account: account, in: self)
}
7 years ago
func confirmDelete(account: Account) {
confirm(title: "Are you sure you would like to delete this wallet?", message: "Make sure you have backup of your wallet") { result in
7 years ago
switch result {
case .success:
self.delete(account: account)
7 years ago
case .failure: break
}
}
}
func delete(account: Account) {
let result = self.keystore.delete(account: account)
7 years ago
switch result {
case .success:
self.fetch()
self.delegate?.didDeleteAccount(account: account, in: self)
7 years ago
case .failure(let error):
self.displayError(error: error)
7 years ago
}
}
7 years ago
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}