// // AplikasiVC.swift // Sirani // // Created by Hendi on 21/06/23. // Copyright © 2023 PT. Mitra Sinerji Teknoindo. All rights reserved. // import UIKit class AplikasiVC: UIViewController { // Outlets @IBOutlet weak var aplikasiTV: 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 = "AplikasiCellId" // Segue Id private let unwindToHomeSegueId = "unwindAplikasiToHomeSegueId" private let searchSegueId = "searchAplikasiSegueId" // Initialize private var presenter: AplikasiPresenter! private var dataList: [Aplikasi] = [Aplikasi]() private var documentInteractorController: UIDocumentInteractionController! // Request Params private var limit: Int = 5 private var page: Int = 1 private var status: String = "apl_all" private var search: String = "" // 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() aplikasiTV.delegate = self aplikasiTV.dataSource = self presenter = AplikasiPresenter(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 unwindToAplikasi(segue: UIStoryboardSegue) {} } // View Delegate Protocols extension AplikasiVC: AplikasiViewDelegate { 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: [Aplikasi]) { if page == 1 { noDataLayout.hide() } if dataList.count > 0 { for data in dataList { self.dataList.append(data) aplikasiTV.beginUpdates() aplikasiTV.insertRows( at: [IndexPath(row: self.dataList.count-1, section: 0)], with: .none ) aplikasiTV.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 AplikasiVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, AplikasiCellDelegate { func openAplikasiDetail(data: Aplikasi?) { print(data?.apl_id) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataList.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView == aplikasiTV { if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? AplikasiCell { let data = dataList[indexPath.row] cell.cellDelegate = self // cell.btnDwnldPeringatan.tag = indexPath.row // cell.btnDownloadQrCode.tag = indexPath.row // cell.btnDownloadAplikasi.tag = indexPath.row cell.updateViews(aplData: data) return cell } else { return AplikasiCell() } } else { return AplikasiCell() } } 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 AplikasiVC: UIDocumentInteractionControllerDelegate { func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { guard let navVC = self.navigationController else { return self } return navVC } } // Search Delegate Protocols extension AplikasiVC: SearchAplikasiDelegate { 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() aplikasiTV.reloadData() loadData() } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == searchSegueId { let destinationVC: SearchAplikasiVC = segue.destination as! SearchAplikasiVC destinationVC.delegate = self } } }