| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- //
- // LabelPresenter.swift
- // Sirani
- //
- // Created by Hendi on 20/06/23.
- // Copyright © 2026 PT. Mitra Sinerji Teknoindo. All rights reserved.
- //
- import Foundation
- import SwiftyJSON
- class LabelPresenter {
-
- private let siraniApiServices: SiraniApiServices
- weak private var view: LabelViewDelegate?
-
- init(siraniApiServices: SiraniApiServices) {
- self.siraniApiServices = siraniApiServices
- }
-
- func attachView(view: LabelViewDelegate?) {
- self.view = view
- }
-
- func detachView() {
- self.view = nil
- }
-
- func loadData(status: String, limit: Int, page: Int, search: String) {
- self.view?.hideNoDataLayout()
- self.view?.showLoading()
-
- var label = [Label]()
- siraniApiServices.postToGetSertifikatPrivate(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
- if response.error == nil {
- guard let data = response.data else { return }
- if let json = try? JSON(data: data) {
- if let data = json["data"].array {
- if data.count > 0 {
- for item in data {
- let labelData = self.parseLabelData(from: item)
- label.append(labelData)
- }
-
- self.setDataList(dataList: label)
- } else {
- if page > 1 {
- self.setDataList(dataList: label)
- } else {
- self.showNoData()
- }
- }
- } else {
- self.showNoData()
- }
- } else {
- self.showNoData()
- }
- } else {
- self.showNoData()
- }
- }
- }
-
- func downloadFile(urlWeb: String, filename: String) {
- self.view?.showLoading()
-
- siraniApiServices.downloadFile(urlWeb: urlWeb, filename: filename).responseData { response in
- if let urlFile = response.fileURL {
- self.onDownloadSuccess(urlFile: urlFile)
- } else {
- self.onDownloadFailed()
- }
- }
-
- }
-
- // other functions
- private func setDataList(dataList: [Label]) {
- self.view?.setDataList(dataList: dataList)
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
- self.view?.hideLoading()
- }
- }
-
- private func showNoData() {
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
- self.view?.showNoDataLayout()
- self.view?.hideLoading()
- }
- }
-
- private func onDownloadSuccess(urlFile: URL) {
- self.view?.hideLoading()
- self.view?.onDownloadSuccess(urlFile: urlFile)
- }
-
- private func onDownloadFailed() {
- self.view?.hideLoading()
- self.view?.onDownloadFailed()
- }
-
- private func parseLabelData(from item: JSON) -> Label {
- var licDate = item["lic_date"].stringValue
- if licDate.isEmpty || licDate == "null" { licDate = item["tgl_terbit"].stringValue }
- if licDate.isEmpty || licDate == "null" { licDate = item["tgl_berlaku"].stringValue }
- if licDate.isEmpty || licDate == "null" { licDate = item["tanggal_berlaku"].stringValue }
- if licDate.isEmpty || licDate == "null" { licDate = item["tgl"].stringValue }
-
- var expDate = item["exp_date"].stringValue
- if expDate.isEmpty || expDate == "null" { expDate = item["tgl_berakhir"].stringValue }
- if expDate.isEmpty || expDate == "null" { expDate = item["tgl_exp"].stringValue }
- if expDate.isEmpty || expDate == "null" { expDate = item["tanggal_berakhir"].stringValue }
- if expDate.isEmpty || expDate == "null" { expDate = item["exp"].stringValue }
-
- let berlakuRaw = item["berlaku"].stringValue
- if (!berlakuRaw.isEmpty && berlakuRaw != "null") {
- if berlakuRaw.contains(" s/d ") {
- let parts = berlakuRaw.components(separatedBy: " s/d ")
- if licDate.isEmpty || licDate == "null" { licDate = parts[0] }
- if (expDate.isEmpty || expDate == "null") && parts.count > 1 { expDate = parts[1] }
- } else if berlakuRaw.contains(" - ") {
- let parts = berlakuRaw.components(separatedBy: " - ")
- if licDate.isEmpty || licDate == "null" { licDate = parts[0] }
- if (expDate.isEmpty || expDate == "null") && parts.count > 1 { expDate = parts[1] }
- }
- }
-
- return Label(
- nomor: item["nomor"].stringValue,
- cust_name: item["cust_name"].stringValue,
- eqp_name: item["eqp_name"].stringValue,
- lic_id: item["lic_id"].intValue,
- brand: item["brand"].stringValue,
- lic_date: licDate,
- lic_no: item["lic_no"].stringValue,
- exp_date: expDate,
- models: item["models"].stringValue,
- plg_id: item["plg_id"].intValue,
- apl_id: item["apl_id"].intValue,
- negara: item["negara"].stringValue,
- model: item["model"].stringValue,
- merk: item["merk"].stringValue,
- berlaku: item["berlaku"].stringValue,
- statusBerlaku: item["StatusBerlaku"].stringValue,
- statusBgColor: item["StatusBgColor"].stringValue
- )
- }
- }
|