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

123 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)
}
}
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 = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = account.address.address
7 years ago
return cell
}
7 years ago
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return allowsAccountDeletion
}
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) {
let askController = UIAlertController.askPassword(
title: "Please enter your password to delete you account",
message: "Make sure you export your account before deleting"
) { result in
switch result {
case .success(let password):
self.delete(account: account, password: password)
case .failure: break
}
}
present(askController, animated: true, completion: nil)
}
7 years ago
func delete(account: Account, password: String) {
7 years ago
let result = self.keystore.delete(account: account, password: password)
switch result {
case .success:
self.fetch()
self.delegate?.didDeleteAccount(account: account, in: self)
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")
}
}