Add AccountSelectionConfiguration

This commit is contained in:
Ivan Grachev 2022-08-15 15:39:48 +03:00
parent 19873bd3da
commit 2924671735
8 changed files with 57 additions and 28 deletions

View File

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

View File

@ -23,3 +23,11 @@ struct PeerMeta {
}
}
extension SafariRequest {
var peerMeta: PeerMeta {
return PeerMeta(title: host, iconURLString: favicon)
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 = "<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>"; };
@ -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 */,