| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- //
- // OdsSp2BeforeVC.swift
- // Sirani
- //
- // Created by Anhar Solehudin on 20/04/19.
- // Copyright © 2019 Anhar Solehudin. All rights reserved.
- //
- import UIKit
- class OdsSp2BeforeVC: UIViewController {
-
- // Outlets
- @IBOutlet weak var sp2BeforeTV: UITableView!
- @IBOutlet weak var noDataLayout: UIView!
- @IBOutlet weak var progressLayout: UIView!
- @IBOutlet weak var progress: UIActivityIndicatorView!
- // View Id
- private let cellId = "sp2BeforeCellId"
-
- // Segue Id
- private let unwindToHomeSegueId = "unwindSp2BeforeToHomeSegueId"
- private let searchSegueId = "searchOdsSp2BeforeSegueId"
-
- // 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: OdsSp2BeforePresenter!
- private var dataList: [OdsSp2Before] = [OdsSp2Before]()
-
- // Request Params
- private var cursor: Int = 0
- private var prev: Int = 1
- private var jenis: String = ""
- private var columns: String = ""
- 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()
-
- sp2BeforeTV.delegate = self
- sp2BeforeTV.dataSource = self
-
- presenter = OdsSp2BeforePresenter(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() {
- presenter.loadData(cursor: cursor, prev: prev, jenis: jenis, columns: columns, search: search)
- } 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 unwindToOdsSp2Before(segue: UIStoryboardSegue) {}
- }
- // View Delegate Protocols
- extension OdsSp2BeforeVC: OdsSp2BeforeViewDelegate {
-
- 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
- progress.startAnimating()
- progressLayout.show()
- }
-
- func hideLoading() {
- loading = false
- progress.stopAnimating()
- progressLayout.hide()
- }
-
- func showNoDataLayout() {
- if cursor == 0 {
- noDataLayout.show()
- }
- }
-
- func hideNoDataLayout() {
- noDataLayout.hide()
- }
-
- func setDataList(dataList: [OdsSp2Before]) {
-
- if cursor == 0 {
- noDataLayout.hide()
- }
-
- if dataList.count > 0 {
-
- for data in dataList {
- self.dataList.append(data)
- sp2BeforeTV.beginUpdates()
- sp2BeforeTV.insertRows(
- at: [IndexPath(row: self.dataList.count-1, section: 0)],
- with: .none
- )
- sp2BeforeTV.endUpdates()
- }
-
- let totalData = dataList.count
- cursor += totalData
- loadMore = true
- } else {
- lastDataLoadedEmpty = true
- loadMore = false
- }
- }
- }
- // UITableController Protocols
- extension OdsSp2BeforeVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return dataList.count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- if tableView == sp2BeforeTV {
- if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? OdsSp2BeforeCell {
-
- let data = dataList[indexPath.row]
- cell.updateViews(odsSp2Before: data)
-
- return cell
- } else {
- return OdsSp2BeforeCell()
- }
- } else {
- return OdsSp2BeforeCell()
- }
- }
-
- 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()
- }
- }
-
- }
- // Search Delegate Protocols
- extension OdsSp2BeforeVC: SearchOdsSp2BeforeDelegate {
- func onSearch(search: String, parameter: String, jenis: String) {
- presenter.attachView(view: self)
-
- loadMore = false
- loading = false
- lastDataLoadedEmpty = false
-
- self.cursor = 0
- self.prev = 1
- self.search = search
- self.jenis = jenis
- self.columns = parameter
- dataList.removeAll()
- sp2BeforeTV.reloadData()
-
- loadData()
- }
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if segue.identifier == searchSegueId {
- let destinationVC: SearchOdsSp2BeforeVC = segue.destination as! SearchOdsSp2BeforeVC
- destinationVC.delegate = self
- }
- }
- }
|