SertifikatSudahUploadVC.swift 8.7 KB

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