LabelVC.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // LabelVC.swift
  3. // Sirani
  4. //
  5. // Created by Hendi on 20/06/23.
  6. // Copyright © 2026 PT. Mitra Sinerji Teknoindo. All rights reserved.
  7. //
  8. import UIKit
  9. class LabelVC: UIViewController {
  10. // Outlets
  11. @IBOutlet weak var labelTV: 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 = "LabelCellId"
  18. // Segue Id
  19. private let unwindToHomeSegueId = "unwindLabelToHomeSegueId"
  20. private let searchSegueId = "searchLabelSegueId"
  21. // Initialize
  22. private var presenter: LabelPresenter!
  23. private var dataList: [Label] = [Label]()
  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 = "label_all"
  29. private var search: String = ""
  30. private var kode: Int?
  31. private var isLabel: Bool = true
  32. // Others
  33. private var loading = false
  34. private var lastDataLoadedEmpty = false
  35. private var loadMore = false
  36. // View Lifecycles
  37. override var preferredStatusBarStyle: UIStatusBarStyle {
  38. return .lightContent
  39. }
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. labelTV.delegate = self
  43. labelTV.dataSource = self
  44. labelTV.rowHeight = UITableView.automaticDimension
  45. labelTV.estimatedRowHeight = 220
  46. presenter = LabelPresenter(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. self.presenter.loadData(status: self.status, limit: self.limit, page: self.page, search: self.search)
  61. } else {
  62. showNoConnection()
  63. }
  64. }
  65. }
  66. private func downloadFile(urlWeb: String, filename: String) {
  67. if !loading {
  68. if ConnectionHelper.isConnectedToNetwork() {
  69. self.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 unwindToLabel(segue: UIStoryboardSegue) {}
  83. }
  84. // View Delegate Protocols
  85. extension LabelVC: LabelViewDelegate {
  86. func showNoConnection() {
  87. let alertNoConnection = UIAlertController(
  88. title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
  89. message: Constants.instance.ALERT_NO_CONNECTION_MSG,
  90. preferredStyle: .alert
  91. )
  92. alertNoConnection.addAction(
  93. UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, 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 page == 1 {
  109. noDataLayout.show()
  110. }
  111. }
  112. func hideNoDataLayout() {
  113. noDataLayout.hide()
  114. }
  115. func setDataList(dataList: [Label]) {
  116. if page == 1 {
  117. noDataLayout.hide()
  118. }
  119. if dataList.count > 0 {
  120. for data in dataList {
  121. self.dataList.append(data)
  122. labelTV.beginUpdates()
  123. labelTV.insertRows(
  124. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  125. with: .none
  126. )
  127. labelTV.endUpdates()
  128. }
  129. page += 1
  130. loadMore = true
  131. } else {
  132. lastDataLoadedEmpty = true
  133. loadMore = false
  134. }
  135. }
  136. func onDownloadSuccess(urlFile: URL) {
  137. self.documentInteractorController = UIDocumentInteractionController()
  138. self.documentInteractorController.delegate = self
  139. self.documentInteractorController.url = urlFile
  140. self.documentInteractorController.presentPreview(animated: true)
  141. }
  142. func onDownloadFailed() {
  143. let alertNoConnection = UIAlertController(
  144. title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
  145. message: "Download Gagal",
  146. preferredStyle: .alert
  147. )
  148. alertNoConnection.addAction(
  149. UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, style: .default, handler: nil)
  150. )
  151. self.present(alertNoConnection, animated: true, completion: nil)
  152. }
  153. }
  154. // UITableController Protocols
  155. extension LabelVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, LabelCellDelegate {
  156. func openLabelDetail(data: Label?) {
  157. self.kode = data?.lic_id
  158. performSegue(withIdentifier: "labelDetailSegueId", sender: self)
  159. }
  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 == labelTV {
  165. if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? LabelCell {
  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(labelData: data)
  172. return cell
  173. } else {
  174. return LabelCell()
  175. }
  176. } else {
  177. return LabelCell()
  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. //
  196. // self.downloadFile(urlWeb: urlWeb!, filename: filename)
  197. // }
  198. // }
  199. }
  200. // DocumentInteractionController
  201. extension LabelVC: UIDocumentInteractionControllerDelegate {
  202. func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
  203. guard let navVC = self.navigationController else {
  204. return self
  205. }
  206. return navVC
  207. }
  208. }
  209. // Search Delegate Protocols
  210. extension LabelVC: SearchLabelDelegate {
  211. func onSearch(search: String, status: String, jenisLabel: String) {
  212. presenter.attachView(view: self)
  213. loadMore = false
  214. loading = false
  215. lastDataLoadedEmpty = false
  216. self.limit = 5
  217. self.page = 1
  218. self.search = search
  219. self.status = status
  220. self.subTitleLabel.text = jenisLabel
  221. dataList.removeAll()
  222. labelTV.reloadData()
  223. loadData()
  224. }
  225. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  226. if segue.identifier == searchSegueId {
  227. let destinationVC: SearchLabelVC = segue.destination as! SearchLabelVC
  228. destinationVC.delegate = self
  229. }
  230. if let detailLabelVC = segue.destination as? QRScannerVC {
  231. detailLabelVC.kode = kode
  232. detailLabelVC.isLabel = isLabel
  233. }
  234. }
  235. }