ScannerViewController.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 login 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. }
  30. public override func viewDidLoad() {
  31. super.viewDidLoad()
  32. self.view.backgroundColor = .white
  33. self.title = "Scan QR Code".localized()
  34. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel".localized(), style: .plain, target: self, action: #selector(cancel(sender:)))
  35. self.view.addSubview(scannerView)
  36. self.view.addSubview(titleInfo)
  37. scannerView.anchor(left: self.view.leftAnchor, right: self.view.rightAnchor, centerX: self.view.centerXAnchor, centerY: self.view.centerYAnchor, height: self.view.bounds.height / 2)
  38. titleInfo.anchor(left: self.view.leftAnchor, bottom: scannerView.topAnchor, right: self.view.rightAnchor, paddingBottom: 10.0, centerX: self.view.centerXAnchor)
  39. scannerView.delegate = self
  40. }
  41. @objc func cancel(sender: Any) {
  42. navigationController?.dismiss(animated: true, completion: nil)
  43. }
  44. public override func viewDidAppear(_ animated: Bool) {
  45. scannerView.startScanning()
  46. }
  47. func qrScanningDidFail() {
  48. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  49. imageView.tintColor = .white
  50. 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)
  51. banner.show()
  52. cancel(sender: "")
  53. }
  54. func qrScanningSucceededWithCode(_ str: String?) {
  55. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  56. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  57. imageView.tintColor = .white
  58. 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)
  59. banner.show()
  60. } else {
  61. DispatchQueue.global().async {
  62. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getWebLoginQRCode(f_qrcode: str ?? "")) {
  63. if response.isOk() {
  64. DispatchQueue.main.async {
  65. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  66. imageView.tintColor = .white
  67. let banner = FloatingNotificationBanner(title: "Successfully Login".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)
  68. banner.show()
  69. }
  70. }
  71. } else {
  72. DispatchQueue.main.async {
  73. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  74. imageView.tintColor = .white
  75. 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)
  76. banner.show()
  77. }
  78. }
  79. }
  80. }
  81. cancel(sender: "")
  82. }
  83. func qrScanningDidStop() {
  84. print("SCANNED STOP")
  85. }
  86. }