// // HasilSurveyVC.swift // Sirani // // Created by Anhar Solehudin on 12/04/19. // Copyright © 2019 Anhar Solehudin. All rights reserved. // import UIKit class HasilSurveyVC: UIViewController { // Outlets @IBOutlet weak var hasilSurveyTV: UITableView! @IBOutlet weak var noDataLayout: UIView! @IBOutlet weak var progressLoadingLayout: UIView! @IBOutlet weak var progressLoading: UIActivityIndicatorView! // String Message private let alertNoConnectionTitle = "Informasi" private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet" private let alertNoConnectionActionYes = "OK" // Initialize private var presenter: HasilSurveyPresenter! private var dataList: [SurveyResult] = [SurveyResult]() // Request Params private var cursor: Int = 0 private var prev: Int = 0 private var jenis: String = "1" private var columns: String = "" private var search: String = "" // Const private let hasilSurveyCellId = "hasilSurveyCellId" // Others private var loading = false private var firstDataSetLoaded = false private var lastDataLoadedEmpty = false // View Lifecycles override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } override func viewDidLoad() { super.viewDidLoad() hasilSurveyTV.delegate = self hasilSurveyTV.dataSource = self hasilSurveyTV.tableFooterView = UIView() presenter = HasilSurveyPresenter(siraniApiServices: SiraniApiServices()) } override func viewWillAppear(_ animated: Bool) { presenter.attachView(view: self) } override func viewDidAppear(_ animated: Bool) { if !firstDataSetLoaded { loadData() firstDataSetLoaded = true } } override func viewDidDisappear(_ animated: Bool) { presenter.detachView() } // private functions private func loadData() { if !loading && !lastDataLoadedEmpty { if ConnectionHelper.isConnectedToNetwork() { presenter.loadData(jenis: jenis, columns: columns, cursor: cursor, prev: prev, search: search) } else { showNoConnection() } } } @IBAction func onBtnBackTap(_ sender: Any) { performSegue(withIdentifier: "unwindHasilSurveyToHomeSegueId", sender: self) } } // View Delegate Protocols extension HasilSurveyVC: HasilSurveyViewDelegate { func showNoConnection() { let alertNoConnection = UIAlertController( title: alertNoConnectionTitle, message: alertNoConnectionMsg, preferredStyle: .alert ) alertNoConnection.addAction( UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil) ) self.present(alertNoConnection, animated: true, completion: nil) } func showLoading() { loading = true progressLoading.startAnimating() progressLoadingLayout.show() } func hideLoading() { loading = false progressLoading.stopAnimating() progressLoadingLayout.hide() } func showNoDataLayout() { if cursor == 0 { noDataLayout.show() } } func hideNoDataLayout() { noDataLayout.hide() } func setDataList(dataList: [SurveyResult]) { if cursor == 0 { noDataLayout.hide() } if dataList.count > 0 { hasilSurveyTV.beginUpdates() for data in dataList { self.dataList.append(data) hasilSurveyTV.insertRows( at: [IndexPath(row: self.dataList.count-1, section: 0)], with: .automatic ) } hasilSurveyTV.endUpdates() let totalData = dataList.count cursor += totalData } else { lastDataLoadedEmpty = true } } } // UITableController Protocols extension HasilSurveyVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataList.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView == hasilSurveyTV { if let cell = tableView.dequeueReusableCell(withIdentifier: hasilSurveyCellId, for: indexPath) as? HasilSurveyCell { let surveyResult = dataList[indexPath.row] cell.updateViews(surveyResult: surveyResult) return cell } else { return HasilSurveyCell() } } else { return HasilSurveyCell() } } func scrollViewDidScroll(_ scrollView: UIScrollView) { let threshold = 100 let contentOffset = scrollView.contentOffset.y let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height; if !loading && (Int(maximumOffset - contentOffset) <= threshold) { loadData() } } }