HasilSurveyVC.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // HasilSurveyVC.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 12/04/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. class HasilSurveyVC: UIViewController {
  10. // Outlets
  11. @IBOutlet weak var hasilSurveyTV: UITableView!
  12. @IBOutlet weak var noDataLayout: UIView!
  13. @IBOutlet weak var progressLoadingLayout: UIView!
  14. @IBOutlet weak var progressLoading: UIActivityIndicatorView!
  15. // String Message
  16. private let alertNoConnectionTitle = "Informasi"
  17. private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet"
  18. private let alertNoConnectionActionYes = "OK"
  19. // Initialize
  20. private var presenter: HasilSurveyPresenter!
  21. private var dataList: [SurveyResult] = [SurveyResult]()
  22. // Request Params
  23. private var cursor: Int = 0
  24. private var prev: Int = 0
  25. private var jenis: String = "1"
  26. private var columns: String = ""
  27. private var search: String = ""
  28. // Const
  29. private let hasilSurveyCellId = "hasilSurveyCellId"
  30. // Others
  31. private var loading = false
  32. private var firstDataSetLoaded = false
  33. private var lastDataLoadedEmpty = false
  34. // View Lifecycles
  35. override var preferredStatusBarStyle: UIStatusBarStyle {
  36. return .lightContent
  37. }
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. hasilSurveyTV.delegate = self
  41. hasilSurveyTV.dataSource = self
  42. hasilSurveyTV.tableFooterView = UIView()
  43. presenter = HasilSurveyPresenter(siraniApiServices: SiraniApiServices())
  44. }
  45. override func viewWillAppear(_ animated: Bool) {
  46. presenter.attachView(view: self)
  47. }
  48. override func viewDidAppear(_ animated: Bool) {
  49. if !firstDataSetLoaded {
  50. loadData()
  51. firstDataSetLoaded = true
  52. }
  53. }
  54. override func viewDidDisappear(_ animated: Bool) {
  55. presenter.detachView()
  56. }
  57. // private functions
  58. private func loadData() {
  59. if !loading && !lastDataLoadedEmpty {
  60. if ConnectionHelper.isConnectedToNetwork() {
  61. presenter.loadData(jenis: jenis, columns: columns, cursor: cursor, prev: prev, search: search)
  62. } else {
  63. showNoConnection()
  64. }
  65. }
  66. }
  67. @IBAction func onBtnBackTap(_ sender: Any) {
  68. performSegue(withIdentifier: "unwindHasilSurveyToHomeSegueId", sender: self)
  69. }
  70. }
  71. // View Delegate Protocols
  72. extension HasilSurveyVC: HasilSurveyViewDelegate {
  73. func showNoConnection() {
  74. let alertNoConnection = UIAlertController(
  75. title: alertNoConnectionTitle,
  76. message: alertNoConnectionMsg,
  77. preferredStyle: .alert
  78. )
  79. alertNoConnection.addAction(
  80. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  81. )
  82. self.present(alertNoConnection, animated: true, completion: nil)
  83. }
  84. func showLoading() {
  85. loading = true
  86. progressLoading.startAnimating()
  87. progressLoadingLayout.show()
  88. }
  89. func hideLoading() {
  90. loading = false
  91. progressLoading.stopAnimating()
  92. progressLoadingLayout.hide()
  93. }
  94. func showNoDataLayout() {
  95. if cursor == 0 {
  96. noDataLayout.show()
  97. }
  98. }
  99. func hideNoDataLayout() {
  100. noDataLayout.hide()
  101. }
  102. func setDataList(dataList: [SurveyResult]) {
  103. if cursor == 0 {
  104. noDataLayout.hide()
  105. }
  106. if dataList.count > 0 {
  107. hasilSurveyTV.beginUpdates()
  108. for data in dataList {
  109. self.dataList.append(data)
  110. hasilSurveyTV.insertRows(
  111. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  112. with: .automatic
  113. )
  114. }
  115. hasilSurveyTV.endUpdates()
  116. let totalData = dataList.count
  117. cursor += totalData
  118. } else {
  119. lastDataLoadedEmpty = true
  120. }
  121. }
  122. }
  123. // UITableController Protocols
  124. extension HasilSurveyVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
  125. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  126. return dataList.count
  127. }
  128. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  129. if tableView == hasilSurveyTV {
  130. if let cell = tableView.dequeueReusableCell(withIdentifier: hasilSurveyCellId, for: indexPath) as? HasilSurveyCell {
  131. let surveyResult = dataList[indexPath.row]
  132. cell.updateViews(surveyResult: surveyResult)
  133. return cell
  134. } else {
  135. return HasilSurveyCell()
  136. }
  137. } else {
  138. return HasilSurveyCell()
  139. }
  140. }
  141. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  142. let threshold = 100
  143. let contentOffset = scrollView.contentOffset.y
  144. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  145. if !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  146. loadData()
  147. }
  148. }
  149. }