session-ios/Session/Shared/ScanQRCodeWrapperVC.swift

87 lines
2.9 KiB
Swift
Raw Normal View History

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
2019-11-19 04:35:38 +01:00
import UIKit
import SessionUIKit
final class ScanQRCodeWrapperVC: BaseVC {
var delegate: (UIViewController & QRScannerDelegate)? = nil
2019-11-19 06:18:29 +01:00
var isPresentedModally = false
private let message: String
private let scanQRCodeVC = QRCodeScanningViewController()
2019-11-19 04:35:38 +01:00
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait }
// MARK: - Lifecycle
init(message: String) {
self.message = message
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
preconditionFailure("Use init(message:) instead.")
}
override init(nibName: String?, bundle: Bundle?) {
preconditionFailure("Use init(message:) instead.")
}
2019-11-19 04:35:38 +01:00
override func viewDidLoad() {
2020-02-20 04:37:17 +01:00
super.viewDidLoad()
title = "Scan QR Code"
// Set up navigation bar if needed
2019-11-19 06:18:29 +01:00
if isPresentedModally {
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(close))
2019-11-19 06:18:29 +01:00
}
// Set up scan QR code VC
2019-11-19 04:35:38 +01:00
scanQRCodeVC.scanDelegate = delegate
let scanQRCodeVCView = scanQRCodeVC.view!
view.addSubview(scanQRCodeVCView)
scanQRCodeVCView.pin(.leading, to: .leading, of: view)
scanQRCodeVCView.pin(.trailing, to: .trailing, of: view)
2020-06-05 05:43:06 +02:00
scanQRCodeVCView.autoPinEdge(.top, to: .top, of: view)
2019-11-19 04:35:38 +01:00
scanQRCodeVCView.autoPinToSquareAspectRatio()
// Set up bottom view
2019-11-19 04:35:38 +01:00
let bottomView = UIView()
view.addSubview(bottomView)
bottomView.pin(.top, to: .bottom, of: scanQRCodeVCView)
bottomView.pin(.leading, to: .leading, of: view)
bottomView.pin(.trailing, to: .trailing, of: view)
bottomView.pin(.bottom, to: .bottom, of: view)
// Set up explanation label
2019-11-19 04:35:38 +01:00
let explanationLabel = UILabel()
explanationLabel.text = message
explanationLabel.themeTextColor = .textPrimary
explanationLabel.font = .systemFont(ofSize: Values.smallFontSize)
2019-11-19 04:35:38 +01:00
explanationLabel.textAlignment = .center
explanationLabel.lineBreakMode = .byWordWrapping
explanationLabel.numberOfLines = 0
2019-11-19 04:35:38 +01:00
bottomView.addSubview(explanationLabel)
explanationLabel.autoPinWidthToSuperview(withMargin: 32)
explanationLabel.autoPinHeightToSuperview(withMargin: 32)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
2019-11-19 04:35:38 +01:00
UIDevice.current.ows_setOrientation(.portrait)
self.scanQRCodeVC.startCapture()
2019-11-19 04:35:38 +01:00
}
2019-11-19 06:18:29 +01:00
// MARK: - Interaction
@objc private func close() {
2019-11-19 06:18:29 +01:00
presentingViewController?.dismiss(animated: true, completion: nil)
}
public func startCapture() {
self.scanQRCodeVC.startCapture()
}
2019-11-19 04:35:38 +01:00
}