NotificationListVC.swift 6.6 KB

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