Use SelectAccountAction instead of AccountSelectionConfiguration

This commit is contained in:
Ivan Grachev 2022-08-23 18:00:48 +03:00
parent b63e3220ae
commit a4f450b030
8 changed files with 56 additions and 70 deletions

View File

@ -1,12 +0,0 @@
// Copyright © 2022 Tokenary. All rights reserved.
import Foundation
import WalletCore
struct AccountSelectionConfiguration {
let peer: PeerMeta?
let coinType: CoinType?
var selectedAccounts: Set<SpecificWalletAccount>
let initiallyConnectedProviders: Set<Web3Provider>
let completion: ((EthereumChain?, [SpecificWalletAccount]?) -> Void)
}

View File

@ -13,9 +13,10 @@ enum DappRequestAction {
}
struct SelectAccountAction {
let provider: Web3Provider
let peer: PeerMeta?
let coinType: CoinType?
var selectedAccounts: Set<SpecificWalletAccount>
let initiallyConnectedProviders: Set<Web3Provider>
let preselectedAccounts: [SpecificWalletAccount]
let completion: (EthereumChain?, [SpecificWalletAccount]?) -> Void
}

View File

@ -37,9 +37,10 @@ struct DappRequestProcessor {
return walletsManager.getSpecificAccount(coin: coin, address: configuration.address)
}
let initiallyConnectedProviders = Set(body.providerConfigurations.map { $0.provider })
let action = SelectAccountAction(provider: .unknown,
initiallyConnectedProviders: initiallyConnectedProviders,
preselectedAccounts: preselectedAccounts) { chain, specificWalletAccounts in
let action = SelectAccountAction(peer: request.peerMeta,
coinType: nil,
selectedAccounts: Set(preselectedAccounts),
initiallyConnectedProviders: initiallyConnectedProviders) { chain, specificWalletAccounts in
if let chain = chain, let specificWalletAccounts = specificWalletAccounts {
var specificProviderBodies = [ResponseToExtension.Body]()
for specificWalletAccount in specificWalletAccounts {
@ -86,8 +87,10 @@ struct DappRequestProcessor {
switch body.method {
case .signIn:
let suggestedAccounts = walletsManager.suggestedAccounts(coin: .near)
let action = SelectAccountAction(provider: .near, initiallyConnectedProviders: Set(), preselectedAccounts: suggestedAccounts) { _, specificWalletAccounts in
let action = SelectAccountAction(peer: peerMeta,
coinType: .near,
selectedAccounts: Set(walletsManager.suggestedAccounts(coin: .near)),
initiallyConnectedProviders: Set()) { _, specificWalletAccounts in
if let specificWalletAccount = specificWalletAccounts?.first, specificWalletAccount.account.coin == .near {
let responseBody = ResponseToExtension.Near(account: specificWalletAccount.account.address)
respond(to: request, body: .near(responseBody), completion: completion)
@ -138,8 +141,10 @@ struct DappRequestProcessor {
switch body.method {
case .connect:
let suggestedAccounts = walletsManager.suggestedAccounts(coin: .solana)
let action = SelectAccountAction(provider: .solana, initiallyConnectedProviders: Set(), preselectedAccounts: suggestedAccounts) { _, specificWalletAccounts in
let action = SelectAccountAction(peer: peerMeta,
coinType: .solana,
selectedAccounts: Set(walletsManager.suggestedAccounts(coin: .solana)),
initiallyConnectedProviders: Set()) { _, specificWalletAccounts in
if let specificWalletAccount = specificWalletAccounts?.first, specificWalletAccount.account.coin == .solana {
let responseBody = ResponseToExtension.Solana(publicKey: specificWalletAccount.account.address)
respond(to: request, body: .solana(responseBody), completion: completion)
@ -219,8 +224,10 @@ struct DappRequestProcessor {
switch ethereumRequest.method {
case .requestAccounts:
let suggestedAccounts = walletsManager.suggestedAccounts(coin: .ethereum)
let action = SelectAccountAction(provider: .ethereum, initiallyConnectedProviders: Set(), preselectedAccounts: suggestedAccounts) { chain, specificWalletAccounts in
let action = SelectAccountAction(peer: peerMeta,
coinType: .ethereum,
selectedAccounts: Set(walletsManager.suggestedAccounts(coin: .ethereum)),
initiallyConnectedProviders: Set()) { chain, specificWalletAccounts in
if let chain = chain, let specificWalletAccount = specificWalletAccounts?.first, specificWalletAccount.account.coin == .ethereum {
let responseBody = ResponseToExtension.Ethereum(results: [specificWalletAccount.account.address], chainId: chain.hexStringId, rpcURL: chain.nodeURLString)
respond(to: request, body: .ethereum(responseBody), completion: completion)

View File

@ -16,6 +16,7 @@ class Agent: NSObject {
static let shared = Agent()
private let walletConnect = WalletConnect.shared
private let walletsManager = WalletsManager.shared
private override init() { super.init() }
private var statusBarItem: NSStatusItem!
@ -79,14 +80,14 @@ class Agent: NSObject {
let accountsList = instantiate(AccountsListViewController.self)
if case let .wcSession(session) = request, let completion = onSelectedWallet(session: session) {
accountsList.accountSelectionConfiguration = AccountSelectionConfiguration(peer: nil,
coinType: .ethereum,
selectedAccounts: Set(),
initiallyConnectedProviders: Set(),
completion: completion)
accountsList.selectAccountAction = SelectAccountAction(peer: nil,
coinType: .ethereum,
selectedAccounts: Set(walletsManager.suggestedAccounts(coin: .ethereum)),
initiallyConnectedProviders: Set(),
completion: completion)
}
let windowController = Window.showNew(closeOthers: accountsList.accountSelectionConfiguration == nil)
let windowController = Window.showNew(closeOthers: accountsList.selectAccountAction == nil)
windowController.contentViewController = accountsList
}
}
@ -322,12 +323,7 @@ class Agent: NSObject {
let windowController = Window.showNew(closeOthers: closeOtherWindows)
windowNumber = windowController.window?.windowNumber
let accountsList = instantiate(AccountsListViewController.self)
let coinType = CoinType.correspondingToWeb3Provider(accountAction.provider)
accountsList.accountSelectionConfiguration = AccountSelectionConfiguration(peer: safariRequest.peerMeta,
coinType: coinType,
selectedAccounts: Set(accountAction.preselectedAccounts),
initiallyConnectedProviders: accountAction.initiallyConnectedProviders,
completion: accountAction.completion)
accountsList.selectAccountAction = accountAction
windowController.contentViewController = accountsList
case .approveMessage(let action):
let windowController = Window.showNew(closeOthers: false)

View File

@ -10,7 +10,7 @@ class AccountsListViewController: NSViewController {
private var cellModels = [CellModel]()
private var chain = EthereumChain.ethereum
private var didCallCompletion = false
var accountSelectionConfiguration: AccountSelectionConfiguration?
var selectAccountAction: SelectAccountAction?
var newWalletId: String?
var getBackToRect: CGRect?
@ -83,7 +83,7 @@ class AccountsListViewController: NSViewController {
updateCellModels()
NotificationCenter.default.addObserver(self, selector: #selector(walletsChanged), name: Notification.Name.walletsChanged, object: nil)
if let preselectedAccount = accountSelectionConfiguration?.selectedAccounts.first {
if let preselectedAccount = selectAccountAction?.selectedAccounts.first {
scrollTo(specificWalletAccount: preselectedAccount)
}
}
@ -105,16 +105,16 @@ class AccountsListViewController: NSViewController {
private func callCompletion(specificWalletAccounts: [SpecificWalletAccount]?) {
if !didCallCompletion {
didCallCompletion = true
accountSelectionConfiguration?.completion(chain, specificWalletAccounts)
selectAccountAction?.completion(chain, specificWalletAccounts)
}
}
private func updateBottomButtons() {
if let accountSelectionConfiguration = accountSelectionConfiguration {
if let selectAccountAction = selectAccountAction {
accountsListBottomConstraint.constant = 62
bottomButtonsStackView.isHidden = false
if !accountSelectionConfiguration.initiallyConnectedProviders.isEmpty {
if !selectAccountAction.initiallyConnectedProviders.isEmpty {
primaryButton.title = Strings.ok
secondaryButton.title = Strings.disconnect
secondaryButton.keyEquivalent = ""
@ -128,15 +128,15 @@ class AccountsListViewController: NSViewController {
}
private func updatePrimaryButton() {
primaryButton.isEnabled = accountSelectionConfiguration?.selectedAccounts.isEmpty == false
primaryButton.isEnabled = selectAccountAction?.selectedAccounts.isEmpty == false
}
private func reloadHeader() {
let canSelectAccount = accountSelectionConfiguration != nil && !wallets.isEmpty
let canSelectAccount = selectAccountAction != nil && !wallets.isEmpty
titleLabel.stringValue = canSelectAccount ? Strings.selectAccountTwoLines : Strings.wallets
addButton.isHidden = wallets.isEmpty
if canSelectAccount, let peer = accountSelectionConfiguration?.peer {
if canSelectAccount, let peer = selectAccountAction?.peer {
websiteNameLabel.stringValue = peer.name
titleLabelTopConstraint.constant = 14
websiteNameStackView.isHidden = false
@ -154,7 +154,7 @@ class AccountsListViewController: NSViewController {
websiteNameStackView.isHidden = true
}
let canSelectNetworkForCurrentProvider = accountSelectionConfiguration?.coinType == .ethereum || accountSelectionConfiguration?.coinType == nil
let canSelectNetworkForCurrentProvider = selectAccountAction?.coinType == .ethereum || selectAccountAction?.coinType == nil
if canSelectAccount, networkButton.isHidden, canSelectNetworkForCurrentProvider {
networkButton.isHidden = false
let menu = NSMenu()
@ -210,7 +210,7 @@ class AccountsListViewController: NSViewController {
}
@IBAction func didClickSecondaryButton(_ sender: Any) {
if accountSelectionConfiguration?.initiallyConnectedProviders.isEmpty == false {
if selectAccountAction?.initiallyConnectedProviders.isEmpty == false {
callCompletion(specificWalletAccounts: [])
} else {
callCompletion(specificWalletAccounts: nil)
@ -218,7 +218,7 @@ class AccountsListViewController: NSViewController {
}
@IBAction func didClickPrimaryButton(_ sender: Any) {
callCompletion(specificWalletAccounts: accountSelectionConfiguration?.selectedAccounts.map { $0 })
callCompletion(specificWalletAccounts: selectAccountAction?.selectedAccounts.map { $0 })
}
@objc private func didSelectChain(_ sender: AnyObject) {
@ -280,7 +280,7 @@ class AccountsListViewController: NSViewController {
@objc private func didClickImportAccount() {
let importViewController = instantiate(ImportViewController.self)
importViewController.accountSelectionConfiguration = accountSelectionConfiguration
importViewController.selectAccountAction = selectAccountAction
view.window?.contentViewController = importViewController
}
@ -306,7 +306,7 @@ class AccountsListViewController: NSViewController {
}
override func cancelOperation(_ sender: Any?) {
if accountSelectionConfiguration?.initiallyConnectedProviders.isEmpty == false {
if selectAccountAction?.initiallyConnectedProviders.isEmpty == false {
callCompletion(specificWalletAccounts: nil)
}
}
@ -496,35 +496,35 @@ class AccountsListViewController: NSViewController {
}
private func validateSelectedAccounts() {
guard let specificWalletAccounts = accountSelectionConfiguration?.selectedAccounts else { return }
guard let specificWalletAccounts = selectAccountAction?.selectedAccounts else { return }
for specificWalletAccount in specificWalletAccounts {
if let wallet = wallets.first(where: { $0.id == specificWalletAccount.walletId }),
wallet.accounts.contains(specificWalletAccount.account) {
continue
} else {
accountSelectionConfiguration?.selectedAccounts.remove(specificWalletAccount)
selectAccountAction?.selectedAccounts.remove(specificWalletAccount)
}
}
}
private func didClickAccountInSelectionMode(specificWalletAccount: SpecificWalletAccount) {
let wasSelected = accountSelectionConfiguration?.selectedAccounts.contains(specificWalletAccount) == true
let wasSelected = selectAccountAction?.selectedAccounts.contains(specificWalletAccount) == true
if !wasSelected, let toDeselect = accountSelectionConfiguration?.selectedAccounts.first(where: { $0.account.coin == specificWalletAccount.account.coin }) {
accountSelectionConfiguration?.selectedAccounts.remove(toDeselect)
if !wasSelected, let toDeselect = selectAccountAction?.selectedAccounts.first(where: { $0.account.coin == specificWalletAccount.account.coin }) {
selectAccountAction?.selectedAccounts.remove(toDeselect)
}
if wasSelected {
accountSelectionConfiguration?.selectedAccounts.remove(specificWalletAccount)
selectAccountAction?.selectedAccounts.remove(specificWalletAccount)
} else {
accountSelectionConfiguration?.selectedAccounts.insert(specificWalletAccount)
selectAccountAction?.selectedAccounts.insert(specificWalletAccount)
}
updatePrimaryButton()
}
private func accountCanBeSelected(_ account: Account) -> Bool {
return accountSelectionConfiguration?.coinType == nil || accountSelectionConfiguration?.coinType == account.coin
return selectAccountAction?.coinType == nil || selectAccountAction?.coinType == account.coin
}
}
@ -576,7 +576,7 @@ extension AccountsListViewController: AccountsHeaderDelegate {
guard let wallet = walletForRow(row) else { return }
let editAccountsViewController = instantiate(EditAccountsViewController.self)
editAccountsViewController.accountSelectionConfiguration = accountSelectionConfiguration
editAccountsViewController.selectAccountAction = selectAccountAction
editAccountsViewController.wallet = wallet
editAccountsViewController.getBackToRect = tableView.visibleRect
view.window?.contentViewController = editAccountsViewController
@ -624,7 +624,7 @@ extension AccountsListViewController: NSTableViewDelegate {
return false
}
if accountSelectionConfiguration != nil {
if selectAccountAction != nil {
if accountCanBeSelected(account) {
let specificWalletAccount = SpecificWalletAccount(walletId: wallet.id, account: account)
didClickAccountInSelectionMode(specificWalletAccount: specificWalletAccount)
@ -649,7 +649,7 @@ extension AccountsListViewController: NSTableViewDataSource {
let rowView = tableView.makeViewOfType(AccountCellView.self, owner: self)
let account = wallet.accounts[0]
let specificWalletAccount = SpecificWalletAccount(walletId: wallet.id, account: account)
let isSelected = accountSelectionConfiguration?.selectedAccounts.contains(specificWalletAccount) == true
let isSelected = selectAccountAction?.selectedAccounts.contains(specificWalletAccount) == true
rowView.setup(account: account, isSelected: isSelected, isDisabled: !accountCanBeSelected(account))
return rowView
case let .mnemonicAccount(walletIndex: walletIndex, accountIndex: accountIndex):
@ -657,7 +657,7 @@ extension AccountsListViewController: NSTableViewDataSource {
let rowView = tableView.makeViewOfType(AccountCellView.self, owner: self)
let account = wallet.accounts[accountIndex]
let specificWalletAccount = SpecificWalletAccount(walletId: wallet.id, account: account)
let isSelected = accountSelectionConfiguration?.selectedAccounts.contains(specificWalletAccount) == true
let isSelected = selectAccountAction?.selectedAccounts.contains(specificWalletAccount) == true
rowView.setup(account: account, isSelected: isSelected, isDisabled: !accountCanBeSelected(account))
return rowView
case .mnemonicWalletHeader:

View File

@ -7,7 +7,7 @@ class EditAccountsViewController: NSViewController {
var wallet: TokenaryWallet!
var getBackToRect: CGRect?
var accountSelectionConfiguration: AccountSelectionConfiguration?
var selectAccountAction: SelectAccountAction?
struct CoinDerivationCellModel {
let coinDerivation: CoinDerivation
@ -64,7 +64,7 @@ class EditAccountsViewController: NSViewController {
private func showAccountsList() {
let accountsListViewController = instantiate(AccountsListViewController.self)
accountsListViewController.accountSelectionConfiguration = accountSelectionConfiguration
accountsListViewController.selectAccountAction = selectAccountAction
accountsListViewController.getBackToRect = getBackToRect
view.window?.contentViewController = accountsListViewController
}

View File

@ -6,7 +6,7 @@ import WalletCore
class ImportViewController: NSViewController {
private let walletsManager = WalletsManager.shared
var accountSelectionConfiguration: AccountSelectionConfiguration?
var selectAccountAction: SelectAccountAction?
private var inputValidationResult = WalletsManager.InputValidationResult.invalid
@IBOutlet weak var textField: NSTextField! {
@ -62,7 +62,7 @@ class ImportViewController: NSViewController {
private func showAccountsList(newWalletId: String?) {
let accountsListViewController = instantiate(AccountsListViewController.self)
accountsListViewController.accountSelectionConfiguration = accountSelectionConfiguration
accountsListViewController.selectAccountAction = selectAccountAction
accountsListViewController.newWalletId = newWalletId
view.window?.contentViewController = accountsListViewController
}

View File

@ -108,8 +108,6 @@
2C6F6D5A28273FE500D6E8FB /* EditAccountsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C6F6D5928273FE500D6E8FB /* EditAccountsViewController.swift */; };
2C6F6D5D2827434800D6E8FB /* CoinDerivationTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2C6F6D5B2827434800D6E8FB /* CoinDerivationTableViewCell.xib */; };
2C6F6D5E2827434800D6E8FB /* CoinDerivationTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C6F6D5C2827434800D6E8FB /* CoinDerivationTableViewCell.swift */; };
2C71175328AA62DE00ABBF2C /* AccountSelectionConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C71175228AA62DE00ABBF2C /* AccountSelectionConfiguration.swift */; };
2C71175428AA62DE00ABBF2C /* AccountSelectionConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C71175228AA62DE00ABBF2C /* AccountSelectionConfiguration.swift */; };
2C773F5E27450B97007B04E7 /* ExtensionBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C773F5D27450B97007B04E7 /* ExtensionBridge.swift */; };
2C773F5F27450FBD007B04E7 /* ExtensionBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C773F5D27450B97007B04E7 /* ExtensionBridge.swift */; };
2C773F62274523DC007B04E7 /* ResponseToExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C773F61274523DC007B04E7 /* ResponseToExtension.swift */; };
@ -346,7 +344,6 @@
2C6F6D5928273FE500D6E8FB /* EditAccountsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditAccountsViewController.swift; sourceTree = "<group>"; };
2C6F6D5B2827434800D6E8FB /* CoinDerivationTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CoinDerivationTableViewCell.xib; sourceTree = "<group>"; };
2C6F6D5C2827434800D6E8FB /* CoinDerivationTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoinDerivationTableViewCell.swift; sourceTree = "<group>"; };
2C71175228AA62DE00ABBF2C /* AccountSelectionConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountSelectionConfiguration.swift; sourceTree = "<group>"; };
2C74386E28297DAC00EC9304 /* near.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; name = near.js; path = "web3-provider/near.js"; sourceTree = "<group>"; };
2C773F5D27450B97007B04E7 /* ExtensionBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionBridge.swift; sourceTree = "<group>"; };
2C773F61274523DC007B04E7 /* ResponseToExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseToExtension.swift; sourceTree = "<group>"; };
@ -872,7 +869,6 @@
0D059AD126C2796200EE3023 /* ApprovalSubject.swift */,
2C09FC652828331D00DE9C27 /* Image.swift */,
2C89D26727BADCA9006C0C8D /* DappRequestAction.swift */,
2C71175228AA62DE00ABBF2C /* AccountSelectionConfiguration.swift */,
0DC850E626B73A5900809E82 /* AuthenticationReason.swift */,
2C2AA1D128AD1DC100E35DBF /* SpecificWalletAccount.swift */,
);
@ -1423,7 +1419,6 @@
2C40379428199110004C7263 /* Solana.swift in Sources */,
2C8A09DF267579EA00993638 /* AccountsListViewController.swift in Sources */,
2C917429267D2A6E00049075 /* Keychain.swift in Sources */,
2C71175328AA62DE00ABBF2C /* AccountSelectionConfiguration.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -1447,7 +1442,6 @@
2CF255B6275A746000AE54B9 /* AccountsListViewController.swift in Sources */,
2C264BCC27B2F2FF00234393 /* TezosSafariRequest.swift in Sources */,
2C96D3A42763C6A800687301 /* UIView.swift in Sources */,
2C71175428AA62DE00ABBF2C /* AccountSelectionConfiguration.swift in Sources */,
2CF25597275A46D300AE54B9 /* Defaults.swift in Sources */,
2CF255A2275A47DD00AE54B9 /* String.swift in Sources */,
2CF2559D275A479800AE54B9 /* TokenaryWallet.swift in Sources */,