2021-06-13 03:45:24 +03:00
|
|
|
// Copyright © 2021 Encrypted Ink. All rights reserved.
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
class AccountsListViewController: NSViewController {
|
|
|
|
|
2021-06-16 21:22:50 +03:00
|
|
|
private let agent = Agent.shared
|
2021-06-13 05:45:54 +03:00
|
|
|
private var accounts = [Account]()
|
|
|
|
|
2021-06-13 06:30:20 +03:00
|
|
|
var onSelectedAccount: ((Account) -> Void)?
|
|
|
|
|
2021-06-13 05:45:54 +03:00
|
|
|
static func with(preloadedAccounts: [Account]) -> AccountsListViewController {
|
|
|
|
let new = instantiate(AccountsListViewController.self)
|
|
|
|
new.accounts = preloadedAccounts
|
|
|
|
return new
|
|
|
|
}
|
2021-06-13 03:45:24 +03:00
|
|
|
|
|
|
|
@IBOutlet weak var titleLabel: NSTextField!
|
|
|
|
@IBOutlet weak var tableView: NSTableView! {
|
|
|
|
didSet {
|
|
|
|
tableView.delegate = self
|
|
|
|
tableView.dataSource = self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2021-06-13 03:59:05 +03:00
|
|
|
|
|
|
|
let menu = NSMenu()
|
|
|
|
menu.addItem(NSMenuItem(title: "Copy address", action: #selector(didClickCopyAddress(_:)), keyEquivalent: ""))
|
|
|
|
menu.addItem(NSMenuItem(title: "Remove account", action: #selector(didClickRemoveAccount(_:)), keyEquivalent: ""))
|
|
|
|
tableView.menu = menu
|
2021-06-13 05:45:54 +03:00
|
|
|
|
|
|
|
if accounts.isEmpty {
|
|
|
|
accounts = AccountsService.getAccounts()
|
|
|
|
}
|
2021-06-13 06:30:20 +03:00
|
|
|
|
2021-06-17 23:14:19 +03:00
|
|
|
reloadTitle()
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: NSApplication.didBecomeActiveNotification, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func reloadTitle() {
|
|
|
|
titleLabel.stringValue = onSelectedAccount != nil ? "Select\nAccount" : "Accounts"
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func didBecomeActive() {
|
|
|
|
// TODO: check if there is something good in pasteboard
|
|
|
|
reloadTitle()
|
2021-06-13 03:45:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func addButtonTapped(_ sender: NSButton) {
|
2021-06-13 05:45:54 +03:00
|
|
|
let importViewController = instantiate(ImportViewController.self)
|
2021-06-13 06:30:20 +03:00
|
|
|
importViewController.onSelectedAccount = onSelectedAccount
|
2021-06-13 05:45:54 +03:00
|
|
|
view.window?.contentViewController = importViewController
|
2021-06-13 03:45:24 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 03:59:05 +03:00
|
|
|
@objc private func didClickCopyAddress(_ sender: AnyObject) {
|
|
|
|
let row = tableView.clickedRow
|
|
|
|
guard row >= 0 else { return }
|
|
|
|
let pasteboard = NSPasteboard.general
|
|
|
|
pasteboard.clearContents()
|
|
|
|
pasteboard.setString(accounts[row].address, forType: .string)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func didClickRemoveAccount(_ sender: AnyObject) {
|
|
|
|
let row = tableView.clickedRow
|
2021-06-16 21:13:47 +03:00
|
|
|
|
|
|
|
let alert = NSAlert()
|
|
|
|
alert.messageText = "Removed accounts can't be recovered."
|
|
|
|
alert.alertStyle = .critical
|
|
|
|
alert.addButton(withTitle: "Cancel")
|
|
|
|
alert.addButton(withTitle: "Remove anyway")
|
|
|
|
if alert.runModal() != .alertFirstButtonReturn {
|
|
|
|
guard row >= 0 else { return }
|
2021-06-16 21:22:50 +03:00
|
|
|
agent.proceedAfterAuthentication(reason: "Remove account") { [weak self] allowed in
|
2021-06-16 22:12:45 +03:00
|
|
|
Window.activateApp()
|
2021-06-16 21:22:50 +03:00
|
|
|
if allowed {
|
|
|
|
self?.removeAccountAtIndex(row)
|
|
|
|
}
|
|
|
|
}
|
2021-06-16 21:13:47 +03:00
|
|
|
}
|
2021-06-13 03:59:05 +03:00
|
|
|
}
|
|
|
|
|
2021-06-17 22:47:39 +03:00
|
|
|
private func showInstructionsAlert() {
|
|
|
|
let alert = NSAlert()
|
|
|
|
alert.messageText = "How to start?"
|
2021-06-17 23:28:07 +03:00
|
|
|
alert.informativeText = "1. Press “Copy to clipboard”\nunder WalletConnect QR code.\n\n2. Launch Encrypted Ink."
|
2021-06-17 22:47:39 +03:00
|
|
|
alert.alertStyle = .informational
|
|
|
|
alert.addButton(withTitle: "OK")
|
2021-06-17 23:28:07 +03:00
|
|
|
alert.addButton(withTitle: "Open Zerion")
|
|
|
|
if alert.runModal() != .alertFirstButtonReturn, let url = URL(string: "https://app.zerion.io") {
|
|
|
|
NSWorkspace.shared.open(url)
|
|
|
|
}
|
2021-06-17 22:47:39 +03:00
|
|
|
}
|
|
|
|
|
2021-06-16 21:22:50 +03:00
|
|
|
private func removeAccountAtIndex(_ index: Int) {
|
|
|
|
AccountsService.removeAccount(accounts[index])
|
|
|
|
accounts.remove(at: index)
|
|
|
|
tableView.reloadData()
|
|
|
|
}
|
|
|
|
|
2021-06-13 03:45:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
extension AccountsListViewController: NSTableViewDelegate {
|
|
|
|
|
|
|
|
func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
|
2021-06-13 06:30:20 +03:00
|
|
|
if let onSelectedAccount = onSelectedAccount {
|
|
|
|
let account = accounts[row]
|
|
|
|
onSelectedAccount(account)
|
|
|
|
return true
|
|
|
|
} else {
|
2021-06-17 22:47:39 +03:00
|
|
|
showInstructionsAlert()
|
2021-06-13 06:30:20 +03:00
|
|
|
return false
|
|
|
|
}
|
2021-06-13 03:45:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension AccountsListViewController: NSTableViewDataSource {
|
|
|
|
|
|
|
|
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
|
|
|
|
let rowView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("AccountCellView"), owner: self) as? AccountCellView
|
|
|
|
rowView?.setup(address: accounts[row].address)
|
|
|
|
return rowView
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
|
|
|
|
return 50
|
|
|
|
}
|
|
|
|
|
|
|
|
func numberOfRows(in tableView: NSTableView) -> Int {
|
|
|
|
return accounts.count
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|