TarifSertifikatVC.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // TarifSertifikatVC.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 31/03/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. class TarifSertifikatVC: UIViewController {
  10. // Outlets & Views
  11. @IBOutlet weak var lblSubtitle: UILabel!
  12. @IBOutlet weak var tarifSertifikatTV: UITableView!
  13. @IBOutlet weak var layoutNoData: UIView!
  14. @IBOutlet weak var progressLoading: UIView!
  15. // View Id
  16. private let tarifSertifikatCellId = "TarifSertifikatCellId"
  17. // Segue Id
  18. private let unwindToHomeSegueId = "unwindTarifToHome"
  19. private let searchSegueId = "tarifSertifikatSearchSegueId"
  20. // String Message
  21. private let alertNoConnectionTitle = "Informasi"
  22. private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet"
  23. private let alertNoConnectionActionYes = "OK"
  24. // Initialize
  25. private var presenter: TarifSertifikatPresenter!
  26. private var dataList: [TarifSertifikat] = [TarifSertifikat]()
  27. // Request Params
  28. private var jenis: String = "1"
  29. private var columns: String = ""
  30. private var cursor: Int = 0
  31. private var prev: Int = 0
  32. private var search: String = ""
  33. private let urlPath = "http://www.telkomtesthouse.co.id/Chargeclient"
  34. // Others
  35. private var loading = false
  36. private var lastDataLoadedEmpty = false
  37. private var loadMore = false
  38. // View Lifecycles
  39. override var preferredStatusBarStyle: UIStatusBarStyle {
  40. return .lightContent
  41. }
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. tarifSertifikatTV.delegate = self
  45. tarifSertifikatTV.dataSource = self
  46. presenter = TarifSertifikatPresenter(siraniApiServices: SiraniApiServices())
  47. presenter.attachView(view: self)
  48. loadData()
  49. }
  50. override func viewWillAppear(_ animated: Bool) {
  51. presenter.attachView(view: self)
  52. }
  53. override func viewDidDisappear(_ animated: Bool) {
  54. presenter.detachView()
  55. }
  56. // private functions
  57. private func loadData() {
  58. if !loading && !lastDataLoadedEmpty {
  59. if ConnectionHelper.isConnectedToNetwork() {
  60. presenter.loadData(jenis: jenis, columns: columns, cursor: cursor, prev: prev, search: search)
  61. } else {
  62. showNoConnection()
  63. }
  64. }
  65. }
  66. // Actions
  67. @IBAction func onBackTapped(_ sender: Any) {
  68. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  69. }
  70. @IBAction func onSearchTapped(_ sender: Any) {
  71. performSegue(withIdentifier: searchSegueId, sender: self)
  72. }
  73. @IBAction func unwindToTarifSertifikat(segue: UIStoryboardSegue) {}
  74. }
  75. // View Delegate Protocols
  76. extension TarifSertifikatVC: TarifSertifikatViewDelegate {
  77. func showNoConnection() {
  78. let alertNoConnection = UIAlertController(
  79. title: alertNoConnectionTitle,
  80. message: alertNoConnectionMsg,
  81. preferredStyle: .alert
  82. )
  83. alertNoConnection.addAction(
  84. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  85. )
  86. self.present(alertNoConnection, animated: true, completion: nil)
  87. }
  88. func showLoading() {
  89. loading = true
  90. progressLoading.show()
  91. }
  92. func hideLoading() {
  93. loading = false
  94. progressLoading.hide()
  95. }
  96. func showNoDataLayout() {
  97. if cursor == 0 {
  98. layoutNoData.show()
  99. }
  100. }
  101. func hideNoDataLayout() {
  102. layoutNoData.hide()
  103. }
  104. func setDataList(dataList: [TarifSertifikat]) {
  105. if cursor == 0 {
  106. layoutNoData.hide()
  107. }
  108. if dataList.count > 0 {
  109. for data in dataList {
  110. self.dataList.append(data)
  111. tarifSertifikatTV.beginUpdates()
  112. tarifSertifikatTV.insertRows(
  113. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  114. with: .none
  115. )
  116. tarifSertifikatTV.endUpdates()
  117. }
  118. // let totalData = dataList.count
  119. // cursor += totalData
  120. loadMore = false
  121. } else {
  122. lastDataLoadedEmpty = true
  123. loadMore = false
  124. }
  125. }
  126. }
  127. // UITableController Protocols
  128. extension TarifSertifikatVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
  129. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  130. return dataList.count
  131. }
  132. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  133. if tableView == tarifSertifikatTV {
  134. if let cell = tableView.dequeueReusableCell(withIdentifier: tarifSertifikatCellId, for: indexPath) as? TarifSertifikatCell {
  135. let tarifSertifikat = dataList[indexPath.row]
  136. cell.updateViews(tarifSertifikat: tarifSertifikat)
  137. return cell
  138. } else {
  139. return TarifSertifikatCell()
  140. }
  141. } else {
  142. return TarifSertifikatCell()
  143. }
  144. }
  145. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  146. let threshold = -5
  147. let contentOffset = scrollView.contentOffset.y
  148. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  149. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  150. loadData()
  151. }
  152. }
  153. }
  154. // SearchTarifSertifikatModal Delegate Protocols
  155. extension TarifSertifikatVC: SearchTarifSertifikatDelegate {
  156. func onSearch(search: String, parameter: String, jenis: String, jenisLabel: String) {
  157. presenter.attachView(view: self)
  158. loadMore = false
  159. loading = false
  160. lastDataLoadedEmpty = false
  161. self.cursor = 0
  162. self.prev = 0
  163. self.search = search
  164. self.jenis = jenis
  165. self.columns = parameter
  166. if jenis == "0" {
  167. if let url = URL(string: urlPath) {
  168. if UIApplication.shared.canOpenURL(url) {
  169. print("canopen")
  170. if #available(iOS 10.0, *) {
  171. UIApplication.shared.open(url)
  172. } else {
  173. UIApplication.shared.openURL(url)
  174. }
  175. }
  176. }
  177. } else {
  178. dataList.removeAll()
  179. tarifSertifikatTV.reloadData()
  180. lblSubtitle.text = jenisLabel
  181. loadData()
  182. }
  183. }
  184. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  185. if segue.identifier == searchSegueId {
  186. let destinationVC: SearchTarifSertifikatVC = segue.destination as! SearchTarifSertifikatVC
  187. destinationVC.delegate = self
  188. }
  189. }
  190. }