AplikasiVC.swift 9.0 KB

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