DaftarSertifikatPresenter.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // DaftarSertifikatPresenter.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 30/03/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftyJSON
  10. class DaftarSertifikatPresenter {
  11. private let siraniApiServices: SiraniApiServices
  12. weak private var view: DaftarSertifikatViewDelegate?
  13. init(siraniApiServices: SiraniApiServices) {
  14. self.siraniApiServices = siraniApiServices
  15. }
  16. func attachView(view: DaftarSertifikatViewDelegate?) {
  17. self.view = view
  18. }
  19. func detachView() {
  20. self.view = nil
  21. }
  22. func loadCertificatesData(jenis: String, columns: String, cursor: Int, prev: Int, search: String) {
  23. self.view?.hideNoDataLayout()
  24. self.view?.showLoading()
  25. var certificates = [CertificateData]()
  26. siraniApiServices.postToGetListSertifikat(jenis: jenis, columns: columns, cursor: cursor, prev: prev, search: search).responseJSON { response in
  27. if response.error == nil {
  28. guard let data = response.data else { return }
  29. if let json = try? JSON(data: data) {
  30. if let data = json["data"].array {
  31. if data.count > 0 {
  32. for item in data {
  33. let certificateData = self.parseCertificateData(from: item)
  34. certificates.append(certificateData)
  35. }
  36. self.setCertificates(certificates: certificates)
  37. } else {
  38. if cursor > 0 {
  39. self.setCertificates(certificates: certificates)
  40. } else {
  41. self.showNoData()
  42. }
  43. }
  44. } else {
  45. self.showNoData()
  46. }
  47. } else {
  48. self.showNoData()
  49. }
  50. } else {
  51. self.showNoData()
  52. }
  53. }
  54. }
  55. // other functions
  56. private func setCertificates(certificates: [CertificateData]) {
  57. self.view?.setCertificates(certificates: certificates)
  58. DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
  59. self.view?.hideLoading()
  60. }
  61. }
  62. private func showNoData() {
  63. DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
  64. self.view?.showNoDataLayout()
  65. self.view?.hideLoading()
  66. }
  67. }
  68. private func parseCertificateData(from item: JSON) -> CertificateData {
  69. var licDate = item["lic_date"].stringValue
  70. if licDate.isEmpty || licDate == "null" { licDate = item["tgl_terbit"].stringValue }
  71. if licDate.isEmpty || licDate == "null" { licDate = item["tgl_berlaku"].stringValue }
  72. if licDate.isEmpty || licDate == "null" { licDate = item["tanggal_berlaku"].stringValue }
  73. if licDate.isEmpty || licDate == "null" { licDate = item["tgl"].stringValue }
  74. var expDate = item["exp_date"].stringValue
  75. if expDate.isEmpty || expDate == "null" { expDate = item["tgl_berakhir"].stringValue }
  76. if expDate.isEmpty || expDate == "null" { expDate = item["tgl_exp"].stringValue }
  77. if expDate.isEmpty || expDate == "null" { expDate = item["tanggal_berakhir"].stringValue }
  78. if expDate.isEmpty || expDate == "null" { expDate = item["exp"].stringValue }
  79. let berlakuRaw = item["berlaku"].stringValue
  80. if (!berlakuRaw.isEmpty && berlakuRaw != "null") {
  81. if berlakuRaw.contains(" s/d ") {
  82. let parts = berlakuRaw.components(separatedBy: " s/d ")
  83. if licDate.isEmpty || licDate == "null" { licDate = parts[0] }
  84. if (expDate.isEmpty || expDate == "null") && parts.count > 1 { expDate = parts[1] }
  85. } else if berlakuRaw.contains(" - ") {
  86. let parts = berlakuRaw.components(separatedBy: " - ")
  87. if licDate.isEmpty || licDate == "null" { licDate = parts[0] }
  88. if (expDate.isEmpty || expDate == "null") && parts.count > 1 { expDate = parts[1] }
  89. }
  90. }
  91. return CertificateData(
  92. nomor: item["nomor"].stringValue,
  93. cust_name: item["cust_name"].stringValue,
  94. eqp_name: item["eqp_name"].stringValue,
  95. lic_id: item["lic_id"].intValue,
  96. brand: item["brand"].stringValue,
  97. lic_date: licDate,
  98. lic_no: item["lic_no"].stringValue,
  99. exp_date: expDate,
  100. models: item["models"].stringValue,
  101. plg_id: item["plg_id"].intValue,
  102. apl_id: item["apl_id"].intValue,
  103. negara: item["negara"].stringValue,
  104. model: item["model"].stringValue,
  105. merk: item["merk"].stringValue,
  106. berlaku: item["berlaku"].stringValue,
  107. statusBerlaku: item["StatusBerlaku"].stringValue,
  108. statusBgColor: item["StatusBgColor"].stringValue
  109. )
  110. }
  111. }