BlueWallet/ios/BlueWalletWatch Extension/ViewQRCodefaceController.swift

96 lines
2.8 KiB
Swift
Raw Normal View History

2020-08-17 05:56:05 +02:00
// BlueWalletWatch Extension
//
2024-01-25 05:39:01 +01:00
// Created by Marcos Rodriguez on 3/11/19.
2020-08-17 05:56:05 +02:00
import WatchKit
import Foundation
import EFQRCode
class ViewQRCodefaceController: WKInterfaceController {
static let identifier = "ViewQRCodefaceController"
@IBOutlet weak var imageInterface: WKInterfaceImage!
2020-08-19 04:25:59 +02:00
@IBOutlet weak var addressLabel: WKInterfaceLabel!
2024-01-25 05:39:01 +01:00
var address: String? {
didSet {
2024-01-25 05:39:01 +01:00
updateQRCode()
updateUserActivity()
}
}
2024-01-25 05:39:01 +01:00
2024-06-15 19:05:54 +02:00
private var interfaceMode = ReceiveInterfaceMode.Address
2021-09-28 04:57:27 +02:00
private let userActivity: NSUserActivity = NSUserActivity(activityType: HandoffIdentifier.Xpub.rawValue)
2021-09-28 03:27:03 +02:00
2020-08-17 05:56:05 +02:00
override func awake(withContext context: Any?) {
super.awake(withContext: context)
2024-01-25 05:39:01 +01:00
configureUserActivity()
2020-08-17 05:56:05 +02:00
guard let passedContext = context as? String else {
pop()
return
}
address = passedContext
2020-08-19 04:25:59 +02:00
addressLabel.setText(passedContext)
2024-01-25 05:39:01 +01:00
toggleViewButtonPressed()
}
2024-01-25 05:39:01 +01:00
private func configureUserActivity() {
userActivity.title = HandOffTitle.Xpub.rawValue
userActivity.requiredUserInfoKeys = [HandOffUserInfoKey.Xpub.rawValue]
userActivity.isEligibleForHandoff = true
}
private func updateUserActivity() {
if let address = address, !address.isEmpty {
userActivity.userInfo = [HandOffUserInfoKey.Xpub.rawValue: address]
userActivity.becomeCurrent()
2020-08-19 04:25:59 +02:00
} else {
2024-01-25 05:39:01 +01:00
userActivity.invalidate()
2020-08-19 04:25:59 +02:00
}
}
2024-01-25 05:39:01 +01:00
private func updateQRCode() {
guard let address = address, !address.isEmpty else {
imageInterface.setImage(nil)
return
}
DispatchQueue.global(qos: .userInteractive).async {
guard let cgImage = EFQRCode.generate(for: address) else {
return
2020-08-19 04:25:59 +02:00
}
2024-01-25 05:39:01 +01:00
DispatchQueue.main.async {
let image = UIImage(cgImage: cgImage)
self.imageInterface.setImage(image)
2020-08-19 04:25:59 +02:00
}
}
2024-01-25 05:39:01 +01:00
}
@IBAction @objc func toggleViewButtonPressed() {
clearAllMenuItems()
2020-08-19 04:25:59 +02:00
interfaceMode = interfaceMode == .QRCode ? .Address : .QRCode
2024-01-25 05:39:01 +01:00
let menuItemTitle = interfaceMode == .QRCode ? "QR Code" : "Address"
let systemImageName = interfaceMode == .QRCode ? "textformat.subscript" : "qrcode"
let defaultMenuItemIcon = interfaceMode == .QRCode ? WKMenuItemIcon.shuffle : WKMenuItemIcon.shuffle
addressLabel.setHidden(interfaceMode != .Address)
imageInterface.setHidden(interfaceMode != .QRCode)
if #available(watchOSApplicationExtension 6.0, *), let image = UIImage(systemName: systemImageName) {
addMenuItem(with: image, title: menuItemTitle, action: #selector(toggleViewButtonPressed))
} else {
addMenuItem(with: defaultMenuItemIcon, title: menuItemTitle, action: #selector(toggleViewButtonPressed))
}
2020-08-17 05:56:05 +02:00
}
2021-09-28 03:27:03 +02:00
override func willActivate() {
super.willActivate()
2024-01-25 05:39:01 +01:00
updateUserActivity()
2021-09-28 03:27:03 +02:00
}
2021-09-28 03:27:03 +02:00
override func didDeactivate() {
super.didDeactivate()
userActivity.invalidate()
}
2020-08-17 05:56:05 +02:00
}