From 8c9293606013cf936fec9626680f8159f1491b04 Mon Sep 17 00:00:00 2001 From: Ivan Grachev Date: Wed, 17 Aug 2022 16:54:18 +0300 Subject: [PATCH] Keep selected accounts in AccountSelectionConfiguration --- .../AccountSelectionConfiguration.swift | 1 + Tokenary macOS/Agent.swift | 5 ++- .../Screens/AccountsListViewController.swift | 38 ++++++++++--------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Shared/Models/AccountSelectionConfiguration.swift b/Shared/Models/AccountSelectionConfiguration.swift index 202eae0c..a334f4df 100644 --- a/Shared/Models/AccountSelectionConfiguration.swift +++ b/Shared/Models/AccountSelectionConfiguration.swift @@ -5,5 +5,6 @@ import WalletCore struct AccountSelectionConfiguration { let peer: PeerMeta? + var selectedAccounts: Set let completion: ((EthereumChain?, SpecificWalletAccount?) -> Void) } diff --git a/Tokenary macOS/Agent.swift b/Tokenary macOS/Agent.swift index 04fe3159..c144a1d8 100644 --- a/Tokenary macOS/Agent.swift +++ b/Tokenary macOS/Agent.swift @@ -79,7 +79,7 @@ class Agent: NSObject { let accountsList = instantiate(AccountsListViewController.self) if case let .wcSession(session) = request, let completion = onSelectedWallet(session: session) { - accountsList.accountSelectionConfiguration = AccountSelectionConfiguration(peer: nil, completion: completion) + accountsList.accountSelectionConfiguration = AccountSelectionConfiguration(peer: nil, selectedAccounts: Set(), completion: completion) } let windowController = Window.showNew(closeOthers: accountsList.accountSelectionConfiguration == nil) @@ -318,7 +318,8 @@ class Agent: NSObject { let windowController = Window.showNew(closeOthers: closeOtherWindows) windowNumber = windowController.window?.windowNumber let accountsList = instantiate(AccountsListViewController.self) - accountsList.accountSelectionConfiguration = AccountSelectionConfiguration(peer: safariRequest.peerMeta, completion: accountAction.completion) + // TODO: pass selected accounts when there are some + accountsList.accountSelectionConfiguration = AccountSelectionConfiguration(peer: safariRequest.peerMeta, selectedAccounts: Set(), completion: accountAction.completion) windowController.contentViewController = accountsList case .approveMessage(let action): let windowController = Window.showNew(closeOthers: false) diff --git a/Tokenary macOS/Screens/AccountsListViewController.swift b/Tokenary macOS/Screens/AccountsListViewController.swift index 562ab848..17c707ee 100644 --- a/Tokenary macOS/Screens/AccountsListViewController.swift +++ b/Tokenary macOS/Screens/AccountsListViewController.swift @@ -8,9 +8,6 @@ class AccountsListViewController: NSViewController { private let agent = Agent.shared private let walletsManager = WalletsManager.shared private var cellModels = [CellModel]() - - private var selectedAccounts = Set([SpecificWalletAccount]()) // TODO: place inside account selection configuration - private var chain = EthereumChain.ethereum private var didCallCompletion = false var accountSelectionConfiguration: AccountSelectionConfiguration? @@ -204,7 +201,7 @@ class AccountsListViewController: NSViewController { } @IBAction func didClickPrimaryButton(_ sender: Any) { - callCompletion(specificWalletAccount: selectedAccounts.first) + callCompletion(specificWalletAccount: accountSelectionConfiguration?.selectedAccounts.first) // TODO: respond with all selected accounts, to all providers } @@ -454,6 +451,20 @@ class AccountsListViewController: NSViewController { } } + private func didClickAccountInSelectionMode(specificWalletAccount: SpecificWalletAccount) { + let wasSelected = accountSelectionConfiguration?.selectedAccounts.contains(specificWalletAccount) == true + + if !wasSelected, let toDeselect = accountSelectionConfiguration?.selectedAccounts.first(where: { $0.account.coin == specificWalletAccount.account.coin }) { + accountSelectionConfiguration?.selectedAccounts.remove(toDeselect) + } + + if wasSelected { + accountSelectionConfiguration?.selectedAccounts.remove(specificWalletAccount) + } else { + accountSelectionConfiguration?.selectedAccounts.insert(specificWalletAccount) + } + } + } extension AccountsListViewController: TableViewMenuSource { @@ -553,18 +564,7 @@ extension AccountsListViewController: NSTableViewDelegate { if accountSelectionConfiguration != nil { let specificWalletAccount = SpecificWalletAccount(walletId: wallet.id, account: account) - let wasSelected = selectedAccounts.contains(specificWalletAccount) - - if !wasSelected, let toDeselect = selectedAccounts.first(where: { $0.account.coin == account.coin }) { - selectedAccounts.remove(toDeselect) - } - - if wasSelected { - selectedAccounts.remove(specificWalletAccount) - } else { - selectedAccounts.insert(specificWalletAccount) - } - + didClickAccountInSelectionMode(specificWalletAccount: specificWalletAccount) tableView.reloadData() return false } else { @@ -585,14 +585,16 @@ extension AccountsListViewController: NSTableViewDataSource { let rowView = tableView.makeViewOfType(AccountCellView.self, owner: self) let account = wallet.accounts[0] let specificWalletAccount = SpecificWalletAccount(walletId: wallet.id, account: account) - rowView.setup(account: account, isSelected: selectedAccounts.contains(specificWalletAccount)) + let isSelected = accountSelectionConfiguration?.selectedAccounts.contains(specificWalletAccount) == true + rowView.setup(account: account, isSelected: isSelected) return rowView case let .mnemonicAccount(walletIndex: walletIndex, accountIndex: accountIndex): let wallet = wallets[walletIndex] let rowView = tableView.makeViewOfType(AccountCellView.self, owner: self) let account = wallet.accounts[accountIndex] let specificWalletAccount = SpecificWalletAccount(walletId: wallet.id, account: account) - rowView.setup(account: account, isSelected: selectedAccounts.contains(specificWalletAccount)) + let isSelected = accountSelectionConfiguration?.selectedAccounts.contains(specificWalletAccount) == true + rowView.setup(account: account, isSelected: isSelected) return rowView case .mnemonicWalletHeader: let rowView = tableView.makeViewOfType(AccountsHeaderRowView.self, owner: self)