MacOS-only 🎤⬇️

This commit is contained in:
Ivan Grachyov 2021-06-13 13:13:47 +03:00
parent 67588ef733
commit d8c2b3130d
6 changed files with 33 additions and 87 deletions

View File

@ -27,7 +27,6 @@
2C1995472674C4BA00A8E370 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2C1995452674C4BA00A8E370 /* Main.storyboard */; };
2C1995562674D0F300A8E370 /* Ethereum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C1995552674D0F300A8E370 /* Ethereum.swift */; };
2C19955B2674D54600A8E370 /* WalletConnect in Frameworks */ = {isa = PBXBuildFile; productRef = 2C19955A2674D54600A8E370 /* WalletConnect */; };
2C1995742674E80E00A8E370 /* NearbyConnectivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C1995732674E80E00A8E370 /* NearbyConnectivity.swift */; };
2C8A09B52675101300993638 /* AccountsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8A09B42675101300993638 /* AccountsService.swift */; };
2C8A09C6267513FC00993638 /* Agent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8A09C5267513FC00993638 /* Agent.swift */; };
2C8A09D42675184700993638 /* Window.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8A09D32675184700993638 /* Window.swift */; };
@ -66,7 +65,6 @@
2C1995482674C4BA00A8E370 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2C1995492674C4BA00A8E370 /* Encrypted_Ink.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Encrypted_Ink.entitlements; sourceTree = "<group>"; };
2C1995552674D0F300A8E370 /* Ethereum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Ethereum.swift; sourceTree = "<group>"; };
2C1995732674E80E00A8E370 /* NearbyConnectivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NearbyConnectivity.swift; sourceTree = "<group>"; };
2C8A09B42675101300993638 /* AccountsService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsService.swift; sourceTree = "<group>"; };
2C8A09C5267513FC00993638 /* Agent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Agent.swift; sourceTree = "<group>"; };
2C8A09D32675184700993638 /* Window.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Window.swift; sourceTree = "<group>"; };
@ -153,7 +151,6 @@
children = (
2C19953F2674C4B900A8E370 /* AppDelegate.swift */,
2C8A09C5267513FC00993638 /* Agent.swift */,
2C1995732674E80E00A8E370 /* NearbyConnectivity.swift */,
2C8A09D626751A0C00993638 /* WalletConnect.swift */,
2C8A09D32675184700993638 /* Window.swift */,
2C8A09C92675142700993638 /* Screens */,
@ -371,7 +368,6 @@
2C8A09EB2675964700993638 /* ApproveViewController.swift in Sources */,
2C1995562674D0F300A8E370 /* Ethereum.swift in Sources */,
2C8A09DF267579EA00993638 /* AccountsListViewController.swift in Sources */,
2C1995742674E80E00A8E370 /* NearbyConnectivity.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -1,24 +1,24 @@
// Copyright © 2021 Encrypted Ink. All rights reserved.
import Cocoa
import WalletConnect
class Agent {
static let shared = Agent()
private var connectivity: NearbyConnectivity!
private init() {}
private var statusBarItem: NSStatusItem!
func start() {
connectivity = NearbyConnectivity(delegate: self)
showInitialScreen(onAppStart: true, wcLink: nil)
showInitialScreen(onAppStart: true, wcSession: nil)
}
func reopen() {
showInitialScreen(onAppStart: false, wcLink: nil)
showInitialScreen(onAppStart: false, wcSession: nil)
}
func showInitialScreen(onAppStart: Bool, wcLink: String?) {
func showInitialScreen(onAppStart: Bool, wcSession: WCSession?) {
let windowController: NSWindowController
if onAppStart, let currentWindowController = Window.current {
windowController = currentWindowController
@ -28,9 +28,9 @@ class Agent {
}
var onSelectedAccount: ((Account) -> Void)?
if let link = wcLink {
if let wcSession = wcSession {
onSelectedAccount = { [weak self] account in
self?.connectWalletWithLink(link, account: account)
self?.connectWallet(session: wcSession, account: account)
}
}
@ -61,8 +61,25 @@ class Agent {
windowController.contentViewController = ErrorViewController.withMessage(message)
}
private func connectWalletWithLink(_ link: String, account: Account) {
WalletConnect.shared.connect(link: link, address: account.address) { [weak self] connected in
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()
@ -76,11 +93,3 @@ class Agent {
}
}
extension Agent: NearbyConnectivityDelegate {
func didFind(link: String) {
showInitialScreen(onAppStart: false, wcLink: link)
}
}

View File

@ -11,10 +11,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
Agent.shared.start()
Agent.shared.setupStatusBarItem()
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
// TODO: make sure it is called only when icon tapped
Agent.shared.reopen()
return true
}

View File

@ -1,61 +0,0 @@
// Copyright © 2021 Encrypted Ink. All rights reserved.
import Foundation
import MultipeerConnectivity
private let serviceIdentifier = "connector"
private let queue = DispatchQueue(label: serviceIdentifier, qos: .default)
private var acceptedMessages = Set<String>()
protocol NearbyConnectivityDelegate: AnyObject {
func didFind(link: String)
}
class NearbyConnectivity: NSObject {
private weak var connectivityDelegate: NearbyConnectivityDelegate?
private var devicePeerID: MCPeerID!
private var serviceBrowser: MCNearbyServiceBrowser!
init(delegate: NearbyConnectivityDelegate) {
super.init()
connectivityDelegate = delegate
devicePeerID = MCPeerID(displayName: UUID().uuidString)
serviceBrowser = MCNearbyServiceBrowser(peer: devicePeerID, serviceType: serviceIdentifier)
serviceBrowser.delegate = self
autoConnect()
}
deinit {
stopBrowsing()
}
private func stopBrowsing() {
serviceBrowser.stopBrowsingForPeers()
}
private func autoConnect() {
queue.async { [weak self] in
self?.serviceBrowser.startBrowsingForPeers()
}
}
}
// MARK: - Browser Delegate
extension NearbyConnectivity: MCNearbyServiceBrowserDelegate {
func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String: String]?) {
let peerName = peerID.displayName
guard let info = info, let link = info["wclink"], !acceptedMessages.contains(peerName) else { return }
acceptedMessages.insert(peerName)
DispatchQueue.main.async { [weak connectivityDelegate] in
connectivityDelegate?.didFind(link: link)
}
}
func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) { }
}

View File

@ -10,11 +10,12 @@ class WalletConnect {
private var interactors = [WCInteractor]()
func connect(link: String, address: String, completion: @escaping ((Bool) -> Void)) {
func sessionWithLink(_ link: String) -> WCSession? {
return WCSession.from(string: link)
}
func connect(session: WCSession, address: String, completion: @escaping ((Bool) -> Void)) {
let clientMeta = WCPeerMeta(name: "Encrypted Ink", url: "https://encrypted.ink")
guard let session = WCSession.from(string: link) else {
return
}
let interactor = WCInteractor(session: session, meta: clientMeta, uuid: UUID())
configure(interactor: interactor, address: address)

View File

@ -19,6 +19,7 @@ struct Window {
static func closeAll() {
NSApplication.shared.windows.forEach { $0.close() }
Agent.shared.setupStatusBarItem()
}
static func activateSafari() {