OdsSp2OtorasiVC.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // OdsSp2OtorasiVC.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 14/04/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. class OdsSp2OtorasiVC: UIViewController {
  10. // Outlets
  11. @IBOutlet weak var sp2OtorasiTV: 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 = "sp2OtorasiCellId"
  17. // Segue Id
  18. private let unwindToHomeSegueId = "unwindSp2OtorasiToHomeSegueId"
  19. private let searchSegueId = "searchOdsSp2OtorisasiSegueId"
  20. // String Message
  21. private let alertNoConnectionTitle = "Informasi"
  22. private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet"
  23. private let alertNoConnectionActionYes = "OK"
  24. // Initialize
  25. private var presenter: OdsSp2OtorasiPresenter!
  26. private var dataList: [OdsSp2Otorasi] = [OdsSp2Otorasi]()
  27. // Request Params
  28. private var cursor: Int = 0
  29. private var prev: Int = 1
  30. private var jenis: String = ""
  31. private var columns: String = ""
  32. private var search: String = ""
  33. // Others
  34. private var loading = false
  35. private var lastDataLoadedEmpty = false
  36. private var loadMore = false
  37. // View Lifecycles
  38. override var preferredStatusBarStyle: UIStatusBarStyle {
  39. return .lightContent
  40. }
  41. override func viewDidLoad() {
  42. super.viewDidLoad()
  43. sp2OtorasiTV.delegate = self
  44. sp2OtorasiTV.dataSource = self
  45. presenter = OdsSp2OtorasiPresenter(siraniApiServices: SiraniApiServices())
  46. presenter.attachView(view: self)
  47. loadData()
  48. }
  49. override func viewWillAppear(_ animated: Bool) {
  50. presenter.attachView(view: self)
  51. }
  52. override func viewDidDisappear(_ animated: Bool) {
  53. presenter.detachView()
  54. }
  55. // private functions
  56. private func loadData() {
  57. if !loading && !lastDataLoadedEmpty {
  58. if ConnectionHelper.isConnectedToNetwork() {
  59. presenter.loadData(cursor: cursor, prev: prev, jenis: jenis, columns: columns, search: search)
  60. } else {
  61. showNoConnection()
  62. }
  63. }
  64. }
  65. @IBAction func onBtnBackTapa(_ sender: Any) {
  66. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  67. }
  68. @IBAction func onBtnSearchTap(_ sender: Any) {
  69. performSegue(withIdentifier: searchSegueId, sender: self)
  70. }
  71. @IBAction func unwindToSp2Otorisasi(segue: UIStoryboardSegue) {}
  72. }
  73. // View Delegate Protocols
  74. extension OdsSp2OtorasiVC: OdsSp2OtorasiViewDelegate {
  75. func showNoConnection() {
  76. let alertNoConnection = UIAlertController(
  77. title: alertNoConnectionTitle,
  78. message: alertNoConnectionMsg,
  79. preferredStyle: .alert
  80. )
  81. alertNoConnection.addAction(
  82. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  83. )
  84. self.present(alertNoConnection, animated: true, completion: nil)
  85. }
  86. func showLoading() {
  87. loading = true
  88. progress.startAnimating()
  89. progressLayout.show()
  90. }
  91. func hideLoading() {
  92. loading = false
  93. progress.stopAnimating()
  94. progressLayout.hide()
  95. }
  96. func showNoDataLayout() {
  97. if cursor == 0 {
  98. noDataLayout.show()
  99. }
  100. }
  101. func hideNoDataLayout() {
  102. noDataLayout.hide()
  103. }
  104. func setDataList(dataList: [OdsSp2Otorasi]) {
  105. if cursor == 0 {
  106. noDataLayout.hide()
  107. }
  108. if dataList.count > 0 {
  109. for data in dataList {
  110. self.dataList.append(data)
  111. sp2OtorasiTV.beginUpdates()
  112. sp2OtorasiTV.insertRows(
  113. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  114. with: .none
  115. )
  116. sp2OtorasiTV.endUpdates()
  117. }
  118. let totalData = dataList.count
  119. cursor += totalData
  120. loadMore = true
  121. } else {
  122. lastDataLoadedEmpty = true
  123. loadMore = false
  124. }
  125. }
  126. }
  127. // UITableController Protocols
  128. extension OdsSp2OtorasiVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
  129. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  130. return dataList.count
  131. }
  132. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  133. if tableView == sp2OtorasiTV {
  134. if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? OdsSp2OtorasiCell {
  135. let data = dataList[indexPath.row]
  136. cell.updateViews(odsSp2Otorasi: data)
  137. return cell
  138. } else {
  139. return OdsSp2OtorasiCell()
  140. }
  141. } else {
  142. return OdsSp2OtorasiCell()
  143. }
  144. }
  145. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  146. let threshold = -5
  147. let contentOffset = scrollView.contentOffset.y
  148. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  149. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  150. loadData()
  151. }
  152. }
  153. }
  154. // Search Delegate Protocols
  155. extension OdsSp2OtorasiVC: SearchOdsSp2OtorisasiDelegate {
  156. func onSearch(search: String, parameter: String, jenis: String) {
  157. presenter.attachView(view: self)
  158. loadMore = false
  159. loading = false
  160. lastDataLoadedEmpty = false
  161. self.cursor = 0
  162. self.prev = 1
  163. self.search = search
  164. self.jenis = jenis
  165. self.columns = parameter
  166. dataList.removeAll()
  167. sp2OtorasiTV.reloadData()
  168. loadData()
  169. }
  170. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  171. if segue.identifier == searchSegueId {
  172. let destinationVC: SearchOdsSp2OtorisasiVC = segue.destination as! SearchOdsSp2OtorisasiVC
  173. destinationVC.delegate = self
  174. }
  175. }
  176. }