ScannerViewController.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // GaspolScannerViewController.swift
  3. // Gaspol
  4. //
  5. // Created by Qindi on 19/05/22.
  6. //
  7. import Foundation
  8. import UIKit
  9. import NotificationBannerSwift
  10. import nuSDKService
  11. public class ScannerViewController: UIViewController, QRScannerViewDelegate {
  12. var scannerView: QRScannerView = {
  13. let scannerView = QRScannerView()
  14. return scannerView
  15. }()
  16. var titleInfo: UILabel = {
  17. let titleInfo = UILabel()
  18. titleInfo.text = "Scan the QR Sign-In code on your web browser".localized()
  19. titleInfo.font = .boldSystemFont(ofSize: 16.0)
  20. titleInfo.numberOfLines = 0
  21. titleInfo.textAlignment = .center
  22. return titleInfo
  23. }()
  24. public override func viewDidDisappear(_ animated: Bool) {
  25. if scannerView.isRunning {
  26. scannerView.stopScanning()
  27. }
  28. self.navigationController?.navigationBar.topItem?.title = nil
  29. self.navigationController?.navigationBar.setNeedsLayout()
  30. }
  31. public override func viewDidLoad() {
  32. super.viewDidLoad()
  33. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16.0), NSAttributedString.Key.foregroundColor: UIColor.white]
  34. let navBarAppearance = UINavigationBarAppearance()
  35. navBarAppearance.configureWithOpaqueBackground()
  36. navBarAppearance.backgroundColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : UIColor.mainColor
  37. navBarAppearance.titleTextAttributes = attributes
  38. navigationController?.navigationBar.standardAppearance = navBarAppearance
  39. navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
  40. self.title = "Scan QR Code".localized()
  41. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel".localized(), style: .plain, target: self, action: #selector(cancel(sender:)))
  42. self.view.addSubview(scannerView)
  43. self.view.addSubview(titleInfo)
  44. scannerView.anchor(left: self.view.leftAnchor, right: self.view.rightAnchor, centerX: self.view.centerXAnchor, centerY: self.view.centerYAnchor, height: self.view.bounds.height / 2)
  45. titleInfo.anchor(left: self.view.leftAnchor, bottom: scannerView.topAnchor, right: self.view.rightAnchor, paddingBottom: 10.0, centerX: self.view.centerXAnchor)
  46. scannerView.delegate = self
  47. }
  48. @objc func cancel(sender: Any) {
  49. navigationController?.dismiss(animated: true, completion: nil)
  50. }
  51. public override func viewDidAppear(_ animated: Bool) {
  52. scannerView.startScanning()
  53. }
  54. func qrScanningDidFail() {
  55. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  56. imageView.tintColor = .white
  57. let banner = FloatingNotificationBanner(title: "Scanning Failed. Please try again".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  58. banner.show()
  59. cancel(sender: "")
  60. }
  61. func qrScanningSucceededWithCode(_ str: String?) {
  62. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  63. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  64. imageView.tintColor = .white
  65. let banner = FloatingNotificationBanner(title: "Check your connection".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  66. banner.show()
  67. } else {
  68. DispatchQueue.global().async {
  69. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getWebLoginQRCode(f_qrcode: str ?? "")) {
  70. if response.isOk() {
  71. DispatchQueue.main.async {
  72. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  73. imageView.tintColor = .white
  74. let banner = FloatingNotificationBanner(title: "Successfully Sign-In".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .success, colors: nil, iconPosition: .center)
  75. banner.show()
  76. }
  77. }
  78. } else {
  79. DispatchQueue.main.async {
  80. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  81. imageView.tintColor = .white
  82. let banner = FloatingNotificationBanner(title: "Unable to access servers".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  83. banner.show()
  84. }
  85. }
  86. }
  87. }
  88. cancel(sender: "")
  89. }
  90. func qrScanningDidStop() {
  91. //print("SCANNED STOP")
  92. }
  93. }