SertifikatSudahUploadVC.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. presenter = SertifikatSudahUploadPresenter(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 onBtnBackTap(_ 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 unwindToSertifikatSudahUpload(segue: UIStoryboardSegue) {}
  83. }
  84. // View Delegate Protocols
  85. extension SertifikatSudahUploadVC: SertifikatSudahUploadViewDelegate {
  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: [SertifikatSudahUpload]) {
  116. if cursor == 0 {
  117. noDataLayout.hide()
  118. }
  119. if dataList.count > 0 {
  120. for data in dataList {
  121. self.dataList.append(data)
  122. sertSdhUploadTV.beginUpdates()
  123. sertSdhUploadTV.insertRows(
  124. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  125. with: .none
  126. )
  127. sertSdhUploadTV.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 SertifikatSudahUploadVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, SertifikatSudahUploadCellDelegate {
  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 == sertSdhUploadTV {
  162. if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? SertifikatSudahUploadCell {
  163. let data = dataList[indexPath.row]
  164. cell.cellDelegate = self
  165. cell.btnDwnldPeringatan.tag = indexPath.row
  166. cell.btnDownloadQrCode.tag = indexPath.row
  167. cell.btnDownloadLabel.tag = indexPath.row
  168. cell.updateViews(sertSdhUpload: data)
  169. return cell
  170. } else {
  171. return SertifikatSudahUploadCell()
  172. }
  173. } else {
  174. return SertifikatSudahUploadCell()
  175. }
  176. }
  177. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  178. let threshold = -5
  179. let contentOffset = scrollView.contentOffset.y
  180. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  181. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  182. loadData()
  183. }
  184. }
  185. func download(urlWeb: String?) {
  186. if urlWeb != nil {
  187. var filename = "file.pdf"
  188. let values = urlWeb!.split(separator: "/")
  189. if values.count > 0 {
  190. filename = String(values[values.count-1])
  191. }
  192. self.downloadFile(urlWeb: urlWeb!, filename: filename)
  193. }
  194. }
  195. }
  196. // DocumentInteractionController
  197. extension SertifikatSudahUploadVC: UIDocumentInteractionControllerDelegate {
  198. func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
  199. guard let navVC = self.navigationController else {
  200. return self
  201. }
  202. return navVC
  203. }
  204. }
  205. // Search Delegate Protocols
  206. extension SertifikatSudahUploadVC: SearchSertifikatSudahUploadDelegate {
  207. func onSearch(search: String, parameter: String, jenis: String) {
  208. presenter.attachView(view: self)
  209. loadMore = false
  210. loading = false
  211. lastDataLoadedEmpty = false
  212. self.cursor = 0
  213. self.prev = 1
  214. self.search = search
  215. self.jenis = jenis
  216. self.columns = parameter
  217. dataList.removeAll()
  218. sertSdhUploadTV.reloadData()
  219. loadData()
  220. }
  221. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  222. if segue.identifier == searchSegueId {
  223. let destinationVC: SearchSertifikatSudahUploadVC = segue.destination as! SearchSertifikatSudahUploadVC
  224. destinationVC.delegate = self
  225. }
  226. }
  227. }