Select accounts for WC

This commit is contained in:
Ivan Grachyov 2021-06-13 06:30:20 +03:00
parent ff9e0501bd
commit 146b151761
4 changed files with 55 additions and 28 deletions

View File

@ -4,18 +4,21 @@ import Cocoa
class Agent {
static let shared = Agent()
private var connectivity: NearbyConnectivity!
private init() {}
func start() {
connectivity = NearbyConnectivity(delegate: self)
showInitialScreen(onAppStart: true)
showInitialScreen(onAppStart: true, wcLink: nil)
}
func reopen() {
showInitialScreen(onAppStart: false)
showInitialScreen(onAppStart: false, wcLink: nil)
}
func showInitialScreen(onAppStart: Bool) {
func showInitialScreen(onAppStart: Bool, wcLink: String?) {
let windowController: NSWindowController
if onAppStart, let currentWindowController = Window.current {
windowController = currentWindowController
@ -24,23 +27,41 @@ class Agent {
windowController = Window.showNew()
}
var onSelectedAccount: ((Account) -> Void)?
if let link = wcLink {
onSelectedAccount = { [weak self] account in
self?.connectWalletWithLink(link, account: account)
}
}
let accounts = AccountsService.getAccounts()
if !accounts.isEmpty {
windowController.contentViewController = AccountsListViewController.with(preloadedAccounts: accounts)
let accountsList = AccountsListViewController.with(preloadedAccounts: accounts)
accountsList.onSelectedAccount = onSelectedAccount
windowController.contentViewController = accountsList
} else {
windowController.contentViewController = instantiate(ImportViewController.self)
let importViewController = instantiate(ImportViewController.self)
importViewController.onSelectedAccount = onSelectedAccount
windowController.contentViewController = importViewController
}
}
func connectWalletWithLink(_ link: String, account: Account) {
WalletConnect.shared.connect(link: link, address: account.address) { connected in
// TODO: close here
// use connected value
}
// TODO: show spinner
Window.closeAll()
Window.activateSafari()
}
}
extension Agent: NearbyConnectivityDelegate {
func didFind(link: String) {
globalLink = link
// showScreen() // TODO: should show account selection
showInitialScreen(onAppStart: false, wcLink: link)
}
}
var globalLink = ""

View File

@ -4,19 +4,18 @@ import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let agent = Agent()
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return false
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
agent.start()
Agent.shared.start()
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
agent.reopen()
// TODO: make sure it is called only when icon tapped
Agent.shared.reopen()
return true
}

View File

@ -6,6 +6,8 @@ class AccountsListViewController: NSViewController {
private var accounts = [Account]()
var onSelectedAccount: ((Account) -> Void)?
static func with(preloadedAccounts: [Account]) -> AccountsListViewController {
let new = instantiate(AccountsListViewController.self)
new.accounts = preloadedAccounts
@ -31,10 +33,15 @@ class AccountsListViewController: NSViewController {
if accounts.isEmpty {
accounts = AccountsService.getAccounts()
}
if onSelectedAccount != nil {
titleLabel.stringValue = "Select\nAccount"
}
}
@IBAction func addButtonTapped(_ sender: NSButton) {
let importViewController = instantiate(ImportViewController.self)
importViewController.onSelectedAccount = onSelectedAccount
view.window?.contentViewController = importViewController
}
@ -59,8 +66,13 @@ class AccountsListViewController: NSViewController {
extension AccountsListViewController: NSTableViewDelegate {
func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
// TODO: jump somewhere else
return true
if let onSelectedAccount = onSelectedAccount {
let account = accounts[row]
onSelectedAccount(account)
return true
} else {
return false
}
}
}

View File

@ -4,6 +4,8 @@ import Cocoa
class ImportViewController: NSViewController {
var onSelectedAccount: ((Account) -> Void)?
@IBOutlet weak var textField: NSTextField! {
didSet {
textField.delegate = self
@ -16,24 +18,17 @@ class ImportViewController: NSViewController {
}
@IBAction func actionButtonTapped(_ sender: Any) {
AccountsService.addAccount(privateKey: textField.stringValue)
// TODO: open accounts list
WalletConnect.shared.connect(link: globalLink, address: "0xCf60CC6E4AD79187E7eBF62e0c21ae3a343180B2") { connected in
// TODO: close here
// use connected value
if let account = AccountsService.addAccount(privateKey: textField.stringValue),
let onSelectedAccount = onSelectedAccount {
onSelectedAccount(account)
} else {
showAccountsList()
}
showAccountsList()
// TODO: show spinner
// Window.closeAll()
// Window.activateSafari()
}
private func showAccountsList() {
let accountsListViewController = instantiate(AccountsListViewController.self)
accountsListViewController.onSelectedAccount = onSelectedAccount
view.window?.contentViewController = accountsListViewController
}