AplikasiStatusVC.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // AplikasiStatusVC.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 21/04/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. class AplikasiStatusVC: UIViewController {
  10. // Outlets
  11. @IBOutlet weak var aplikasiStatusTV: 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 = "aplikasiStatusCellId"
  17. // Segue Id
  18. private let unwindToHomeSegueId = "unwindAplikasiStatusToHomeSegueId"
  19. private let searchSegueId = "searchAplikasiStatusSegueId"
  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: AplikasiStatusPresenter!
  26. private var dataList: [AplikasiStatus] = [AplikasiStatus]()
  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. aplikasiStatusTV.delegate = self
  44. aplikasiStatusTV.dataSource = self
  45. aplikasiStatusTV.separatorStyle = .none
  46. aplikasiStatusTV.rowHeight = UITableView.automaticDimension
  47. aplikasiStatusTV.estimatedRowHeight = 220
  48. presenter = AplikasiStatusPresenter(siraniApiServices: SiraniApiServices())
  49. presenter.attachView(view: self)
  50. loadData()
  51. }
  52. override func viewWillAppear(_ animated: Bool) {
  53. presenter.attachView(view: self)
  54. }
  55. override func viewDidDisappear(_ animated: Bool) {
  56. presenter.detachView()
  57. }
  58. // private functions
  59. private func loadData() {
  60. if !loading && !lastDataLoadedEmpty {
  61. if ConnectionHelper.isConnectedToNetwork() {
  62. presenter.loadData(cursor: cursor, prev: prev, jenis: jenis, columns: columns, search: search)
  63. } else {
  64. showNoConnection()
  65. }
  66. }
  67. }
  68. // Actions
  69. @IBAction func onBtnBackTap(_ sender: Any) {
  70. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  71. }
  72. @IBAction func onBtnSearchTap(_ sender: Any) {
  73. performSegue(withIdentifier: searchSegueId, sender: self)
  74. }
  75. @IBAction func unwindToAplikasiStatus(segue: UIStoryboardSegue) {}
  76. }
  77. // View Delegate Protocols
  78. extension AplikasiStatusVC: AplikasiStatusViewDelegate {
  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. progress.startAnimating()
  93. progressLayout.show()
  94. }
  95. func hideLoading() {
  96. loading = false
  97. progress.stopAnimating()
  98. progressLayout.hide()
  99. }
  100. func showNoDataLayout() {
  101. if cursor == 0 {
  102. noDataLayout.show()
  103. }
  104. }
  105. func hideNoDataLayout() {
  106. noDataLayout.hide()
  107. }
  108. func setDataList(dataList: [AplikasiStatus]) {
  109. if cursor == 0 {
  110. noDataLayout.hide()
  111. }
  112. if dataList.count > 0 {
  113. for data in dataList {
  114. self.dataList.append(data)
  115. aplikasiStatusTV.beginUpdates()
  116. aplikasiStatusTV.insertRows(
  117. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  118. with: .none
  119. )
  120. aplikasiStatusTV.endUpdates()
  121. }
  122. let totalData = dataList.count
  123. cursor += totalData
  124. loadMore = true
  125. } else {
  126. lastDataLoadedEmpty = true
  127. loadMore = false
  128. }
  129. }
  130. }
  131. // UITableController Protocols
  132. extension AplikasiStatusVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
  133. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  134. return dataList.count
  135. }
  136. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  137. if tableView == aplikasiStatusTV {
  138. if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? AplikasiStatusCell {
  139. let data = dataList[indexPath.row]
  140. cell.updateViews(aplikasiStatus: data)
  141. return cell
  142. } else {
  143. return AplikasiStatusCell()
  144. }
  145. } else {
  146. return AplikasiStatusCell()
  147. }
  148. }
  149. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  150. let threshold = -5
  151. let contentOffset = scrollView.contentOffset.y
  152. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  153. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  154. loadData()
  155. }
  156. }
  157. }
  158. // Search Delegate Protocols
  159. extension AplikasiStatusVC: SearchAplikasiStatusDelegate {
  160. func onSearch(search: String, parameter: String, jenis: String) {
  161. presenter.attachView(view: self)
  162. loadMore = false
  163. loading = false
  164. lastDataLoadedEmpty = false
  165. self.cursor = 0
  166. self.prev = 1
  167. self.search = search
  168. self.jenis = jenis
  169. self.columns = parameter
  170. dataList.removeAll()
  171. aplikasiStatusTV.reloadData()
  172. loadData()
  173. }
  174. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  175. if segue.identifier == searchSegueId {
  176. let destinationVC: SearchAplikasiStatusVC = segue.destination as! SearchAplikasiStatusVC
  177. destinationVC.delegate = self
  178. }
  179. }
  180. }