AplikasiPresenter.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // AplikasiPresenter.swift
  3. // Sirani
  4. //
  5. // Created by Hendi on 21/06/23.
  6. // Copyright © 2023 PT. Mitra Sinerji Teknoindo. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftyJSON
  10. class AplikasiPresenter {
  11. private let siraniApiServices: SiraniApiServices
  12. weak private var view: AplikasiViewDelegate?
  13. init(siraniApiServices: SiraniApiServices) {
  14. self.siraniApiServices = siraniApiServices
  15. }
  16. func attachView(view: AplikasiViewDelegate?) {
  17. self.view = view
  18. }
  19. func detachView() {
  20. self.view = nil
  21. }
  22. func loadData(status: String, limit: Int, page: Int, search: String) {
  23. self.view?.hideNoDataLayout()
  24. self.view?.showLoading()
  25. var label = [Aplikasi]()
  26. siraniApiServices.postToGetAplikasi(user_id: UserState.instance.getUserId(), cust_id: UserState.instance.getCustId(), role_id: UserState.instance.getRoleId(), status: status, limit: limit, page: page, 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 aplikasiData = Aplikasi(
  34. id: item["id"].stringValue,
  35. apl_id: item["apl_id"].intValue,
  36. cust_id: item["cust_id"].intValue,
  37. cust_name: item["cust_name"].stringValue,
  38. id_izin: item["id_izin"].stringValue,
  39. id_proyek: item["id_proyek"].stringValue,
  40. nib: item["nib"].stringValue,
  41. resi_no: item["resi_no"].stringValue,
  42. resi_date: item["resi_date"].stringValue,
  43. eqp_name: item["eqp_name"].stringValue,
  44. merk: item["merk"].stringValue,
  45. model: item["model"].stringValue,
  46. brand: item["brand"].stringValue,
  47. disposisi: item["disposisi"].stringValue,
  48. status: item["status"].stringValue,
  49. StatusStr: item["StatusStr"].stringValue,
  50. statusBgColor: item["StatusBgColor"].stringValue)
  51. label.append(aplikasiData)
  52. }
  53. self.setDataList(dataList: label)
  54. } else {
  55. if page > 1 {
  56. self.setDataList(dataList: label)
  57. } else {
  58. self.showNoData()
  59. }
  60. }
  61. } else {
  62. self.showNoData()
  63. }
  64. } else {
  65. self.showNoData()
  66. }
  67. } else {
  68. self.showNoData()
  69. }
  70. }
  71. }
  72. func downloadFile(urlWeb: String, filename: String) {
  73. self.view?.showLoading()
  74. siraniApiServices.downloadFile(urlWeb: urlWeb, filename: filename).responseData { response in
  75. if let urlFile = response.fileURL {
  76. self.onDownloadSuccess(urlFile: urlFile)
  77. } else {
  78. self.onDownloadFailed()
  79. }
  80. }
  81. }
  82. // other functions
  83. private func setDataList(dataList: [Aplikasi]) {
  84. self.view?.setDataList(dataList: dataList)
  85. DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
  86. self.view?.hideLoading()
  87. }
  88. }
  89. private func showNoData() {
  90. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  91. self.view?.showNoDataLayout()
  92. self.view?.hideLoading()
  93. }
  94. }
  95. private func onDownloadSuccess(urlFile: URL) {
  96. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  97. self.view?.onDownloadSuccess(urlFile: urlFile)
  98. self.view?.hideLoading()
  99. }
  100. }
  101. private func onDownloadFailed() {
  102. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  103. self.view?.onDownloadFailed()
  104. self.view?.hideLoading()
  105. }
  106. }
  107. }