| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- //
- // 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()
- }
- }
-
- }
|