LabelVC.swift 9.1 KB

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