| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- //
- // AplikasiPresenter.swift
- // Sirani
- //
- // Created by Hendi on 21/06/23.
- // Copyright © 2026 PT. Mitra Sinerji Teknoindo. All rights reserved.
- //
- import Foundation
- import SwiftyJSON
- class AplikasiPresenter {
-
- private let siraniApiServices: SiraniApiServices
- weak private var view: AplikasiViewDelegate?
-
- init(siraniApiServices: SiraniApiServices) {
- self.siraniApiServices = siraniApiServices
- }
-
- func attachView(view: AplikasiViewDelegate?) {
- 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 = [Aplikasi]()
- 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
- 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 aplikasiData = Aplikasi(
- id: item["id"].stringValue,
- apl_id: item["apl_id"].intValue,
- cust_id: item["cust_id"].intValue,
- cust_name: item["cust_name"].stringValue,
- id_izin: item["id_izin"].stringValue,
- id_proyek: item["id_proyek"].stringValue,
- nib: item["nib"].stringValue,
- resi_no: item["resi_no"].stringValue,
- resi_date: item["resi_date"].stringValue,
- eqp_name: item["eqp_name"].stringValue,
- merk: item["merk"].stringValue,
- model: item["model"].stringValue,
- brand: item["brand"].stringValue,
- disposisi: item["disposisi"].stringValue,
- status: item["status"].stringValue,
- StatusStr: item["StatusStr"].stringValue,
- statusBgColor: item["StatusBgColor"].stringValue)
-
- label.append(aplikasiData)
- }
-
- 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: [Aplikasi]) {
- self.view?.setDataList(dataList: dataList)
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
- self.view?.hideLoading()
- }
- }
-
- private func showNoData() {
- DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
- self.view?.showNoDataLayout()
- self.view?.hideLoading()
- }
- }
-
- private func onDownloadSuccess(urlFile: URL) {
- DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
- self.view?.onDownloadSuccess(urlFile: urlFile)
- self.view?.hideLoading()
- }
- }
-
- private func onDownloadFailed() {
- DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
- self.view?.onDownloadFailed()
- self.view?.hideLoading()
- }
- }
- }
|