QR code: Integrate Eric's QR code scanner
Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
@@ -11,25 +11,21 @@ import CoreData
|
||||
import UIKit
|
||||
|
||||
protocol QRScanViewControllerDelegate: class {
|
||||
func didSave(tunnel: Tunnel, qrScanViewController: QRScanViewController)
|
||||
func scannedQRCode(tunnelConfiguration: TunnelConfiguration, qrScanViewController: QRScanViewController)
|
||||
}
|
||||
|
||||
class QRScanViewController: UIViewController {
|
||||
|
||||
private var viewContext: NSManagedObjectContext!
|
||||
private weak var delegate: QRScanViewControllerDelegate?
|
||||
weak var delegate: QRScanViewControllerDelegate?
|
||||
var captureSession: AVCaptureSession? = AVCaptureSession()
|
||||
let metadataOutput = AVCaptureMetadataOutput()
|
||||
var previewLayer: AVCaptureVideoPreviewLayer!
|
||||
|
||||
func configure(context: NSManagedObjectContext, delegate: QRScanViewControllerDelegate? = nil) {
|
||||
viewContext = context
|
||||
self.delegate = delegate
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
self.title = "Scan QR code"
|
||||
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelTapped))
|
||||
|
||||
guard let videoCaptureDevice = AVCaptureDevice.default(for: .video),
|
||||
let videoInput = try? AVCaptureDeviceInput(device: videoCaptureDevice),
|
||||
let captureSession = captureSession,
|
||||
@@ -69,8 +65,9 @@ class QRScanViewController: UIViewController {
|
||||
|
||||
func scanDidComplete(withCode code: String) {
|
||||
do {
|
||||
let tunnel = try Tunnel.fromConfig(code, context: viewContext)
|
||||
delegate?.didSave(tunnel: tunnel, qrScanViewController: self)
|
||||
let tunnelConfiguration = try WgQuickConfigFileParser.parse(code, name: "Scanned")
|
||||
delegate?.scannedQRCode(tunnelConfiguration: tunnelConfiguration, qrScanViewController: self)
|
||||
dismiss(animated: true, completion: nil)
|
||||
} catch {
|
||||
scanDidEncounterError(title: "Invalid Code", message: "The scanned code is not a valid WireGuard config file.")
|
||||
}
|
||||
@@ -85,6 +82,9 @@ class QRScanViewController: UIViewController {
|
||||
captureSession = nil
|
||||
}
|
||||
|
||||
@objc func cancelTapped() {
|
||||
dismiss(animated: true, completion: nil)
|
||||
}
|
||||
}
|
||||
|
||||
extension QRScanViewController: AVCaptureMetadataOutputObjectsDelegate {
|
||||
@@ -101,7 +101,4 @@ extension QRScanViewController: AVCaptureMetadataOutputObjectsDelegate {
|
||||
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
|
||||
scanDidComplete(withCode: stringValue)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension QRScanViewController: Identifyable {}
|
||||
|
||||
@@ -47,6 +47,11 @@ class TunnelsListTableViewController: UITableViewController {
|
||||
}
|
||||
alert.addAction(importFileAction)
|
||||
|
||||
let scanQRCodeAction = UIAlertAction(title: "Scan QR code", style: .default) { [weak self] (action) in
|
||||
self?.presentViewControllerForScanningQRCode()
|
||||
}
|
||||
alert.addAction(scanQRCodeAction)
|
||||
|
||||
let createFromScratchAction = UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (action) in
|
||||
if let s = self, let tunnelsManager = s.tunnelsManager {
|
||||
s.presentViewControllerForTunnelCreation(tunnelsManager: tunnelsManager, tunnelConfiguration: nil)
|
||||
@@ -96,6 +101,14 @@ class TunnelsListTableViewController: UITableViewController {
|
||||
self.present(filePicker, animated: true)
|
||||
}
|
||||
|
||||
func presentViewControllerForScanningQRCode() {
|
||||
let scanQRCodeVC = QRScanViewController()
|
||||
scanQRCodeVC.delegate = self
|
||||
let scanQRCodeNC = UINavigationController(rootViewController: scanQRCodeVC)
|
||||
scanQRCodeNC.modalPresentationStyle = .fullScreen
|
||||
self.present(scanQRCodeNC, animated: true)
|
||||
}
|
||||
|
||||
func showErrorAlert(title: String, message: String) {
|
||||
let okAction = UIAlertAction(title: "Ok", style: .default)
|
||||
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
||||
@@ -130,6 +143,14 @@ extension TunnelsListTableViewController: UIDocumentPickerDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: QRScanViewControllerDelegate
|
||||
|
||||
extension TunnelsListTableViewController: QRScanViewControllerDelegate {
|
||||
func scannedQRCode(tunnelConfiguration: TunnelConfiguration, qrScanViewController: QRScanViewController) {
|
||||
print("Scanned QR code") // TODO
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: UITableViewDataSource
|
||||
|
||||
extension TunnelsListTableViewController {
|
||||
|
||||
Reference in New Issue
Block a user