AplikasiVC.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // AplikasiVC.swift
  3. // Sirani
  4. //
  5. // Created by Hendi on 21/06/23.
  6. // Copyright © 2023 PT. Mitra Sinerji Teknoindo. All rights reserved.
  7. //
  8. import UIKit
  9. class AplikasiVC: UIViewController {
  10. // Outlets
  11. @IBOutlet weak var aplikasiTV: UITableView!
  12. @IBOutlet weak var noDataLayout: UIView!
  13. @IBOutlet weak var progressLayout: UIView!
  14. @IBOutlet weak var progress: UIActivityIndicatorView!
  15. @IBOutlet weak var subTitleLabel: UILabel!
  16. // View Id
  17. private let cellId = "AplikasiCellId"
  18. // Segue Id
  19. private let unwindToHomeSegueId = "unwindAplikasiToHomeSegueId"
  20. private let searchSegueId = "searchAplikasiSegueId"
  21. // Initialize
  22. private var presenter: AplikasiPresenter!
  23. private var dataList: [Aplikasi] = [Aplikasi]()
  24. private var documentInteractorController: UIDocumentInteractionController!
  25. // Request Params
  26. private var limit: Int = 5
  27. private var page: Int = 1
  28. private var status: String = "apl_all"
  29. private var search: String = ""
  30. // Others
  31. private var loading = false
  32. private var lastDataLoadedEmpty = false
  33. private var loadMore = false
  34. // View Lifecycles
  35. override var preferredStatusBarStyle: UIStatusBarStyle {
  36. return .lightContent
  37. }
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. aplikasiTV.delegate = self
  41. aplikasiTV.dataSource = self
  42. presenter = AplikasiPresenter(siraniApiServices: SiraniApiServices())
  43. presenter.attachView(view: self)
  44. loadData()
  45. }
  46. override func viewWillAppear(_ animated: Bool) {
  47. presenter.attachView(view: self)
  48. }
  49. override func viewDidDisappear(_ animated: Bool) {
  50. presenter.detachView()
  51. }
  52. // private functions
  53. private func loadData() {
  54. if !loading && !lastDataLoadedEmpty {
  55. if ConnectionHelper.isConnectedToNetwork() {
  56. self.presenter.loadData(status: self.status, limit: self.limit, page: self.page, search: self.search)
  57. } else {
  58. showNoConnection()
  59. }
  60. }
  61. }
  62. private func downloadFile(urlWeb: String, filename: String) {
  63. if !loading {
  64. if ConnectionHelper.isConnectedToNetwork() {
  65. self.presenter.downloadFile(urlWeb: urlWeb, filename: filename)
  66. } else {
  67. showNoConnection()
  68. }
  69. }
  70. }
  71. // Actions
  72. @IBAction func onBtnBackTap(_ sender: Any) {
  73. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  74. }
  75. @IBAction func onBtnSearchTap(_ sender: Any) {
  76. performSegue(withIdentifier: searchSegueId, sender: self)
  77. }
  78. @IBAction func unwindToAplikasi(segue: UIStoryboardSegue) {}
  79. }
  80. // View Delegate Protocols
  81. extension AplikasiVC: AplikasiViewDelegate {
  82. func showNoConnection() {
  83. let alertNoConnection = UIAlertController(
  84. title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
  85. message: Constants.instance.ALERT_NO_CONNECTION_MSG,
  86. preferredStyle: .alert
  87. )
  88. alertNoConnection.addAction(
  89. UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, style: .default, handler: nil)
  90. )
  91. self.present(alertNoConnection, animated: true, completion: nil)
  92. }
  93. func showLoading() {
  94. loading = true
  95. progress.startAnimating()
  96. progressLayout.show()
  97. }
  98. func hideLoading() {
  99. loading = false
  100. progress.stopAnimating()
  101. progressLayout.hide()
  102. }
  103. func showNoDataLayout() {
  104. if page == 1 {
  105. noDataLayout.show()
  106. }
  107. }
  108. func hideNoDataLayout() {
  109. noDataLayout.hide()
  110. }
  111. func setDataList(dataList: [Aplikasi]) {
  112. if page == 1 {
  113. noDataLayout.hide()
  114. }
  115. if dataList.count > 0 {
  116. for data in dataList {
  117. self.dataList.append(data)
  118. aplikasiTV.beginUpdates()
  119. aplikasiTV.insertRows(
  120. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  121. with: .none
  122. )
  123. aplikasiTV.endUpdates()
  124. }
  125. page += 1
  126. loadMore = true
  127. } else {
  128. lastDataLoadedEmpty = true
  129. loadMore = false
  130. }
  131. }
  132. func onDownloadSuccess(urlFile: URL) {
  133. self.documentInteractorController = UIDocumentInteractionController()
  134. self.documentInteractorController.delegate = self
  135. self.documentInteractorController.url = urlFile
  136. self.documentInteractorController.presentPreview(animated: true)
  137. }
  138. func onDownloadFailed() {
  139. let alertNoConnection = UIAlertController(
  140. title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
  141. message: "Download Gagal",
  142. preferredStyle: .alert
  143. )
  144. alertNoConnection.addAction(
  145. UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, style: .default, handler: nil)
  146. )
  147. self.present(alertNoConnection, animated: true, completion: nil)
  148. }
  149. }
  150. // UITableController Protocols
  151. extension AplikasiVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, AplikasiCellDelegate {
  152. func openAplikasiDetail(data: Aplikasi?) {
  153. print(data?.apl_id)
  154. }
  155. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  156. return dataList.count
  157. }
  158. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  159. if tableView == aplikasiTV {
  160. if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? AplikasiCell {
  161. let data = dataList[indexPath.row]
  162. cell.cellDelegate = self
  163. // cell.btnDwnldPeringatan.tag = indexPath.row
  164. // cell.btnDownloadQrCode.tag = indexPath.row
  165. // cell.btnDownloadAplikasi.tag = indexPath.row
  166. cell.updateViews(aplData: data)
  167. return cell
  168. } else {
  169. return AplikasiCell()
  170. }
  171. } else {
  172. return AplikasiCell()
  173. }
  174. }
  175. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  176. let threshold = -5
  177. let contentOffset = scrollView.contentOffset.y
  178. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  179. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  180. loadData()
  181. }
  182. }
  183. // func download(urlWeb: String?) {
  184. // if urlWeb != nil {
  185. // var filename = "file.pdf"
  186. // let values = urlWeb!.split(separator: "/")
  187. // if values.count > 0 {
  188. // filename = String(values[values.count-1])
  189. // }
  190. //
  191. // self.downloadFile(urlWeb: urlWeb!, filename: filename)
  192. // }
  193. // }
  194. }
  195. // DocumentInteractionController
  196. extension AplikasiVC: 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 AplikasiVC: SearchAplikasiDelegate {
  206. func onSearch(search: String, status: String, jenisLabel: String) {
  207. presenter.attachView(view: self)
  208. loadMore = false
  209. loading = false
  210. lastDataLoadedEmpty = false
  211. self.limit = 5
  212. self.page = 1
  213. self.search = search
  214. self.status = status
  215. self.subTitleLabel.text = jenisLabel
  216. dataList.removeAll()
  217. aplikasiTV.reloadData()
  218. loadData()
  219. }
  220. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  221. if segue.identifier == searchSegueId {
  222. let destinationVC: SearchAplikasiVC = segue.destination as! SearchAplikasiVC
  223. destinationVC.delegate = self
  224. }
  225. }
  226. }