HomePresenter.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // HomePresenter.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 28/03/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftyJSON
  10. class HomePresenter {
  11. private let siraniApiServices: SiraniApiServices
  12. weak private var view: HomeViewDelegate?
  13. init(siraniApiServices: SiraniApiServices) {
  14. self.siraniApiServices = siraniApiServices
  15. }
  16. func attachView(view: HomeViewDelegate?) {
  17. self.view = view
  18. }
  19. func detachView() {
  20. self.view = nil
  21. }
  22. func loadSlideshowImages() {
  23. var images = [String]()
  24. siraniApiServices.postSlideShowImages(cursor: "1", prev: "0", search: "").responseJSON { response in
  25. if response.error == nil {
  26. guard let data = response.data else { return }
  27. if let json = try? JSON(data: data) {
  28. if let dataImages = json["data"].array {
  29. for item in dataImages {
  30. let path = item["path"].stringValue
  31. images.append(path)
  32. }
  33. self.view?.setSlideshowImages(images: images)
  34. } else {
  35. print("parse data error")
  36. }
  37. } else {
  38. print("parse data error")
  39. }
  40. } else {
  41. print(response.error ?? "Error")
  42. }
  43. }
  44. }
  45. func loadNews(cursor: Int, prev: Int, jenis: String, columns: String) {
  46. self.view?.showLoading()
  47. var listNews = [News]()
  48. siraniApiServices.postToGetNews(cursor: cursor, prev: prev, jenis: jenis, columns: columns, keyword: "").responseJSON { response in
  49. if response.error == nil {
  50. guard let data = response.data else { return }
  51. if let json = try? JSON(data: data) {
  52. if let data = json["data"].array {
  53. if data.count > 0 {
  54. for item in data {
  55. let news = News(
  56. id: item["id"].intValue,
  57. title_news: item["title_news"].stringValue,
  58. pra_news: item["pra_news"].stringValue,
  59. create_date: item["create_date"].stringValue,
  60. thumb_filename: item["thumb_filename"].stringValue
  61. )
  62. listNews.append(news)
  63. }
  64. self.view?.setNews(listNews: listNews)
  65. self.view?.hideLoading()
  66. } else {
  67. self.view?.hideLoading()
  68. }
  69. } else {
  70. self.view?.hideLoading()
  71. }
  72. } else {
  73. self.view?.hideLoading()
  74. }
  75. } else {
  76. self.view?.hideLoading()
  77. }
  78. }
  79. }
  80. func loadMenuNotifications() {
  81. self.view?.showLoading()
  82. let isLogin = UserState.instance.isLoggedIn()
  83. if isLogin {
  84. let userId = UserState.instance.getUserId()
  85. let custId = UserState.instance.getCustId()
  86. let roleId = UserState.instance.getRoleId()
  87. siraniApiServices.postToGetNotificationsTotal(user_id: userId, cust_id: custId, role_id: roleId).responseJSON { response in
  88. if response.error == nil {
  89. guard let data = response.data else { return }
  90. if let json = try? JSON(data: data) {
  91. let menuNotification = MenuNotification(
  92. cnt_sdocmasalah: json["cnt_sdocmasalah"].intValue ,
  93. cnt_sdocsp: json["cnt_sdocsp"].intValue ,
  94. cnt_sdocspmasalah: json["cnt_sdocspmasalah"].intValue ,
  95. tot_aplikasi_sdoc: json["tot_aplikasi_sdoc"].intValue ,
  96. cnt_aplikasimasalah: json["cnt_aplikasimasalah"].intValue ,
  97. cnt_aplikasiperbaikancust: json["cnt_aplikasiperbaikancust"].intValue ,
  98. tot_aplikasi: json["tot_aplikasi"].intValue ,
  99. cnt_sertsp: json["cnt_sertsp"].intValue ,
  100. tot_sp2sertifikat: json["tot_sp2sertifikat"].intValue ,
  101. cnt_sptigabaru: json["cnt_sptigabaru"].intValue ,
  102. tot_sp3: json["tot_sp3"].intValue ,
  103. cnt_rhu: json["cnt_rhu"].intValue ,
  104. tot_rhu: json["tot_rhu"].intValue ,
  105. cnt_sertifikattidakberlaku: json["cnt_sertifikattidakberlaku"].intValue ,
  106. cnt_ods_sp2baru: json["cnt_ods_sp2baru"].intValue ,
  107. cnt_ods_sp2otorisasi: json["cnt_ods_sp2otorisasi"].intValue ,
  108. cnt_ods_sp2before: json["cnt_ods_sp2before"].intValue ,
  109. cnt_ods_sp2after: json["cnt_ods_sp2after"].intValue ,
  110. tot_ods: json["tot_ods"].intValue ,
  111. cnt_sp2_sertifikat_daftar_sp2: json["cnt_sp2_sertifikat_daftar_sp2"].intValue ,
  112. tot_sp2_sertifikat: json["tot_sp2_sertifikat"].intValue ,
  113. cnt_sp2_tambahan_daftar_sp2: json["cnt_sp2_tambahan_daftar_sp2"].intValue ,
  114. tot_sp2_tambahan: json["tot_sp2_tambahan"].intValue ,
  115. cnt_sertifikatsementara: json["cnt_sertifikatsementara"].intValue ,
  116. cnt_sertifikatberlaku: json["cnt_sertifikatberlaku"].intValue ,
  117. tot_sertifikat: json["tot_sertifikat"].intValue
  118. )
  119. self.view?.setMenuNotifications(menuNotification: menuNotification)
  120. self.view?.hideLoading()
  121. } else {
  122. self.view?.hideLoading()
  123. }
  124. } else {
  125. self.view?.hideLoading()
  126. }
  127. }
  128. } else {
  129. print("user not login")
  130. }
  131. }
  132. func loadTotalUnreadNotif() {
  133. let isLogin = UserState.instance.isLoggedIn()
  134. if isLogin {
  135. let userId = UserState.instance.getUserId()
  136. let custId = UserState.instance.getCustId()
  137. let roleId = UserState.instance.getRoleId()
  138. siraniApiServices.postToGetTotalUnreadNotif(user_id: userId, cust_id: custId, role_id: roleId).responseJSON { response in
  139. if response.error == nil {
  140. guard let data = response.data else { return }
  141. if let json = try? JSON(data: data) {
  142. let totalUnreadNotif = json["jml_notif"].int ?? 0
  143. self.view?.setTotalUnreadNotif(totalUnreadNotif: totalUnreadNotif)
  144. }
  145. }
  146. }
  147. } else {
  148. print("user not login")
  149. }
  150. }
  151. }