OdsDaftarRhuVC.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // OdsDaftarRhuVC.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 20/04/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. class OdsDaftarRhuVC: UIViewController {
  10. // Outlets
  11. @IBOutlet weak var daftarRhuTV: 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 = "odsDaftarRhuCellId"
  17. // Segue Id
  18. private let unwindToHomeSegueId = "unwindDaftarRhuToHomeSegueId"
  19. private let searchSegueId = "searchOdsDaftarRhuSegueId"
  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: OdsDaftarRhuPresenter!
  26. private var dataList: [OdsDaftarRhu] = [OdsDaftarRhu]()
  27. private var documentInteractorController: UIDocumentInteractionController!
  28. // Request Params
  29. private var cursor: Int = 0
  30. private var prev: Int = 1
  31. private var jenis: String = ""
  32. private var columns: String = ""
  33. private var search: String = ""
  34. // Others
  35. private var loading = false
  36. private var lastDataLoadedEmpty = false
  37. private var loadMore = false
  38. // View Lifecycles
  39. override var preferredStatusBarStyle: UIStatusBarStyle {
  40. return .lightContent
  41. }
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. daftarRhuTV.delegate = self
  45. daftarRhuTV.dataSource = self
  46. presenter = OdsDaftarRhuPresenter(siraniApiServices: SiraniApiServices())
  47. presenter.attachView(view: self)
  48. loadData()
  49. }
  50. override func viewWillAppear(_ animated: Bool) {
  51. presenter.attachView(view: self)
  52. }
  53. override func viewDidDisappear(_ animated: Bool) {
  54. presenter.detachView()
  55. }
  56. // private functions
  57. private func loadData() {
  58. if !loading && !lastDataLoadedEmpty {
  59. if ConnectionHelper.isConnectedToNetwork() {
  60. presenter.loadData(cursor: cursor, prev: prev, jenis: jenis, columns: columns, search: search)
  61. } else {
  62. showNoConnection()
  63. }
  64. }
  65. }
  66. private func downloadFile(urlWeb: String, filename: String) {
  67. if !loading {
  68. if ConnectionHelper.isConnectedToNetwork() {
  69. presenter.downloadFile(urlWeb: urlWeb, filename: filename)
  70. } else {
  71. showNoConnection()
  72. }
  73. }
  74. }
  75. // Actions
  76. @IBAction func onBtnBacksTap(_ sender: Any) {
  77. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  78. }
  79. @IBAction func onBtnSearchTap(_ sender: Any) {
  80. performSegue(withIdentifier: searchSegueId, sender: self)
  81. }
  82. @IBAction func unwindToOdsDaftarRhu(segue: UIStoryboardSegue) {}
  83. }
  84. // View Delegate Protocols
  85. extension OdsDaftarRhuVC: OdsDaftarRhuViewDelegate {
  86. func showNoConnection() {
  87. let alertNoConnection = UIAlertController(
  88. title: alertNoConnectionTitle,
  89. message: alertNoConnectionMsg,
  90. preferredStyle: .alert
  91. )
  92. alertNoConnection.addAction(
  93. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  94. )
  95. self.present(alertNoConnection, animated: true, completion: nil)
  96. }
  97. func showLoading() {
  98. loading = true
  99. progress.startAnimating()
  100. progressLayout.show()
  101. }
  102. func hideLoading() {
  103. loading = false
  104. progress.stopAnimating()
  105. progressLayout.hide()
  106. }
  107. func showNoDataLayout() {
  108. if cursor == 0 {
  109. noDataLayout.show()
  110. }
  111. }
  112. func hideNoDataLayout() {
  113. noDataLayout.hide()
  114. }
  115. func setDataList(dataList: [OdsDaftarRhu]) {
  116. if cursor == 0 {
  117. noDataLayout.hide()
  118. }
  119. if dataList.count > 0 {
  120. for data in dataList {
  121. self.dataList.append(data)
  122. daftarRhuTV.beginUpdates()
  123. daftarRhuTV.insertRows(
  124. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  125. with: .none
  126. )
  127. daftarRhuTV.endUpdates()
  128. }
  129. let totalData = dataList.count
  130. cursor += totalData
  131. loadMore = true
  132. } else {
  133. lastDataLoadedEmpty = true
  134. loadMore = false
  135. }
  136. }
  137. func onDownloadSuccess(urlFile: URL) {
  138. self.documentInteractorController = UIDocumentInteractionController()
  139. self.documentInteractorController.delegate = self
  140. self.documentInteractorController.url = urlFile
  141. self.documentInteractorController.presentPreview(animated: true)
  142. }
  143. func onDownloadFailed() {
  144. let alertNoConnection = UIAlertController(
  145. title: alertNoConnectionTitle,
  146. message: "Download Gagal",
  147. preferredStyle: .alert
  148. )
  149. alertNoConnection.addAction(
  150. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  151. )
  152. self.present(alertNoConnection, animated: true, completion: nil)
  153. }
  154. }
  155. // UITableController Protocols
  156. extension OdsDaftarRhuVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, OdsDaftarRhuCellDelegate {
  157. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  158. return dataList.count
  159. }
  160. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  161. if tableView == daftarRhuTV {
  162. if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? OdsDaftarRhuCell {
  163. let data = dataList[indexPath.row]
  164. cell.cellDelegate = self
  165. cell.btnDwnldLhu.tag = indexPath.row
  166. cell.btnDwnlodEmc.tag = indexPath.row
  167. cell.updateViews(odsDaftarRhu: data)
  168. return cell
  169. } else {
  170. return OdsDaftarRhuCell()
  171. }
  172. } else {
  173. return OdsDaftarRhuCell()
  174. }
  175. }
  176. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  177. let threshold = -5
  178. let contentOffset = scrollView.contentOffset.y
  179. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  180. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  181. loadData()
  182. }
  183. }
  184. func download(urlWeb: String?) {
  185. if urlWeb != nil {
  186. var filename = "file.pdf"
  187. let values = urlWeb!.split(separator: "/")
  188. if values.count > 0 {
  189. filename = String(values[values.count-1])
  190. }
  191. self.downloadFile(urlWeb: urlWeb!, filename: filename)
  192. }
  193. }
  194. }
  195. // DocumentInteractionController
  196. extension OdsDaftarRhuVC: UIDocumentInteractionControllerDelegate {
  197. func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
  198. guard let navVC = self.navigationController else {
  199. return self
  200. }
  201. return navVC
  202. }
  203. }
  204. // Search Delegate Protocols
  205. extension OdsDaftarRhuVC: SearchOdsDaftarRhuDelegate {
  206. func onSearch(search: String, parameter: String, jenis: String) {
  207. presenter.attachView(view: self)
  208. loadMore = false
  209. loading = false
  210. lastDataLoadedEmpty = false
  211. self.cursor = 0
  212. self.prev = 1
  213. self.search = search
  214. self.jenis = jenis
  215. self.columns = parameter
  216. dataList.removeAll()
  217. daftarRhuTV.reloadData()
  218. loadData()
  219. }
  220. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  221. if segue.identifier == searchSegueId {
  222. let destinationVC: SearchOdsDaftarRhuVC = segue.destination as! SearchOdsDaftarRhuVC
  223. destinationVC.delegate = self
  224. }
  225. }
  226. }