// // NotificationListPresenter.swift // Sirani // // Created by Anhar Solehudin on 11/11/20. // Copyright © 2020 Anhar Solehudin. All rights reserved. // import Foundation import SwiftyJSON class NotificationListPresenter { private let siraniApiServices: SiraniApiServices weak private var view: NotificationListViewDelegate? init(siraniApiServices: SiraniApiServices) { self.siraniApiServices = siraniApiServices } func attachView(view: NotificationListViewDelegate?) { self.view = view } func detachView() { self.view = nil } func loadData(cursor: Int, prev: Int) { 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() var dataList = [NotificationItem]() siraniApiServices.postToGetPushNotificationList(cursor: cursor, prev: prev, 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) { if let data = json["data"].array { if data.count > 0 { for item in data { let notificationItem = NotificationItem( log_activity_id: item["log_activity_id"].intValue, apl_id: item["apl_id"].intValue, activity_date: item["activity_date"].stringValue, judul_notifikasi: item["judul_notifikasi"].stringValue, note: item["note"].string ?? "", plg_id: item["plg_id"].intValue, cust_name: item["cust_name"].stringValue, eqp_name: item["eqp_name"].stringValue, models: item["models"].stringValue, fullname: item["fullname"].stringValue, is_read: item["is_read"].intValue ) dataList.append(notificationItem) } self.view?.setDataList(dataList: dataList) self.view?.hideLoading() } else { self.view?.hideLoading() } } else { self.view?.hideLoading() } } else { self.view?.hideLoading() } } else { self.view?.hideLoading() } } } else { print("user not login") } } }