AplikasiVC.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. ConnectionHelper.checkIpAddress { [weak self] success in
  57. guard let self = self else { return }
  58. DispatchQueue.main.async {
  59. if success {
  60. self.presenter.loadData(status: self.status, limit: self.limit, page: self.page, search: self.search)
  61. } else {
  62. CommonHelper.showNotAllowConnection(in: self)
  63. }
  64. }
  65. }
  66. } else {
  67. showNoConnection()
  68. }
  69. }
  70. }
  71. private func downloadFile(urlWeb: String, filename: String) {
  72. if !loading {
  73. if ConnectionHelper.isConnectedToNetwork() {
  74. ConnectionHelper.checkIpAddress { [weak self] success in
  75. guard let self = self else { return }
  76. DispatchQueue.main.async {
  77. if success {
  78. self.presenter.downloadFile(urlWeb: urlWeb, filename: filename)
  79. } else {
  80. CommonHelper.showNotAllowConnection(in: self)
  81. }
  82. }
  83. }
  84. } else {
  85. showNoConnection()
  86. }
  87. }
  88. }
  89. // Actions
  90. @IBAction func onBtnBackTap(_ sender: Any) {
  91. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  92. }
  93. @IBAction func onBtnSearchTap(_ sender: Any) {
  94. performSegue(withIdentifier: searchSegueId, sender: self)
  95. }
  96. @IBAction func unwindToAplikasi(segue: UIStoryboardSegue) {}
  97. }
  98. // View Delegate Protocols
  99. extension AplikasiVC: AplikasiViewDelegate {
  100. func showNoConnection() {
  101. let alertNoConnection = UIAlertController(
  102. title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
  103. message: Constants.instance.ALERT_NO_CONNECTION_MSG,
  104. preferredStyle: .alert
  105. )
  106. alertNoConnection.addAction(
  107. UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, style: .default, handler: nil)
  108. )
  109. self.present(alertNoConnection, animated: true, completion: nil)
  110. }
  111. func showLoading() {
  112. loading = true
  113. progress.startAnimating()
  114. progressLayout.show()
  115. }
  116. func hideLoading() {
  117. loading = false
  118. progress.stopAnimating()
  119. progressLayout.hide()
  120. }
  121. func showNoDataLayout() {
  122. if page == 1 {
  123. noDataLayout.show()
  124. }
  125. }
  126. func hideNoDataLayout() {
  127. noDataLayout.hide()
  128. }
  129. func setDataList(dataList: [Aplikasi]) {
  130. if page == 1 {
  131. noDataLayout.hide()
  132. }
  133. if dataList.count > 0 {
  134. for data in dataList {
  135. self.dataList.append(data)
  136. aplikasiTV.beginUpdates()
  137. aplikasiTV.insertRows(
  138. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  139. with: .none
  140. )
  141. aplikasiTV.endUpdates()
  142. }
  143. page += 1
  144. loadMore = true
  145. } else {
  146. lastDataLoadedEmpty = true
  147. loadMore = false
  148. }
  149. }
  150. func onDownloadSuccess(urlFile: URL) {
  151. self.documentInteractorController = UIDocumentInteractionController()
  152. self.documentInteractorController.delegate = self
  153. self.documentInteractorController.url = urlFile
  154. self.documentInteractorController.presentPreview(animated: true)
  155. }
  156. func onDownloadFailed() {
  157. let alertNoConnection = UIAlertController(
  158. title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
  159. message: "Download Gagal",
  160. preferredStyle: .alert
  161. )
  162. alertNoConnection.addAction(
  163. UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, style: .default, handler: nil)
  164. )
  165. self.present(alertNoConnection, animated: true, completion: nil)
  166. }
  167. }
  168. // UITableController Protocols
  169. extension AplikasiVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, AplikasiCellDelegate {
  170. func openAplikasiDetail(data: Aplikasi?) {
  171. print(data?.apl_id)
  172. }
  173. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  174. return dataList.count
  175. }
  176. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  177. if tableView == aplikasiTV {
  178. if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? AplikasiCell {
  179. let data = dataList[indexPath.row]
  180. cell.cellDelegate = self
  181. // cell.btnDwnldPeringatan.tag = indexPath.row
  182. // cell.btnDownloadQrCode.tag = indexPath.row
  183. // cell.btnDownloadAplikasi.tag = indexPath.row
  184. cell.updateViews(aplData: data)
  185. return cell
  186. } else {
  187. return AplikasiCell()
  188. }
  189. } else {
  190. return AplikasiCell()
  191. }
  192. }
  193. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  194. let threshold = -5
  195. let contentOffset = scrollView.contentOffset.y
  196. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  197. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  198. loadData()
  199. }
  200. }
  201. // func download(urlWeb: String?) {
  202. // if urlWeb != nil {
  203. // var filename = "file.pdf"
  204. // let values = urlWeb!.split(separator: "/")
  205. // if values.count > 0 {
  206. // filename = String(values[values.count-1])
  207. // }
  208. //
  209. // self.downloadFile(urlWeb: urlWeb!, filename: filename)
  210. // }
  211. // }
  212. }
  213. // DocumentInteractionController
  214. extension AplikasiVC: UIDocumentInteractionControllerDelegate {
  215. func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
  216. guard let navVC = self.navigationController else {
  217. return self
  218. }
  219. return navVC
  220. }
  221. }
  222. // Search Delegate Protocols
  223. extension AplikasiVC: SearchAplikasiDelegate {
  224. func onSearch(search: String, status: String, jenisLabel: String) {
  225. presenter.attachView(view: self)
  226. loadMore = false
  227. loading = false
  228. lastDataLoadedEmpty = false
  229. self.limit = 5
  230. self.page = 1
  231. self.search = search
  232. self.status = status
  233. self.subTitleLabel.text = jenisLabel
  234. dataList.removeAll()
  235. aplikasiTV.reloadData()
  236. loadData()
  237. }
  238. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  239. if segue.identifier == searchSegueId {
  240. let destinationVC: SearchAplikasiVC = segue.destination as! SearchAplikasiVC
  241. destinationVC.delegate = self
  242. }
  243. }
  244. }