2021-06-12 19:16:23 +03:00
|
|
|
// Copyright © 2021 Encrypted Ink. All rights reserved.
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
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
|
|
|
private var connectivity: NearbyConnectivity!
|
|
|
|
|
2021-06-13 06:30:20 +03:00
|
|
|
private init() {}
|
|
|
|
|
2021-06-12 19:16:23 +03:00
|
|
|
func start() {
|
|
|
|
connectivity = NearbyConnectivity(delegate: self)
|
2021-06-13 06:30:20 +03:00
|
|
|
showInitialScreen(onAppStart: true, wcLink: nil)
|
2021-06-13 05:45:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func reopen() {
|
2021-06-13 06:30:20 +03:00
|
|
|
showInitialScreen(onAppStart: false, wcLink: nil)
|
2021-06-13 05:45:54 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 06:30:20 +03:00
|
|
|
func showInitialScreen(onAppStart: Bool, wcLink: String?) {
|
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)?
|
|
|
|
if let link = wcLink {
|
|
|
|
onSelectedAccount = { [weak self] account in
|
|
|
|
self?.connectWalletWithLink(link, account: account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 06:30:20 +03:00
|
|
|
func connectWalletWithLink(_ link: String, account: Account) {
|
|
|
|
WalletConnect.shared.connect(link: link, address: account.address) { connected in
|
2021-06-13 07:01:01 +03:00
|
|
|
Window.closeAll()
|
|
|
|
Window.activateSafari()
|
|
|
|
// TODO: show error if 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
|
|
|
}
|
|
|
|
|
|
|
|
extension Agent: NearbyConnectivityDelegate {
|
|
|
|
|
|
|
|
func didFind(link: String) {
|
2021-06-13 06:30:20 +03:00
|
|
|
showInitialScreen(onAppStart: false, wcLink: link)
|
2021-06-12 19:16:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|