LabelVC.swift 8.3 KB

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