Show add account options instead of empty accounts list

This commit is contained in:
Ivan Grachev 2021-07-28 23:58:16 +03:00
parent 78a667f75c
commit b63e2cfcb9
3 changed files with 60 additions and 18 deletions

View File

@ -883,8 +883,8 @@ DQ
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="BpB-tc-JdP">
<rect key="frame" x="14" y="8" width="177" height="25"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="center" title="Label" id="FAR-3U-VR0">
<rect key="frame" x="18" y="8" width="181" height="25"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="left" title="Label" id="FAR-3U-VR0">
<font key="font" metaFont="systemBold" size="21"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
@ -893,8 +893,8 @@ DQ
</subviews>
<constraints>
<constraint firstItem="BpB-tc-JdP" firstAttribute="centerY" secondItem="1KM-PS-nEo" secondAttribute="centerY" id="TPK-nv-hua"/>
<constraint firstItem="BpB-tc-JdP" firstAttribute="leading" secondItem="1KM-PS-nEo" secondAttribute="leading" constant="16" id="UGV-sR-Wou"/>
<constraint firstAttribute="trailing" secondItem="BpB-tc-JdP" secondAttribute="trailing" constant="16" id="YjX-hl-ACx"/>
<constraint firstItem="BpB-tc-JdP" firstAttribute="leading" secondItem="1KM-PS-nEo" secondAttribute="leading" constant="20" id="UGV-sR-Wou"/>
<constraint firstAttribute="trailing" secondItem="BpB-tc-JdP" secondAttribute="trailing" constant="8" id="YjX-hl-ACx"/>
</constraints>
<connections>
<outlet property="titleLabel" destination="BpB-tc-JdP" id="E5r-c3-Wju"/>

View File

@ -51,6 +51,7 @@ class AccountsListViewController: NSViewController {
setupAccountsMenu()
reloadAccounts()
reloadTitle()
updateCellModels()
NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: NSApplication.didBecomeActiveNotification, object: nil)
}
@ -77,6 +78,7 @@ class AccountsListViewController: NSViewController {
private func reloadTitle() {
titleLabel.stringValue = onSelectedAccount != nil && !accounts.isEmpty ? "Select\nAccount" : "Accounts"
addButton.isHidden = accounts.isEmpty
}
@objc private func didBecomeActive() {
@ -107,6 +109,8 @@ class AccountsListViewController: NSViewController {
@objc private func didClickCreateAccount() {
accountsService.createAccount()
reloadAccounts()
reloadTitle()
updateCellModels()
tableView.reloadData()
// TODO: show backup phrase
}
@ -189,26 +193,50 @@ class AccountsListViewController: NSViewController {
private func removeAccountAtIndex(_ index: Int) {
accountsService.removeAccount(accounts[index])
accounts.remove(at: index)
reloadTitle()
updateCellModels()
tableView.reloadData()
}
private func updateCellModels() {
if accounts.isEmpty {
cellModels = [.addAccountOption(.createNew), .addAccountOption(.importExisting)]
tableView.shouldShowRightClickMenu = false
} else {
cellModels = accounts.map { .account($0) }
tableView.shouldShowRightClickMenu = true
}
}
}
extension AccountsListViewController: NSTableViewDelegate {
func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
guard tableView.selectedRow < 0 else { return false }
if let onSelectedAccount = onSelectedAccount {
let account = accounts[row]
onSelectedAccount(account)
} else {
Timer.scheduledTimer(withTimeInterval: 0.01, repeats: false) { [weak self] _ in
var point = NSEvent.mouseLocation
point.x += 1
self?.tableView.menu?.popUp(positioning: nil, at: point, in: nil)
let model = cellModels[row]
switch model {
case let .account(account):
if let onSelectedAccount = onSelectedAccount {
onSelectedAccount(account)
} else {
Timer.scheduledTimer(withTimeInterval: 0.01, repeats: false) { [weak self] _ in
var point = NSEvent.mouseLocation
point.x += 1
self?.tableView.menu?.popUp(positioning: nil, at: point, in: nil)
}
}
return true
case let .addAccountOption(addAccountOption):
switch addAccountOption {
case .createNew:
didClickCreateAccount()
case .importExisting:
didClickImportAccount()
}
return false
}
return true
}
}
@ -216,17 +244,29 @@ extension AccountsListViewController: NSTableViewDelegate {
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
let model = cellModels[row]
switch model {
case let .account(account):
let rowView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("AccountCellView"), owner: self) as? AccountCellView
rowView?.setup(address: account.address)
return rowView
case let .addAccountOption(addAccountOption):
let rowView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("AddAccountOptionCellView"), owner: self) as? AddAccountOptionCellView
rowView?.setup(title: addAccountOption.title)
return rowView
}
}
func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
return 50
if case .account = cellModels[row] {
return 50
} else {
return 44
}
}
func numberOfRows(in tableView: NSTableView) -> Int {
return accounts.count
return cellModels.count
}
}

View File

@ -5,8 +5,10 @@ import Cocoa
class RightClickTableView: NSTableView {
var deselectedRow = -1
var shouldShowRightClickMenu = true
override func menu(for event: NSEvent) -> NSMenu? {
guard shouldShowRightClickMenu else { return nil }
let point = convert(event.locationInWindow, from: nil)
let index = row(at: point)
if index >= 0 {