| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- //
- // LabelVC.swift
- // Sirani
- //
- // Created by Hendi on 20/06/23.
- // Copyright © 2026 PT. Mitra Sinerji Teknoindo. All rights reserved.
- //
- import UIKit
- class LabelVC: UIViewController {
-
- // Outlets
- @IBOutlet weak var labelTV: UITableView!
- @IBOutlet weak var noDataLayout: UIView!
- @IBOutlet weak var progressLayout: UIView!
- @IBOutlet weak var progress: UIActivityIndicatorView!
- @IBOutlet weak var subTitleLabel: UILabel!
-
- // View Id
- private let cellId = "LabelCellId"
-
- // Segue Id
- private let unwindToHomeSegueId = "unwindLabelToHomeSegueId"
- private let searchSegueId = "searchLabelSegueId"
-
- // Initialize
- private var presenter: LabelPresenter!
- private var dataList: [Label] = [Label]()
- private var documentInteractorController: UIDocumentInteractionController!
-
- // Request Params
- private var limit: Int = 5
- private var page: Int = 1
- private var status: String = "label_all"
- private var search: String = ""
- private var kode: Int?
- private var isLabel: Bool = true
-
- // Others
- private var loading = false
- private var lastDataLoadedEmpty = false
- private var loadMore = false
-
- // View Lifecycles
- override var preferredStatusBarStyle: UIStatusBarStyle {
- return .lightContent
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- labelTV.delegate = self
- labelTV.dataSource = self
- labelTV.rowHeight = UITableView.automaticDimension
- labelTV.estimatedRowHeight = 220
-
- presenter = LabelPresenter(siraniApiServices: SiraniApiServices())
- presenter.attachView(view: self)
- loadData()
- }
-
- override func viewWillAppear(_ animated: Bool) {
- presenter.attachView(view: self)
- }
-
- override func viewDidDisappear(_ animated: Bool) {
- presenter.detachView()
- }
-
- // private functions
- private func loadData() {
- if !loading && !lastDataLoadedEmpty {
- if ConnectionHelper.isConnectedToNetwork() {
- self.presenter.loadData(status: self.status, limit: self.limit, page: self.page, search: self.search)
- } else {
- showNoConnection()
- }
- }
- }
-
- private func downloadFile(urlWeb: String, filename: String) {
- if !loading {
- if ConnectionHelper.isConnectedToNetwork() {
- self.presenter.downloadFile(urlWeb: urlWeb, filename: filename)
- } else {
- showNoConnection()
- }
- }
- }
-
- // Actions
- @IBAction func onBtnBackTap(_ sender: Any) {
- performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
- }
-
- @IBAction func onBtnSearchTap(_ sender: Any) {
- performSegue(withIdentifier: searchSegueId, sender: self)
- }
-
- @IBAction func unwindToLabel(segue: UIStoryboardSegue) {}
-
- }
- // View Delegate Protocols
- extension LabelVC: LabelViewDelegate {
-
- func showNoConnection() {
- let alertNoConnection = UIAlertController(
- title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
- message: Constants.instance.ALERT_NO_CONNECTION_MSG,
- preferredStyle: .alert
- )
- alertNoConnection.addAction(
- UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, style: .default, handler: nil)
- )
- self.present(alertNoConnection, animated: true, completion: nil)
- }
-
- func showLoading() {
- loading = true
- progress.startAnimating()
- progressLayout.show()
- }
-
- func hideLoading() {
- loading = false
- progress.stopAnimating()
- progressLayout.hide()
- }
-
- func showNoDataLayout() {
- if page == 1 {
- noDataLayout.show()
- }
- }
-
- func hideNoDataLayout() {
- noDataLayout.hide()
- }
-
- func setDataList(dataList: [Label]) {
-
- if page == 1 {
- noDataLayout.hide()
- }
-
- if dataList.count > 0 {
-
- for data in dataList {
- self.dataList.append(data)
- labelTV.beginUpdates()
- labelTV.insertRows(
- at: [IndexPath(row: self.dataList.count-1, section: 0)],
- with: .none
- )
- labelTV.endUpdates()
- }
-
- page += 1
- loadMore = true
- } else {
- lastDataLoadedEmpty = true
- loadMore = false
- }
- }
-
- func onDownloadSuccess(urlFile: URL) {
- self.documentInteractorController = UIDocumentInteractionController()
- self.documentInteractorController.delegate = self
- self.documentInteractorController.url = urlFile
- self.documentInteractorController.presentPreview(animated: true)
- }
-
- func onDownloadFailed() {
- let alertNoConnection = UIAlertController(
- title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
- message: "Download Gagal",
- preferredStyle: .alert
- )
- alertNoConnection.addAction(
- UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, style: .default, handler: nil)
- )
- self.present(alertNoConnection, animated: true, completion: nil)
- }
- }
- // UITableController Protocols
- extension LabelVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, LabelCellDelegate {
- func openLabelDetail(data: Label?) {
- self.kode = data?.lic_id
- performSegue(withIdentifier: "labelDetailSegueId", sender: self)
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return dataList.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- if tableView == labelTV {
- if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? LabelCell {
- let data = dataList[indexPath.row]
- cell.cellDelegate = self
- // cell.btnDwnldPeringatan.tag = indexPath.row
- // cell.btnDownloadQrCode.tag = indexPath.row
- // cell.btnDownloadLabel.tag = indexPath.row
- cell.updateViews(labelData: data)
- return cell
- } else {
- return LabelCell()
- }
- } else {
- return LabelCell()
- }
- }
- func scrollViewDidScroll(_ scrollView: UIScrollView) {
- let threshold = -5
- let contentOffset = scrollView.contentOffset.y
- let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
- if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
- loadData()
- }
- }
- // func download(urlWeb: String?) {
- // if urlWeb != nil {
- // var filename = "file.pdf"
- // let values = urlWeb!.split(separator: "/")
- // if values.count > 0 {
- // filename = String(values[values.count-1])
- // }
- //
- // self.downloadFile(urlWeb: urlWeb!, filename: filename)
- // }
- // }
- }
- // DocumentInteractionController
- extension LabelVC: UIDocumentInteractionControllerDelegate {
- func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
- guard let navVC = self.navigationController else {
- return self
- }
- return navVC
- }
- }
- // Search Delegate Protocols
- extension LabelVC: SearchLabelDelegate {
- func onSearch(search: String, status: String, jenisLabel: String) {
- presenter.attachView(view: self)
- loadMore = false
- loading = false
- lastDataLoadedEmpty = false
- self.limit = 5
- self.page = 1
- self.search = search
- self.status = status
-
- self.subTitleLabel.text = jenisLabel
- dataList.removeAll()
- labelTV.reloadData()
- loadData()
- }
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if segue.identifier == searchSegueId {
- let destinationVC: SearchLabelVC = segue.destination as! SearchLabelVC
- destinationVC.delegate = self
- }
-
- if let detailLabelVC = segue.destination as? QRScannerVC {
- detailLabelVC.kode = kode
- detailLabelVC.isLabel = isLabel
- }
- }
-
- }
|