From e6a5cbac19d305d974b34b14571e8fece723d732 Mon Sep 17 00:00:00 2001 From: Ivan Grachev Date: Thu, 18 Aug 2022 15:46:26 +0300 Subject: [PATCH] Scroll to the preselected account when account selection appears --- .../Screens/AccountsListViewController.swift | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Tokenary macOS/Screens/AccountsListViewController.swift b/Tokenary macOS/Screens/AccountsListViewController.swift index e146bb35..c62ca8b5 100644 --- a/Tokenary macOS/Screens/AccountsListViewController.swift +++ b/Tokenary macOS/Screens/AccountsListViewController.swift @@ -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]