NotificationListVC.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // NotificationListVC.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 10/11/20.
  6. // Copyright © 2020 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. class NotificationListVC: UIViewController {
  10. @IBOutlet weak var notifListTV: UITableView!
  11. @IBOutlet weak var noDataLayout: UIView!
  12. @IBOutlet weak var progressLayout: UIView!
  13. @IBOutlet weak var progress: UIActivityIndicatorView!
  14. // View Id
  15. private let cellId = "notificationCellId"
  16. // String Message
  17. private let alertNoConnectionTitle = "Informasi"
  18. private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet"
  19. private let alertNoConnectionActionYes = "OK"
  20. // Initialize
  21. private var presenter: NotificationListPresenter!
  22. private var dataList: [NotificationItem] = [NotificationItem]()
  23. // Request Params
  24. private var cursor: Int = 0
  25. private var prev: Int = 1
  26. // Others
  27. private var loading = false
  28. private var lastDataLoadedEmpty = false
  29. private var loadMore = false
  30. private var selectedNotifId: Int = 0
  31. // View Lifecycles
  32. override var preferredStatusBarStyle: UIStatusBarStyle {
  33. return .lightContent
  34. }
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. notifListTV.delegate = self
  38. notifListTV.dataSource = self
  39. presenter = NotificationListPresenter(siraniApiServices: SiraniApiServices())
  40. presenter.attachView(view: self)
  41. loadData()
  42. }
  43. override func viewWillAppear(_ animated: Bool) {
  44. AppUtility.lockOrientation(.portrait)
  45. presenter.attachView(view: self)
  46. }
  47. override func viewDidAppear(_ animated: Bool) {
  48. }
  49. override func viewWillDisappear(_ animated: Bool) {
  50. AppUtility.lockOrientation(.all)
  51. presenter.detachView()
  52. }
  53. override func viewDidDisappear(_ animated: Bool) {
  54. }
  55. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  56. if let detailsVC = segue.destination as? NotificationDetailVC {
  57. detailsVC.notifId = selectedNotifId
  58. }
  59. }
  60. @IBAction func unwindToNotificationList(segue: UIStoryboardSegue) {}
  61. @IBAction func onBackTapped(_ sender: Any) {
  62. performSegue(withIdentifier: "unwindNotificationListToHome", sender: self)
  63. }
  64. // private functions
  65. private func loadData() {
  66. if !loading && !lastDataLoadedEmpty {
  67. if ConnectionHelper.isConnectedToNetwork() {
  68. if ConnectionHelper.isConnectedToNetwork() {
  69. ConnectionHelper.checkIpAddress { [weak self] success in
  70. guard let self = self else { return }
  71. DispatchQueue.main.async {
  72. if success {
  73. self.presenter.loadData(cursor: self.cursor, prev: self.prev)
  74. } else {
  75. CommonHelper.showNotAllowConnection(in: self)
  76. }
  77. }
  78. }
  79. }
  80. } else {
  81. showNoConnection()
  82. }
  83. }
  84. }
  85. }
  86. // View Delegate Protocols
  87. extension NotificationListVC: NotificationListViewDelegate {
  88. func showNoConnection() {
  89. let alertNoConnection = UIAlertController(
  90. title: alertNoConnectionTitle,
  91. message: alertNoConnectionMsg,
  92. preferredStyle: .alert
  93. )
  94. alertNoConnection.addAction(
  95. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  96. )
  97. self.present(alertNoConnection, animated: true, completion: nil)
  98. }
  99. func showLoading() {
  100. loading = true
  101. progress.startAnimating()
  102. progressLayout.show()
  103. }
  104. func hideLoading() {
  105. loading = false
  106. progress.stopAnimating()
  107. progressLayout.hide()
  108. }
  109. func showNoDataLayout() {
  110. if cursor == 0 {
  111. noDataLayout.show()
  112. }
  113. }
  114. func hideNoDataLayout() {
  115. noDataLayout.hide()
  116. }
  117. func setDataList(dataList: [NotificationItem]) {
  118. if cursor == 0 {
  119. noDataLayout.hide()
  120. }
  121. if dataList.count > 0 {
  122. for data in dataList {
  123. self.dataList.append(data)
  124. notifListTV.beginUpdates()
  125. notifListTV.insertRows(
  126. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  127. with: .none
  128. )
  129. notifListTV.endUpdates()
  130. }
  131. let totalData = dataList.count
  132. cursor += totalData
  133. loadMore = true
  134. } else {
  135. lastDataLoadedEmpty = true
  136. loadMore = false
  137. }
  138. }
  139. }
  140. // UITableController Protocols
  141. extension NotificationListVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, NotificationCellDelegate {
  142. func openNotifDetail(notificationItem: NotificationItem?, position: Int?) {
  143. if notificationItem != nil {
  144. selectedNotifId = notificationItem!.log_activity_id
  145. if position != nil {
  146. self.dataList[position!].updateIsRead(isRead: 1)
  147. notifListTV.beginUpdates()
  148. notifListTV.reloadRows(
  149. at: [IndexPath(row: position!, section: 0)],
  150. with: .none
  151. )
  152. notifListTV.endUpdates()
  153. }
  154. performSegue(withIdentifier: "NotificationDetailSegueId", sender: self)
  155. }
  156. }
  157. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  158. return dataList.count
  159. }
  160. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  161. if tableView == notifListTV {
  162. if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? NotificationCell {
  163. let data = dataList[indexPath.row]
  164. cell.cellDelegate = self
  165. cell.card.tag = indexPath.row
  166. cell.updateViews(notificationItem: data, position: indexPath.row)
  167. return cell
  168. } else {
  169. return NotificationCell()
  170. }
  171. } else {
  172. return NotificationCell()
  173. }
  174. }
  175. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  176. let threshold = -5
  177. let contentOffset = scrollView.contentOffset.y
  178. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  179. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  180. loadData()
  181. }
  182. }
  183. }