TarifSertifikatVC.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. ConnectionHelper.checkIpAddress { [weak self] success in
  61. guard let self = self else { return }
  62. DispatchQueue.main.async {
  63. if success {
  64. self.presenter.loadData(jenis: self.jenis, columns: self.columns, cursor: self.cursor, prev: self.prev, search: self.search)
  65. } else {
  66. CommonHelper.showNotAllowConnection(in: self)
  67. }
  68. }
  69. }
  70. } else {
  71. showNoConnection()
  72. }
  73. }
  74. }
  75. // Actions
  76. @IBAction func onBackTapped(_ sender: Any) {
  77. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  78. }
  79. @IBAction func onSearchTapped(_ sender: Any) {
  80. performSegue(withIdentifier: searchSegueId, sender: self)
  81. }
  82. @IBAction func unwindToTarifSertifikat(segue: UIStoryboardSegue) {}
  83. }
  84. // View Delegate Protocols
  85. extension TarifSertifikatVC: TarifSertifikatViewDelegate {
  86. func showNoConnection() {
  87. let alertNoConnection = UIAlertController(
  88. title: alertNoConnectionTitle,
  89. message: alertNoConnectionMsg,
  90. preferredStyle: .alert
  91. )
  92. alertNoConnection.addAction(
  93. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  94. )
  95. self.present(alertNoConnection, animated: true, completion: nil)
  96. }
  97. func showLoading() {
  98. loading = true
  99. progressLoading.show()
  100. }
  101. func hideLoading() {
  102. loading = false
  103. progressLoading.hide()
  104. }
  105. func showNoDataLayout() {
  106. if cursor == 0 {
  107. layoutNoData.show()
  108. }
  109. }
  110. func hideNoDataLayout() {
  111. layoutNoData.hide()
  112. }
  113. func setDataList(dataList: [TarifSertifikat]) {
  114. if cursor == 0 {
  115. layoutNoData.hide()
  116. }
  117. if dataList.count > 0 {
  118. for data in dataList {
  119. self.dataList.append(data)
  120. tarifSertifikatTV.beginUpdates()
  121. tarifSertifikatTV.insertRows(
  122. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  123. with: .none
  124. )
  125. tarifSertifikatTV.endUpdates()
  126. }
  127. // let totalData = dataList.count
  128. // cursor += totalData
  129. loadMore = false
  130. } else {
  131. lastDataLoadedEmpty = true
  132. loadMore = false
  133. }
  134. }
  135. }
  136. // UITableController Protocols
  137. extension TarifSertifikatVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
  138. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  139. return dataList.count
  140. }
  141. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  142. if tableView == tarifSertifikatTV {
  143. if let cell = tableView.dequeueReusableCell(withIdentifier: tarifSertifikatCellId, for: indexPath) as? TarifSertifikatCell {
  144. let tarifSertifikat = dataList[indexPath.row]
  145. cell.updateViews(tarifSertifikat: tarifSertifikat)
  146. return cell
  147. } else {
  148. return TarifSertifikatCell()
  149. }
  150. } else {
  151. return TarifSertifikatCell()
  152. }
  153. }
  154. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  155. let threshold = -5
  156. let contentOffset = scrollView.contentOffset.y
  157. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  158. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  159. loadData()
  160. }
  161. }
  162. }
  163. // SearchTarifSertifikatModal Delegate Protocols
  164. extension TarifSertifikatVC: SearchTarifSertifikatDelegate {
  165. func onSearch(search: String, parameter: String, jenis: String, jenisLabel: String) {
  166. presenter.attachView(view: self)
  167. loadMore = false
  168. loading = false
  169. lastDataLoadedEmpty = false
  170. self.cursor = 0
  171. self.prev = 0
  172. self.search = search
  173. self.jenis = jenis
  174. self.columns = parameter
  175. if jenis == "0" {
  176. if let url = URL(string: urlPath) {
  177. if UIApplication.shared.canOpenURL(url) {
  178. print("canopen")
  179. if #available(iOS 10.0, *) {
  180. UIApplication.shared.open(url)
  181. } else {
  182. UIApplication.shared.openURL(url)
  183. }
  184. }
  185. }
  186. } else {
  187. dataList.removeAll()
  188. tarifSertifikatTV.reloadData()
  189. lblSubtitle.text = jenisLabel
  190. loadData()
  191. }
  192. }
  193. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  194. if segue.identifier == searchSegueId {
  195. let destinationVC: SearchTarifSertifikatVC = segue.destination as! SearchTarifSertifikatVC
  196. destinationVC.delegate = self
  197. }
  198. }
  199. }