Keep selected accounts in AccountSelectionConfiguration

This commit is contained in:
Ivan Grachev 2022-08-17 16:54:18 +03:00
parent b2dae33adc
commit 8c92936060
3 changed files with 24 additions and 20 deletions

View File

@ -5,5 +5,6 @@ import WalletCore
struct AccountSelectionConfiguration {
let peer: PeerMeta?
var selectedAccounts: Set<SpecificWalletAccount>
let completion: ((EthereumChain?, SpecificWalletAccount?) -> Void)
}

View File

@ -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)

View File

@ -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)