Scroll to the preselected account when account selection appears

This commit is contained in:
Ivan Grachev 2022-08-18 15:46:26 +03:00
parent f488fbadf6
commit e6a5cbac19

View File

@ -82,6 +82,10 @@ class AccountsListViewController: NSViewController {
updateBottomButtons()
updateCellModels()
NotificationCenter.default.addObserver(self, selector: #selector(walletsChanged), name: Notification.Name.walletsChanged, object: nil)
if let preselectedAccount = accountSelectionConfiguration?.selectedAccounts.first {
scrollTo(specificWalletAccount: preselectedAccount)
}
}
override func viewDidAppear() {
@ -274,6 +278,27 @@ class AccountsListViewController: NSViewController {
view.window?.contentViewController = importViewController
}
private func scrollTo(specificWalletAccount: SpecificWalletAccount) {
guard let specificWalletIndex = wallets.firstIndex(where: { $0.id == specificWalletAccount.walletId }),
let specificAccountIndex = wallets[specificWalletIndex].accounts.firstIndex(where: { $0 == specificWalletAccount.account })
else { return }
let row = cellModels.firstIndex { cellModel in
switch cellModel {
case let .mnemonicAccount(walletIndex, accountIndex):
return walletIndex == specificWalletIndex && accountIndex == specificAccountIndex
case let .privateKeyAccount(walletIndex):
return walletIndex == specificWalletIndex
default:
return false
}
}
if let row = row {
tableView.scrollRowToVisible(row)
}
}
private func walletForRow(_ row: Int) -> TokenaryWallet? {
guard row >= 0 else { return nil }
let item = cellModels[row]