From 29246717353f6e01005b7bc2c10eb3bb62820e2e Mon Sep 17 00:00:00 2001 From: Ivan Grachev Date: Mon, 15 Aug 2022 15:39:48 +0300 Subject: [PATCH] Add AccountSelectionConfiguration --- .../AccountSelectionConfiguration.swift | 9 +++++ Shared/Models/PeerMeta.swift | 8 ++++ Shared/Services/DappRequestProcessor.swift | 6 +-- Tokenary macOS/Agent.swift | 8 ++-- .../Screens/AccountsListViewController.swift | 40 +++++++++++-------- .../Screens/EditAccountsViewController.swift | 4 +- .../Screens/ImportViewController.swift | 4 +- Tokenary.xcodeproj/project.pbxproj | 6 +++ 8 files changed, 57 insertions(+), 28 deletions(-) create mode 100644 Shared/Models/AccountSelectionConfiguration.swift diff --git a/Shared/Models/AccountSelectionConfiguration.swift b/Shared/Models/AccountSelectionConfiguration.swift new file mode 100644 index 00000000..7ab7eec8 --- /dev/null +++ b/Shared/Models/AccountSelectionConfiguration.swift @@ -0,0 +1,9 @@ +// Copyright © 2022 Tokenary. All rights reserved. + +import Foundation +import WalletCore + +struct AccountSelectionConfiguration { + let peer: PeerMeta? + let completion: ((EthereumChain?, TokenaryWallet?, Account?) -> Void) +} diff --git a/Shared/Models/PeerMeta.swift b/Shared/Models/PeerMeta.swift index bace4e25..5c112f42 100644 --- a/Shared/Models/PeerMeta.swift +++ b/Shared/Models/PeerMeta.swift @@ -23,3 +23,11 @@ struct PeerMeta { } } + +extension SafariRequest { + + var peerMeta: PeerMeta { + return PeerMeta(title: host, iconURLString: favicon) + } + +} diff --git a/Shared/Services/DappRequestProcessor.swift b/Shared/Services/DappRequestProcessor.swift index 66e50a96..3d358391 100644 --- a/Shared/Services/DappRequestProcessor.swift +++ b/Shared/Services/DappRequestProcessor.swift @@ -57,7 +57,7 @@ struct DappRequestProcessor { } private static func process(request: SafariRequest, nearRequest body: SafariRequest.Near, completion: @escaping () -> Void) -> DappRequestAction { - let peerMeta = PeerMeta(title: request.host, iconURLString: request.favicon) + let peerMeta = request.peerMeta lazy var account = getAccount(coin: .near, address: body.account) lazy var privateKey = getPrivateKey(coin: .near, address: body.account) @@ -108,7 +108,7 @@ struct DappRequestProcessor { } private static func process(request: SafariRequest, solanaRequest body: SafariRequest.Solana, completion: @escaping () -> Void) -> DappRequestAction { - let peerMeta = PeerMeta(title: request.host, iconURLString: request.favicon) + let peerMeta = request.peerMeta lazy var account = getAccount(coin: .solana, address: body.publicKey) lazy var privateKey = getPrivateKey(coin: .solana, address: body.publicKey) @@ -189,7 +189,7 @@ struct DappRequestProcessor { } private static func process(request: SafariRequest, ethereumRequest: SafariRequest.Ethereum, completion: @escaping () -> Void) -> DappRequestAction { - let peerMeta = PeerMeta(title: request.host, iconURLString: request.favicon) + let peerMeta = request.peerMeta lazy var account = getAccount(coin: .ethereum, address: ethereumRequest.address) switch ethereumRequest.method { diff --git a/Tokenary macOS/Agent.swift b/Tokenary macOS/Agent.swift index 38ab3d23..2099de5b 100644 --- a/Tokenary macOS/Agent.swift +++ b/Tokenary macOS/Agent.swift @@ -78,11 +78,11 @@ class Agent: NSObject { } else { let accountsList = instantiate(AccountsListViewController.self) - if case let .wcSession(session) = request { - accountsList.onSelectedWallet = onSelectedWallet(session: session) + if case let .wcSession(session) = request, let completion = onSelectedWallet(session: session) { + accountsList.accountSelectionConfiguration = AccountSelectionConfiguration(peer: nil, completion: completion) } - let windowController = Window.showNew(closeOthers: accountsList.onSelectedWallet == nil) + let windowController = Window.showNew(closeOthers: accountsList.accountSelectionConfiguration == nil) windowController.contentViewController = accountsList } } @@ -318,7 +318,7 @@ class Agent: NSObject { let windowController = Window.showNew(closeOthers: closeOtherWindows) windowNumber = windowController.window?.windowNumber let accountsList = instantiate(AccountsListViewController.self) - accountsList.onSelectedWallet = accountAction.completion + accountsList.accountSelectionConfiguration = AccountSelectionConfiguration(peer: safariRequest.peerMeta, 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 5962997a..8db12b3d 100644 --- a/Tokenary macOS/Screens/AccountsListViewController.swift +++ b/Tokenary macOS/Screens/AccountsListViewController.swift @@ -11,7 +11,7 @@ class AccountsListViewController: NSViewController { private var chain = EthereumChain.ethereum private var didCallCompletion = false - var onSelectedWallet: ((EthereumChain?, TokenaryWallet?, Account?) -> Void)? + var accountSelectionConfiguration: AccountSelectionConfiguration? var newWalletId: String? var getBackToRect: CGRect? @@ -77,7 +77,6 @@ class AccountsListViewController: NSViewController { reloadHeader() updateCellModels() - NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: NSApplication.didBecomeActiveNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(walletsChanged), name: Notification.Name.walletsChanged, object: nil) } @@ -98,17 +97,32 @@ class AccountsListViewController: NSViewController { private func callCompletion(wallet: TokenaryWallet?, account: Account?) { if !didCallCompletion { didCallCompletion = true - onSelectedWallet?(chain, wallet, account) + accountSelectionConfiguration?.completion(chain, wallet, account) } } private func reloadHeader() { - let canSelectAccount = onSelectedWallet != nil && !wallets.isEmpty + let canSelectAccount = accountSelectionConfiguration != nil && !wallets.isEmpty titleLabel.stringValue = canSelectAccount ? Strings.selectAccountTwoLines : Strings.wallets addButton.isHidden = wallets.isEmpty - titleLabelTopConstraint.constant = canSelectAccount ? 14 : 8 - websiteNameStackView.isHidden = !canSelectAccount + if let peer = accountSelectionConfiguration?.peer { + websiteNameLabel.stringValue = peer.name + titleLabelTopConstraint.constant = 14 + websiteNameStackView.isHidden = false + + if websiteLogoImageView.image == nil, let urlString = peer.iconURLString, let url = URL(string: urlString) { + websiteLogoImageView.kf.setImage(with: url) { [weak websiteLogoImageView] result in + if case .success = result { + websiteLogoImageView?.layer?.backgroundColor = NSColor.clear.cgColor + websiteLogoImageView?.layer?.cornerRadius = 0 + } + } + } + } else { + titleLabelTopConstraint.constant = 8 + websiteNameStackView.isHidden = true + } if canSelectAccount, networkButton.isHidden { networkButton.isHidden = false @@ -140,14 +154,6 @@ class AccountsListViewController: NSViewController { } } - @objc private func didBecomeActive() { - guard view.window?.isVisible == true else { return } - if let completion = agent.getWalletSelectionCompletionIfShouldSelect() { - onSelectedWallet = completion - } - reloadHeader() - } - @IBAction func addButtonTapped(_ sender: NSButton) { let menu = sender.menu @@ -231,7 +237,7 @@ class AccountsListViewController: NSViewController { @objc private func didClickImportAccount() { let importViewController = instantiate(ImportViewController.self) - importViewController.onSelectedWallet = onSelectedWallet + importViewController.accountSelectionConfiguration = accountSelectionConfiguration view.window?.contentViewController = importViewController } @@ -466,7 +472,7 @@ extension AccountsListViewController: AccountsHeaderDelegate { guard let wallet = walletForRow(row) else { return } let editAccountsViewController = instantiate(EditAccountsViewController.self) - editAccountsViewController.onSelectedWallet = onSelectedWallet + editAccountsViewController.accountSelectionConfiguration = accountSelectionConfiguration editAccountsViewController.wallet = wallet editAccountsViewController.getBackToRect = tableView.visibleRect view.window?.contentViewController = editAccountsViewController @@ -514,7 +520,7 @@ extension AccountsListViewController: NSTableViewDelegate { return false } - if onSelectedWallet != nil { + if accountSelectionConfiguration != nil { callCompletion(wallet: wallet, account: account) } else { showMenuOnCellSelection(row: row) diff --git a/Tokenary macOS/Screens/EditAccountsViewController.swift b/Tokenary macOS/Screens/EditAccountsViewController.swift index ff184399..ebb93992 100644 --- a/Tokenary macOS/Screens/EditAccountsViewController.swift +++ b/Tokenary macOS/Screens/EditAccountsViewController.swift @@ -7,7 +7,7 @@ class EditAccountsViewController: NSViewController { var wallet: TokenaryWallet! var getBackToRect: CGRect? - var onSelectedWallet: ((EthereumChain?, TokenaryWallet?, Account?) -> Void)? + var accountSelectionConfiguration: AccountSelectionConfiguration? struct CoinDerivationCellModel { let coinDerivation: CoinDerivation @@ -64,7 +64,7 @@ class EditAccountsViewController: NSViewController { private func showAccountsList() { let accountsListViewController = instantiate(AccountsListViewController.self) - accountsListViewController.onSelectedWallet = onSelectedWallet + accountsListViewController.accountSelectionConfiguration = accountSelectionConfiguration accountsListViewController.getBackToRect = getBackToRect view.window?.contentViewController = accountsListViewController } diff --git a/Tokenary macOS/Screens/ImportViewController.swift b/Tokenary macOS/Screens/ImportViewController.swift index bfe76709..3c9eb7a0 100644 --- a/Tokenary macOS/Screens/ImportViewController.swift +++ b/Tokenary macOS/Screens/ImportViewController.swift @@ -6,7 +6,7 @@ import WalletCore class ImportViewController: NSViewController { private let walletsManager = WalletsManager.shared - var onSelectedWallet: ((EthereumChain?, TokenaryWallet?, Account?) -> Void)? + var accountSelectionConfiguration: AccountSelectionConfiguration? 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.onSelectedWallet = onSelectedWallet + accountsListViewController.accountSelectionConfiguration = accountSelectionConfiguration accountsListViewController.newWalletId = newWalletId view.window?.contentViewController = accountsListViewController } diff --git a/Tokenary.xcodeproj/project.pbxproj b/Tokenary.xcodeproj/project.pbxproj index b4d60e18..8f86c9ef 100644 --- a/Tokenary.xcodeproj/project.pbxproj +++ b/Tokenary.xcodeproj/project.pbxproj @@ -102,6 +102,8 @@ 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 */; }; @@ -336,6 +338,7 @@ 2C6F6D5928273FE500D6E8FB /* EditAccountsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditAccountsViewController.swift; sourceTree = ""; }; 2C6F6D5B2827434800D6E8FB /* CoinDerivationTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CoinDerivationTableViewCell.xib; sourceTree = ""; }; 2C6F6D5C2827434800D6E8FB /* CoinDerivationTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoinDerivationTableViewCell.swift; sourceTree = ""; }; + 2C71175228AA62DE00ABBF2C /* AccountSelectionConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountSelectionConfiguration.swift; sourceTree = ""; }; 2C74386E28297DAC00EC9304 /* near.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; name = near.js; path = "web3-provider/near.js"; sourceTree = ""; }; 2C773F5D27450B97007B04E7 /* ExtensionBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionBridge.swift; sourceTree = ""; }; 2C773F61274523DC007B04E7 /* ResponseToExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseToExtension.swift; sourceTree = ""; }; @@ -860,6 +863,7 @@ 0D059AD126C2796200EE3023 /* ApprovalSubject.swift */, 2C09FC652828331D00DE9C27 /* Image.swift */, 2C89D26727BADCA9006C0C8D /* DappRequestAction.swift */, + 2C71175228AA62DE00ABBF2C /* AccountSelectionConfiguration.swift */, 0DC850E626B73A5900809E82 /* AuthenticationReason.swift */, ); path = Models; @@ -1406,6 +1410,7 @@ 2C40379428199110004C7263 /* Solana.swift in Sources */, 2C8A09DF267579EA00993638 /* AccountsListViewController.swift in Sources */, 2C917429267D2A6E00049075 /* Keychain.swift in Sources */, + 2C71175328AA62DE00ABBF2C /* AccountSelectionConfiguration.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1428,6 +1433,7 @@ 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 */,