BlueWallet/ios/BlueWalletWatch Extension/ViewQRCodefaceController.swift

106 lines
3.4 KiB
Swift
Raw Normal View History

2020-08-17 05:56:05 +02:00
//
// ReceiveInterfaceController.swift
// BlueWalletWatch Extension
//
// Created by Marcos Rodriguez on 3/12/19.
// Copyright © 2019 Facebook. All rights reserved.
//
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!
var address: String? {
didSet {
if let address = address, !address.isEmpty{
userActivity.userInfo = [HandOffUserInfoKey.Xpub.rawValue: address]
userActivity.becomeCurrent()
}
}
}
2020-08-19 04:25:59 +02:00
private var interfaceMode = InterfaceMode.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)
userActivity.title = HandOffTitle.Xpub.rawValue
userActivity.requiredUserInfoKeys = [HandOffUserInfoKey.Xpub.rawValue]
userActivity.isEligibleForHandoff = true
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)
2020-08-17 05:56:05 +02:00
DispatchQueue.main.async {
guard let cgImage = EFQRCode.generate(
2023-10-27 05:45:55 +02:00
for: passedContext) else {
2020-08-17 05:56:05 +02:00
return
}
let image = UIImage(cgImage: cgImage)
self.imageInterface.setImage(nil)
self.imageInterface.setImage(image)
}
2020-08-19 04:25:59 +02:00
if #available(watchOSApplicationExtension 6.0, *) {
if let image = UIImage(systemName: "textformat.subscript") {
addMenuItem(with: image, title: "Address", action:#selector(toggleViewButtonPressed))
} else {
addMenuItem(with: .shuffle, title: "Address", action: #selector(toggleViewButtonPressed))
}
} else {
addMenuItem(with: .shuffle, title: "Address", action: #selector(toggleViewButtonPressed))
}
}
@IBAction @objc func toggleViewButtonPressed() {
clearAllMenuItems()
switch interfaceMode {
case .Address:
addressLabel.setHidden(false)
imageInterface.setHidden(true)
if #available(watchOSApplicationExtension 6.0, *) {
if let image = UIImage(systemName: "qrcode") {
addMenuItem(with: image, title: "QR Code", action:#selector(toggleViewButtonPressed))
} else {
addMenuItem(with: .shuffle, title: "QR Code", action: #selector(toggleViewButtonPressed))
}
} else {
addMenuItem(with: .shuffle, title: "QR Code", action: #selector(toggleViewButtonPressed))
}
case .QRCode:
addressLabel.setHidden(true)
imageInterface.setHidden(false)
if #available(watchOSApplicationExtension 6.0, *) {
if let image = UIImage(systemName: "textformat.subscript") {
addMenuItem(with: image, title: "Address", action:#selector(toggleViewButtonPressed))
} else {
addMenuItem(with: .shuffle, title: "Address", action: #selector(toggleViewButtonPressed))
}
} else {
addMenuItem(with: .shuffle, title: "Address", action: #selector(toggleViewButtonPressed))
}
}
interfaceMode = interfaceMode == .QRCode ? .Address : .QRCode
2020-08-17 05:56:05 +02:00
}
2021-09-28 03:27:03 +02:00
override func willActivate() {
super.willActivate()
update(userActivity)
}
2021-09-28 03:27:03 +02:00
override func didDeactivate() {
super.didDeactivate()
userActivity.invalidate()
invalidateUserActivity()
}
2020-08-17 05:56:05 +02:00
}