diff --git a/Encrypted Ink/Agent.swift b/Encrypted Ink/Agent.swift index 845ab683..dbc5e88b 100644 --- a/Encrypted Ink/Agent.swift +++ b/Encrypted Ink/Agent.swift @@ -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 = "" diff --git a/Encrypted Ink/AppDelegate.swift b/Encrypted Ink/AppDelegate.swift index c34c5cef..444f1288 100644 --- a/Encrypted Ink/AppDelegate.swift +++ b/Encrypted Ink/AppDelegate.swift @@ -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 } diff --git a/Encrypted Ink/Screens/AccountsListViewController.swift b/Encrypted Ink/Screens/AccountsListViewController.swift index 758a1806..1c42b352 100644 --- a/Encrypted Ink/Screens/AccountsListViewController.swift +++ b/Encrypted Ink/Screens/AccountsListViewController.swift @@ -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 + } } } diff --git a/Encrypted Ink/Screens/ImportViewController.swift b/Encrypted Ink/Screens/ImportViewController.swift index f2bf2269..144624db 100644 --- a/Encrypted Ink/Screens/ImportViewController.swift +++ b/Encrypted Ink/Screens/ImportViewController.swift @@ -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 }