| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- //
- // TarifSertifikatVC.swift
- // Sirani
- //
- // Created by Anhar Solehudin on 31/03/19.
- // Copyright © 2019 Anhar Solehudin. All rights reserved.
- //
- import UIKit
- class TarifSertifikatVC: UIViewController {
-
- // Outlets & Views
- @IBOutlet weak var lblSubtitle: UILabel!
- @IBOutlet weak var tarifSertifikatTV: UITableView!
- @IBOutlet weak var layoutNoData: UIView!
- @IBOutlet weak var progressLoading: UIView!
-
- // View Id
- private let tarifSertifikatCellId = "TarifSertifikatCellId"
-
- // Segue Id
- private let unwindToHomeSegueId = "unwindTarifToHome"
- private let searchSegueId = "tarifSertifikatSearchSegueId"
-
- // 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: TarifSertifikatPresenter!
- private var dataList: [TarifSertifikat] = [TarifSertifikat]()
-
- // Request Params
- private var jenis: String = "1"
- private var columns: String = ""
- private var cursor: Int = 0
- private var prev: Int = 0
- private var search: String = ""
- private let urlPath = "http://www.telkomtesthouse.co.id/Chargeclient"
-
- // 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()
-
- tarifSertifikatTV.delegate = self
- tarifSertifikatTV.dataSource = self
-
- presenter = TarifSertifikatPresenter(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(jenis: self.jenis, columns: self.columns, cursor: self.cursor, prev: self.prev, search: self.search)
- } else {
- showNoConnection()
- }
- }
- }
- // Actions
- @IBAction func onBackTapped(_ sender: Any) {
- performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
- }
-
- @IBAction func onSearchTapped(_ sender: Any) {
- performSegue(withIdentifier: searchSegueId, sender: self)
- }
-
- @IBAction func unwindToTarifSertifikat(segue: UIStoryboardSegue) {}
-
- }
- // View Delegate Protocols
- extension TarifSertifikatVC: TarifSertifikatViewDelegate {
-
- 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.show()
- }
-
- func hideLoading() {
- loading = false
- progressLoading.hide()
- }
-
- func showNoDataLayout() {
- if cursor == 0 {
- layoutNoData.show()
- }
- }
-
- func hideNoDataLayout() {
- layoutNoData.hide()
- }
-
- func setDataList(dataList: [TarifSertifikat]) {
- if cursor == 0 {
- layoutNoData.hide()
- }
- if dataList.count > 0 {
-
- for data in dataList {
- self.dataList.append(data)
- tarifSertifikatTV.beginUpdates()
- tarifSertifikatTV.insertRows(
- at: [IndexPath(row: self.dataList.count-1, section: 0)],
- with: .none
- )
- tarifSertifikatTV.endUpdates()
- }
-
- // let totalData = dataList.count
- // cursor += totalData
- loadMore = false
- } else {
- lastDataLoadedEmpty = true
- loadMore = false
- }
- }
- }
- // UITableController Protocols
- extension TarifSertifikatVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return dataList.count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- if tableView == tarifSertifikatTV {
- if let cell = tableView.dequeueReusableCell(withIdentifier: tarifSertifikatCellId, for: indexPath) as? TarifSertifikatCell {
-
- let tarifSertifikat = dataList[indexPath.row]
- cell.updateViews(tarifSertifikat: tarifSertifikat)
-
- return cell
- } else {
- return TarifSertifikatCell()
- }
- } else {
- return TarifSertifikatCell()
- }
- }
-
- 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()
- }
- }
-
- }
- // SearchTarifSertifikatModal Delegate Protocols
- extension TarifSertifikatVC: SearchTarifSertifikatDelegate {
- func onSearch(search: String, parameter: String, jenis: String, jenisLabel: String) {
- presenter.attachView(view: self)
-
- loadMore = false
- loading = false
- lastDataLoadedEmpty = false
-
- self.cursor = 0
- self.prev = 0
- self.search = search
- self.jenis = jenis
- self.columns = parameter
-
- if jenis == "0" {
- if let url = URL(string: urlPath) {
- if UIApplication.shared.canOpenURL(url) {
- print("canopen")
- if #available(iOS 10.0, *) {
- UIApplication.shared.open(url)
- } else {
- UIApplication.shared.openURL(url)
- }
- }
- }
- } else {
- dataList.removeAll()
- tarifSertifikatTV.reloadData()
- lblSubtitle.text = jenisLabel
-
- loadData()
- }
- }
-
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if segue.identifier == searchSegueId {
- let destinationVC: SearchTarifSertifikatVC = segue.destination as! SearchTarifSertifikatVC
- destinationVC.delegate = self
- }
- }
- }
|