QRScannerPresenter.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // QRScannerPresenter.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 10/04/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftyJSON
  10. class QRScannerPresenter {
  11. private let siraniApiServices: SiraniApiServices
  12. weak private var view: QRScannerViewDelegate?
  13. init(siraniApiServices: SiraniApiServices) {
  14. self.siraniApiServices = siraniApiServices
  15. }
  16. func attachView(view: QRScannerViewDelegate?) {
  17. self.view = view
  18. }
  19. func detachView() {
  20. self.view = nil
  21. }
  22. func loadData(code: String) {
  23. self.view?.showLoading()
  24. siraniApiServices.postToGetResultQrCode(code: code).responseJSON { response in
  25. if response.error == nil {
  26. guard let data = response.data else { return }
  27. if let json = try? JSON(data: data) {
  28. let result = ResultQrCode(
  29. lic_no: json["lic_no"].stringValue,
  30. lic_date: json["lic_date"].stringValue,
  31. cust_id: json["cust_id"].intValue,
  32. pmh_name: json["pmh_name"].stringValue,
  33. eqp_name: json["eqp_name"].stringValue,
  34. merk: json["merk"].stringValue,
  35. model: json["model"].stringValue,
  36. imei: json["imei"].stringValue,
  37. mfc_name: json["mfc_name"].stringValue,
  38. negara: json["negara"].stringValue,
  39. acuan: json["acuan"].stringValue,
  40. eqp_frek: json["eqp_frek"].stringValue,
  41. eqp_power: json["eqp_power"].stringValue
  42. )
  43. self.setResult(result: result)
  44. }
  45. } else {
  46. self.setEmptyResult()
  47. }
  48. }
  49. }
  50. // other functions
  51. private func setResult(result: ResultQrCode) {
  52. self.view?.setResulQrCode(resultQrCode: result)
  53. DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
  54. self.view?.hideLoading()
  55. }
  56. }
  57. private func setEmptyResult() {
  58. self.view?.hideLoading()
  59. DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
  60. self.view?.setEmptyQrCode()
  61. }
  62. }
  63. }