tokenary/Encrypted Ink/Agent.swift

96 lines
3.4 KiB
Swift
Raw Normal View History

2021-06-12 19:16:23 +03:00
// Copyright © 2021 Encrypted Ink. All rights reserved.
import Cocoa
2021-06-13 13:13:47 +03:00
import WalletConnect
2021-06-12 19:16:23 +03:00
class Agent {
2021-06-13 05:45:54 +03:00
2021-06-13 06:30:20 +03:00
static let shared = Agent()
2021-06-12 19:16:23 +03:00
2021-06-13 06:30:20 +03:00
private init() {}
2021-06-13 13:13:47 +03:00
private var statusBarItem: NSStatusItem!
2021-06-13 06:30:20 +03:00
2021-06-12 19:16:23 +03:00
func start() {
2021-06-13 13:13:47 +03:00
showInitialScreen(onAppStart: true, wcSession: nil)
2021-06-13 05:45:54 +03:00
}
func reopen() {
2021-06-13 13:13:47 +03:00
showInitialScreen(onAppStart: false, wcSession: nil)
2021-06-13 05:45:54 +03:00
}
2021-06-13 13:13:47 +03:00
func showInitialScreen(onAppStart: Bool, wcSession: WCSession?) {
2021-06-13 05:45:54 +03:00
let windowController: NSWindowController
if onAppStart, let currentWindowController = Window.current {
windowController = currentWindowController
Window.activate(windowController)
} else {
windowController = Window.showNew()
}
2021-06-13 06:30:20 +03:00
var onSelectedAccount: ((Account) -> Void)?
2021-06-13 13:13:47 +03:00
if let wcSession = wcSession {
2021-06-13 06:30:20 +03:00
onSelectedAccount = { [weak self] account in
2021-06-13 13:13:47 +03:00
self?.connectWallet(session: wcSession, account: account)
2021-06-13 06:30:20 +03:00
}
}
2021-06-13 05:45:54 +03:00
let accounts = AccountsService.getAccounts()
if !accounts.isEmpty {
2021-06-13 06:30:20 +03:00
let accountsList = AccountsListViewController.with(preloadedAccounts: accounts)
accountsList.onSelectedAccount = onSelectedAccount
windowController.contentViewController = accountsList
2021-06-13 05:45:54 +03:00
} else {
2021-06-13 06:30:20 +03:00
let importViewController = instantiate(ImportViewController.self)
importViewController.onSelectedAccount = onSelectedAccount
windowController.contentViewController = importViewController
2021-06-13 05:45:54 +03:00
}
2021-06-12 19:16:23 +03:00
}
2021-06-13 09:32:07 +03:00
func showApprove(title: String, meta: String, completion: @escaping (Bool) -> Void) {
let windowController = Window.showNew()
let approveViewController = ApproveViewController.with(title: title, meta: meta) { result in
completion(result)
Window.closeAll()
Window.activateSafari()
}
windowController.contentViewController = approveViewController
}
func showErrorMessage(_ message: String) {
let windowController = Window.showNew()
windowController.contentViewController = ErrorViewController.withMessage(message)
}
2021-06-13 13:13:47 +03:00
func setupStatusBarItem() {
let statusBar = NSStatusBar.system
statusBarItem = statusBar.statusItem(withLength: NSStatusItem.squareLength)
statusBarItem.button?.title = "🍎"
statusBarItem.button?.target = self
statusBarItem.button?.action = #selector(statusBarButtonClicked(sender:))
statusBarItem.button?.sendAction(on: [.leftMouseUp])
}
@objc private func statusBarButtonClicked(sender: NSStatusBarButton) {
let pasteboard = NSPasteboard.general
let link = pasteboard.string(forType: .string) ?? ""
pasteboard.clearContents()
let session = WalletConnect.shared.sessionWithLink(link)
showInitialScreen(onAppStart: false, wcSession: session)
}
private func connectWallet(session: WCSession, account: Account) {
WalletConnect.shared.connect(session: session, address: account.address) { [weak self] connected in
if connected {
Window.closeAll()
Window.activateSafari()
} else {
self?.showErrorMessage("Failed to connect")
}
2021-06-13 06:30:20 +03:00
}
2021-06-13 07:01:01 +03:00
let windowController = Window.showNew()
windowController.contentViewController = WaitingViewController.withReason("Connecting")
2021-06-13 06:30:20 +03:00
}
2021-06-12 19:16:23 +03:00
}