tokenary/Tokenary macOS/AppDelegate.swift

86 lines
3.1 KiB
Swift
Raw Normal View History

2021-11-30 15:56:00 +03:00
// Copyright © 2021 Tokenary. All rights reserved.
2021-06-12 13:32:27 +03:00
import Cocoa
2021-06-12 18:22:02 +03:00
@NSApplicationMain
2021-06-12 13:32:27 +03:00
class AppDelegate: NSObject, NSApplicationDelegate {
2021-06-12 18:48:23 +03:00
private let agent = Agent.shared
2021-06-28 16:20:08 +03:00
private let gasService = GasService.shared
2021-07-04 18:05:50 +03:00
private let priceService = PriceService.shared
2021-07-11 18:21:20 +03:00
private let networkMonitor = NetworkMonitor.shared
2021-08-01 13:53:13 +03:00
private let walletsManager = WalletsManager.shared
2021-11-22 13:11:34 +03:00
private let walletConnect = WalletConnect.shared
private var didFinishLaunching = false
2021-11-22 13:11:34 +03:00
private var initialExternalRequest: Agent.ExternalRequest?
2021-07-07 20:50:09 +03:00
override init() {
super.init()
let manager = NSAppleEventManager.shared()
manager.setEventHandler(self, andSelector: #selector(self.getUrl(_:withReplyEvent:)),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL))
}
2021-06-13 01:35:24 +03:00
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return false
}
2021-07-07 20:50:09 +03:00
@objc private func getUrl(_ event: NSAppleEventDescriptor, withReplyEvent replyEvent: NSAppleEventDescriptor) {
2021-11-22 13:11:34 +03:00
processInput(url: event.paramDescriptor(forKeyword: keyDirectObject)?.stringValue)
2021-07-07 20:50:09 +03:00
}
2021-06-12 13:32:27 +03:00
func applicationDidFinishLaunching(_ aNotification: Notification) {
2021-11-30 20:46:02 +03:00
walletsManager.migrateFromLegacyIfNeeded()
agent.start()
2021-06-28 16:20:08 +03:00
gasService.start()
2021-07-04 18:05:50 +03:00
priceService.start()
2021-07-11 18:21:20 +03:00
networkMonitor.start()
2021-08-01 13:53:13 +03:00
walletsManager.start()
didFinishLaunching = true
2021-11-22 13:11:34 +03:00
if let externalRequest = initialExternalRequest {
initialExternalRequest = nil
agent.showInitialScreen(externalRequest: externalRequest)
}
2021-06-12 13:32:27 +03:00
}
2021-11-22 13:11:34 +03:00
2021-06-12 21:15:32 +03:00
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
agent.reopen()
2021-06-12 21:15:32 +03:00
return true
}
2021-06-15 00:08:26 +03:00
func application(_ application: NSApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([NSUserActivityRestoring]) -> Void) -> Bool {
2021-11-22 13:11:34 +03:00
processInput(url: userActivity.webpageURL?.absoluteString)
2021-07-07 22:53:28 +03:00
return true
}
2021-11-22 13:11:34 +03:00
private func processInput(url: String?) {
guard let url = url else { return }
2021-11-30 15:56:00 +03:00
for scheme in ["https://tokenary.io/wc?uri=", "tokenary://wc?uri="] {
2021-11-22 13:11:34 +03:00
if url.hasPrefix(scheme), let link = url.dropFirst(scheme.count).removingPercentEncoding, let session = walletConnect.sessionWithLink(link) {
processExternalRequest(.wcSession(session))
return
}
2021-06-15 00:08:26 +03:00
}
2021-11-22 13:11:34 +03:00
2021-11-30 15:56:00 +03:00
let safariPrefix = "tokenary://safari?request="
2021-11-22 13:11:34 +03:00
if url.hasPrefix(safariPrefix), let request = SafariRequest(query: String(url.dropFirst(safariPrefix.count))) {
processExternalRequest(.safari(request))
}
}
private func processExternalRequest(_ externalRequest: Agent.ExternalRequest) {
if didFinishLaunching {
agent.showInitialScreen(externalRequest: externalRequest)
} else {
initialExternalRequest = externalRequest
}
2021-06-15 00:08:26 +03:00
}
2021-06-12 18:48:23 +03:00
}