NotificationListVC.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. presenter.loadData(cursor: cursor, prev: prev)
  69. } else {
  70. showNoConnection()
  71. }
  72. }
  73. }
  74. }
  75. // View Delegate Protocols
  76. extension NotificationListVC: NotificationListViewDelegate {
  77. func showNoConnection() {
  78. let alertNoConnection = UIAlertController(
  79. title: alertNoConnectionTitle,
  80. message: alertNoConnectionMsg,
  81. preferredStyle: .alert
  82. )
  83. alertNoConnection.addAction(
  84. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  85. )
  86. self.present(alertNoConnection, animated: true, completion: nil)
  87. }
  88. func showLoading() {
  89. loading = true
  90. progress.startAnimating()
  91. progressLayout.show()
  92. }
  93. func hideLoading() {
  94. loading = false
  95. progress.stopAnimating()
  96. progressLayout.hide()
  97. }
  98. func showNoDataLayout() {
  99. if cursor == 0 {
  100. noDataLayout.show()
  101. }
  102. }
  103. func hideNoDataLayout() {
  104. noDataLayout.hide()
  105. }
  106. func setDataList(dataList: [NotificationItem]) {
  107. if cursor == 0 {
  108. noDataLayout.hide()
  109. }
  110. if dataList.count > 0 {
  111. for data in dataList {
  112. self.dataList.append(data)
  113. notifListTV.beginUpdates()
  114. notifListTV.insertRows(
  115. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  116. with: .none
  117. )
  118. notifListTV.endUpdates()
  119. }
  120. let totalData = dataList.count
  121. cursor += totalData
  122. loadMore = true
  123. } else {
  124. lastDataLoadedEmpty = true
  125. loadMore = false
  126. }
  127. }
  128. }
  129. // UITableController Protocols
  130. extension NotificationListVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, NotificationCellDelegate {
  131. func openNotifDetail(notificationItem: NotificationItem?, position: Int?) {
  132. if notificationItem != nil {
  133. selectedNotifId = notificationItem!.log_activity_id
  134. if position != nil {
  135. self.dataList[position!].updateIsRead(isRead: 1)
  136. notifListTV.beginUpdates()
  137. notifListTV.reloadRows(
  138. at: [IndexPath(row: position!, section: 0)],
  139. with: .none
  140. )
  141. notifListTV.endUpdates()
  142. }
  143. performSegue(withIdentifier: "NotificationDetailSegueId", sender: self)
  144. }
  145. }
  146. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  147. return dataList.count
  148. }
  149. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  150. if tableView == notifListTV {
  151. if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? NotificationCell {
  152. let data = dataList[indexPath.row]
  153. cell.cellDelegate = self
  154. cell.card.tag = indexPath.row
  155. cell.updateViews(notificationItem: data, position: indexPath.row)
  156. return cell
  157. } else {
  158. return NotificationCell()
  159. }
  160. } else {
  161. return NotificationCell()
  162. }
  163. }
  164. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  165. let threshold = -5
  166. let contentOffset = scrollView.contentOffset.y
  167. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  168. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  169. loadData()
  170. }
  171. }
  172. }