NotificationListPresenter.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // NotificationListPresenter.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 11/11/20.
  6. // Copyright © 2020 Anhar Solehudin. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftyJSON
  10. class NotificationListPresenter {
  11. private let siraniApiServices: SiraniApiServices
  12. weak private var view: NotificationListViewDelegate?
  13. init(siraniApiServices: SiraniApiServices) {
  14. self.siraniApiServices = siraniApiServices
  15. }
  16. func attachView(view: NotificationListViewDelegate?) {
  17. self.view = view
  18. }
  19. func detachView() {
  20. self.view = nil
  21. }
  22. func loadData(cursor: Int, prev: Int) {
  23. self.view?.showLoading()
  24. let isLogin = UserState.instance.isLoggedIn()
  25. if isLogin {
  26. let userId = UserState.instance.getUserId()
  27. let custId = UserState.instance.getCustId()
  28. let roleId = UserState.instance.getRoleId()
  29. var dataList = [NotificationItem]()
  30. siraniApiServices.postToGetPushNotificationList(cursor: cursor, prev: prev, user_id: userId, cust_id: custId, role_id: roleId).responseJSON { response in
  31. if response.error == nil {
  32. guard let data = response.data else { return }
  33. if let json = try? JSON(data: data) {
  34. if let data = json["data"].array {
  35. if data.count > 0 {
  36. for item in data {
  37. let notificationItem = NotificationItem(
  38. log_activity_id: item["log_activity_id"].intValue,
  39. apl_id: item["apl_id"].intValue,
  40. activity_date: item["activity_date"].stringValue,
  41. judul_notifikasi: item["judul_notifikasi"].stringValue,
  42. note: item["note"].string ?? "",
  43. plg_id: item["plg_id"].intValue,
  44. cust_name: item["cust_name"].stringValue,
  45. eqp_name: item["eqp_name"].stringValue,
  46. models: item["models"].stringValue,
  47. fullname: item["fullname"].stringValue,
  48. is_read: item["is_read"].intValue
  49. )
  50. dataList.append(notificationItem)
  51. }
  52. self.view?.setDataList(dataList: dataList)
  53. self.view?.hideLoading()
  54. } else {
  55. self.view?.hideLoading()
  56. }
  57. } else {
  58. self.view?.hideLoading()
  59. }
  60. } else {
  61. self.view?.hideLoading()
  62. }
  63. } else {
  64. self.view?.hideLoading()
  65. }
  66. }
  67. } else {
  68. print("user not login")
  69. }
  70. }
  71. }