Sp2TambahanVC.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // Sp2TambahanVC.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 27/04/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. class Sp2TambahanVC: UIViewController {
  10. // outlets
  11. @IBOutlet weak var sp2TambahanTV: 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 = "sp2TambahanCellId"
  17. // Segue Id
  18. private let unwindToHomeSegueId = "unwindSp2TambahanToHomeSegueId"
  19. private let searchSegueId = "searchSp2TambahanSegueId"
  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: Sp2TambahanPresenter!
  26. private var dataList: [Sp2Tambahan] = [Sp2Tambahan]()
  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. sp2TambahanTV.delegate = self
  44. sp2TambahanTV.dataSource = self
  45. presenter = Sp2TambahanPresenter(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. // Actions
  66. @IBAction func onBtnBackTap(_ sender: Any) {
  67. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  68. }
  69. @IBAction func onBtnSearchTap(_ sender: Any) {
  70. performSegue(withIdentifier: searchSegueId, sender: self)
  71. }
  72. @IBAction func unwindToSp2Tambahan(segue: UIStoryboardSegue) {}
  73. }
  74. // View Delegate Protocols
  75. extension Sp2TambahanVC: Sp2TambahanViewDelegate {
  76. func showNoConnection() {
  77. let alertNoConnection = UIAlertController(
  78. title: alertNoConnectionTitle,
  79. message: alertNoConnectionMsg,
  80. preferredStyle: .alert
  81. )
  82. alertNoConnection.addAction(
  83. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  84. )
  85. self.present(alertNoConnection, animated: true, completion: nil)
  86. }
  87. func showLoading() {
  88. loading = true
  89. progress.startAnimating()
  90. progressLayout.show()
  91. }
  92. func hideLoading() {
  93. loading = false
  94. progress.stopAnimating()
  95. progressLayout.hide()
  96. }
  97. func showNoDataLayout() {
  98. if cursor == 0 {
  99. noDataLayout.show()
  100. }
  101. }
  102. func hideNoDataLayout() {
  103. noDataLayout.hide()
  104. }
  105. func setDataList(dataList: [Sp2Tambahan]) {
  106. if cursor == 0 {
  107. noDataLayout.hide()
  108. }
  109. if dataList.count > 0 {
  110. for data in dataList {
  111. self.dataList.append(data)
  112. sp2TambahanTV.beginUpdates()
  113. sp2TambahanTV.insertRows(
  114. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  115. with: .none
  116. )
  117. sp2TambahanTV.endUpdates()
  118. }
  119. let totalData = dataList.count
  120. cursor += totalData
  121. loadMore = true
  122. } else {
  123. lastDataLoadedEmpty = true
  124. loadMore = false
  125. }
  126. }
  127. }
  128. // UITableController Protocols
  129. extension Sp2TambahanVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
  130. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  131. return dataList.count
  132. }
  133. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  134. if tableView == sp2TambahanTV {
  135. if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? Sp2TambahanCell {
  136. let data = dataList[indexPath.row]
  137. cell.updateViews(sp2Tambahan: data)
  138. return cell
  139. } else {
  140. return Sp2TambahanCell()
  141. }
  142. } else {
  143. return Sp2TambahanCell()
  144. }
  145. }
  146. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  147. let threshold = -5
  148. let contentOffset = scrollView.contentOffset.y
  149. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  150. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  151. loadData()
  152. }
  153. }
  154. }
  155. // Search Delegate Protocols
  156. extension Sp2TambahanVC: SearchSp2TambahanDelegate {
  157. func onSearch(search: String, parameter: String, jenis: String) {
  158. presenter.attachView(view: self)
  159. loadMore = false
  160. loading = false
  161. lastDataLoadedEmpty = false
  162. self.cursor = 0
  163. self.prev = 1
  164. self.search = search
  165. self.jenis = jenis
  166. self.columns = parameter
  167. dataList.removeAll()
  168. sp2TambahanTV.reloadData()
  169. loadData()
  170. }
  171. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  172. if segue.identifier == searchSegueId {
  173. let destinationVC: SearchSp2TambahanVC = segue.destination as! SearchSp2TambahanVC
  174. destinationVC.delegate = self
  175. }
  176. }
  177. }