// // HomePresenter.swift // Sirani // // Created by Anhar Solehudin on 28/03/19. // Copyright © 2019 Anhar Solehudin. All rights reserved. // import Foundation import SwiftyJSON class HomePresenter { private let siraniApiServices: SiraniApiServices weak private var view: HomeViewDelegate? init(siraniApiServices: SiraniApiServices) { self.siraniApiServices = siraniApiServices } func attachView(view: HomeViewDelegate?) { self.view = view } func detachView() { self.view = nil } func loadSlideshowImages() { var images = [String]() siraniApiServices.postSlideShowImages(cursor: "1", prev: "0", search: "").responseJSON { response in if response.error == nil { guard let data = response.data else { return } if let json = try? JSON(data: data) { if let dataImages = json["data"].array { for item in dataImages { let path = item["path"].stringValue images.append(path) } self.view?.setSlideshowImages(images: images) } else { print("parse data error") } } else { print("parse data error") } } else { print(response.error ?? "Error") } } } func loadNews(cursor: Int, prev: Int, jenis: String, columns: String) { self.view?.showLoading() var listNews = [News]() siraniApiServices.postToGetNews(cursor: cursor, prev: prev, jenis: jenis, columns: columns, keyword: "").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 news = News( id: item["id"].intValue, title_news: item["title_news"].stringValue, pra_news: item["pra_news"].stringValue, create_date: item["create_date"].stringValue ) listNews.append(news) } self.view?.setNews(listNews: listNews) self.view?.hideLoading() } else { self.view?.hideLoading() } } else { self.view?.hideLoading() } } else { self.view?.hideLoading() } } else { self.view?.hideLoading() } } } func loadMenuNotifications() { self.view?.showLoading() let isLogin = UserState.instance.isLoggedIn() if isLogin { let userId = UserState.instance.getUserId() let custId = UserState.instance.getCustId() let roleId = UserState.instance.getRoleId() siraniApiServices.postToGetNotificationsTotal(user_id: userId, cust_id: custId, role_id: roleId).responseJSON { response in if response.error == nil { guard let data = response.data else { return } if let json = try? JSON(data: data) { let menuNotification = MenuNotification( cnt_sdocmasalah: json["cnt_sdocmasalah"].intValue , cnt_sdocsp: json["cnt_sdocsp"].intValue , cnt_sdocspmasalah: json["cnt_sdocspmasalah"].intValue , tot_aplikasi_sdoc: json["tot_aplikasi_sdoc"].intValue , cnt_aplikasimasalah: json["cnt_aplikasimasalah"].intValue , cnt_aplikasiperbaikancust: json["cnt_aplikasiperbaikancust"].intValue , tot_aplikasi: json["tot_aplikasi"].intValue , cnt_sertsp: json["cnt_sertsp"].intValue , tot_sp2sertifikat: json["tot_sp2sertifikat"].intValue , cnt_sptigabaru: json["cnt_sptigabaru"].intValue , tot_sp3: json["tot_sp3"].intValue , cnt_rhu: json["cnt_rhu"].intValue , tot_rhu: json["tot_rhu"].intValue , cnt_sertifikattidakberlaku: json["cnt_sertifikattidakberlaku"].intValue , cnt_ods_sp2baru: json["cnt_ods_sp2baru"].intValue , cnt_ods_sp2otorisasi: json["cnt_ods_sp2otorisasi"].intValue , cnt_ods_sp2before: json["cnt_ods_sp2before"].intValue , cnt_ods_sp2after: json["cnt_ods_sp2after"].intValue , tot_ods: json["tot_ods"].intValue , cnt_sp2_sertifikat_daftar_sp2: json["cnt_sp2_sertifikat_daftar_sp2"].intValue , tot_sp2_sertifikat: json["tot_sp2_sertifikat"].intValue , cnt_sp2_tambahan_daftar_sp2: json["cnt_sp2_tambahan_daftar_sp2"].intValue , tot_sp2_tambahan: json["tot_sp2_tambahan"].intValue , cnt_sertifikatsementara: json["cnt_sertifikatsementara"].intValue , cnt_sertifikatberlaku: json["cnt_sertifikatberlaku"].intValue , tot_sertifikat: json["tot_sertifikat"].intValue ) self.view?.setMenuNotifications(menuNotification: menuNotification) self.view?.hideLoading() } else { self.view?.hideLoading() } } else { self.view?.hideLoading() } } } else { print("user not login") } } func loadTotalUnreadNotif() { let isLogin = UserState.instance.isLoggedIn() if isLogin { let userId = UserState.instance.getUserId() let custId = UserState.instance.getCustId() let roleId = UserState.instance.getRoleId() siraniApiServices.postToGetTotalUnreadNotif(user_id: userId, cust_id: custId, role_id: roleId).responseJSON { response in if response.error == nil { guard let data = response.data else { return } if let json = try? JSON(data: data) { let totalUnreadNotif = json["jml_notif"].int ?? 0 self.view?.setTotalUnreadNotif(totalUnreadNotif: totalUnreadNotif) } } } } else { print("user not login") } } }