BalaiUjiVC.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // BalaiUjiVC.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 03/04/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. class BalaiUjiVC: UIViewController {
  11. // Outlets & Views
  12. @IBOutlet weak var lblSubtitle: UILabel!
  13. @IBOutlet weak var balaiUjiTV: UITableView!
  14. @IBOutlet weak var progressLoading: UIView!
  15. @IBOutlet weak var noDataLayout: UIView!
  16. @IBOutlet weak var btnDownloadLuarNegeriData: UIButton!
  17. // View Id
  18. private let balaiUjiCellId = "BalaiUjiCellId"
  19. // Segue Id
  20. private let unwindToHomeSegueId = "unwindBalaiUjiToHome"
  21. private let searchSegueId = "balaiUjiSearchSegueId"
  22. private let mapsSegueId = "lokasiBalaiUjiSegueId"
  23. // String Message
  24. private let alertNoConnectionTitle = "Informasi"
  25. private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet"
  26. private let alertNoConnectionActionYes = "OK"
  27. // Initialize
  28. private var presenter: BalaiUjiPresenter!
  29. private var dataList: [BalaiUji] = [BalaiUji]()
  30. private var documentInteractorController: UIDocumentInteractionController!
  31. // Request Params
  32. private var cursor: Int = 0
  33. private var prev: Int = 1
  34. private var jenis: String = "ID"
  35. private var columns: String = ""
  36. private var search: String = ""
  37. // Others
  38. private var loading = false
  39. private var lastDataLoadedEmpty = false
  40. private var loadMore = false
  41. // View Lifecycles
  42. override var preferredStatusBarStyle: UIStatusBarStyle {
  43. return .lightContent
  44. }
  45. override func viewDidLoad() {
  46. super.viewDidLoad()
  47. balaiUjiTV.delegate = self
  48. balaiUjiTV.dataSource = self
  49. presenter = BalaiUjiPresenter(siraniApiServices: SiraniApiServices())
  50. presenter.attachView(view: self)
  51. loadData()
  52. }
  53. override func viewWillAppear(_ animated: Bool) {
  54. presenter.attachView(view: self)
  55. }
  56. override func viewDidDisappear(_ animated: Bool) {
  57. presenter.detachView()
  58. }
  59. // private functions
  60. private func loadData() {
  61. if !loading && !lastDataLoadedEmpty {
  62. if ConnectionHelper.isConnectedToNetwork() {
  63. presenter.loadData(cursor: cursor, prev: prev, jenis: jenis, columns: columns, search: search)
  64. } else {
  65. showNoConnection()
  66. }
  67. }
  68. }
  69. // Actions
  70. @IBAction func onBtnBackTap(_ sender: Any) {
  71. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  72. }
  73. @IBAction func onBtnSearchTap(_ sender: Any) {
  74. if !loading { performSegue(withIdentifier: searchSegueId, sender: self) }
  75. }
  76. @IBAction func navToMapBalaiUji(_ sender: Any) {
  77. if !loading { performSegue(withIdentifier: mapsSegueId, sender: self) }
  78. }
  79. @IBAction func onBtnLuarNegeriDataTap(_ sender: Any) {
  80. if !loading {
  81. if ConnectionHelper.isConnectedToNetwork() {
  82. self.progressLoading.show()
  83. self.loading = true
  84. let filename = "2019_Pengakuan_Balai_Uji_Asing.pdf";
  85. let destination: DownloadRequest.Destination = { _, _ in
  86. let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
  87. let fileURL = documentsURL.appendingPathComponent(filename)
  88. return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
  89. }
  90. let urlRequest = "https://sertifikasi.postel.go.id/esertifikasi_mobile/assets/file/2019_Pengakuan_Balai_Uji_Asing.pdf"
  91. AF.download(urlRequest, to: destination).validate().responseData { response in
  92. if let url = response.fileURL {
  93. self.documentInteractorController = UIDocumentInteractionController()
  94. self.documentInteractorController.delegate = self
  95. self.documentInteractorController.url = url
  96. self.documentInteractorController.presentPreview(animated: true)
  97. }
  98. DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
  99. self.progressLoading.hide()
  100. self.loading = false
  101. }
  102. }
  103. }
  104. }
  105. }
  106. @IBAction func unwindToBalaiUji(segue: UIStoryboardSegue) {}
  107. }
  108. // View Delegate Protocols
  109. extension BalaiUjiVC: BalaiUjiViewDelegate {
  110. func showNoConnection() {
  111. let alertNoConnection = UIAlertController(
  112. title: alertNoConnectionTitle,
  113. message: alertNoConnectionMsg,
  114. preferredStyle: .alert
  115. )
  116. alertNoConnection.addAction(
  117. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  118. )
  119. self.present(alertNoConnection, animated: true, completion: nil)
  120. }
  121. func showLoading() {
  122. loading = true
  123. progressLoading.show()
  124. }
  125. func hideLoading() {
  126. loading = false
  127. progressLoading.hide()
  128. }
  129. func showNoDataLayout() {
  130. if cursor == 0 {
  131. noDataLayout.show()
  132. }
  133. }
  134. func hideNoDataLayout() {
  135. noDataLayout.hide()
  136. }
  137. func setDataList(dataList: [BalaiUji]) {
  138. if cursor == 0 {
  139. noDataLayout.hide()
  140. }
  141. if dataList.count > 0 {
  142. for data in dataList {
  143. self.dataList.append(data)
  144. balaiUjiTV.beginUpdates()
  145. balaiUjiTV.insertRows(
  146. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  147. with: .none
  148. )
  149. balaiUjiTV.endUpdates()
  150. }
  151. let totalData = dataList.count
  152. cursor += totalData
  153. loadMore = true
  154. } else {
  155. lastDataLoadedEmpty = true
  156. loadMore = false
  157. }
  158. }
  159. }
  160. // UITableController Protocols
  161. extension BalaiUjiVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, BalaiUjiCellDelegate {
  162. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  163. return dataList.count
  164. }
  165. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  166. if tableView == balaiUjiTV {
  167. if let cell = tableView.dequeueReusableCell(withIdentifier: balaiUjiCellId, for: indexPath) as? BalaiUjiCell {
  168. let balaiUji = dataList[indexPath.row]
  169. cell.cellDelegate = self
  170. cell.btnWebsite.tag = indexPath.row
  171. cell.updateViews(balaiUji: balaiUji)
  172. return cell
  173. } else {
  174. return BalaiUjiCell()
  175. }
  176. } else {
  177. return BalaiUjiCell()
  178. }
  179. }
  180. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  181. let threshold = -5
  182. let contentOffset = scrollView.contentOffset.y
  183. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  184. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  185. loadData()
  186. }
  187. }
  188. func openWeb(urlWeb: String?) {
  189. if let urlWeb = urlWeb {
  190. if let urlPage = URL(string: urlWeb) {
  191. if UIApplication.shared.canOpenURL(urlPage) {
  192. if #available(iOS 10.0, *) {
  193. UIApplication.shared.open(urlPage)
  194. } else {
  195. UIApplication.shared.openURL(urlPage)
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. // SearchBalaiUji Delegate Protocols
  203. extension BalaiUjiVC: SearchBalaiUjiDelegate {
  204. func onSearch(search: String, parameter: String, jenis: String, jenisLabel: String, valueLabel: String) {
  205. presenter.attachView(view: self)
  206. loadMore = false
  207. loading = false
  208. lastDataLoadedEmpty = false
  209. self.cursor = 0
  210. self.prev = 0
  211. self.search = search
  212. self.jenis = jenis
  213. self.columns = parameter
  214. dataList.removeAll()
  215. balaiUjiTV.reloadData()
  216. lblSubtitle.text = jenisLabel
  217. if valueLabel == "LN" {
  218. self.btnDownloadLuarNegeriData.show()
  219. } else {
  220. self.btnDownloadLuarNegeriData.hide()
  221. }
  222. loadData()
  223. }
  224. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  225. if segue.identifier == searchSegueId {
  226. let destinationVC: SearchBalaiUjiVC = segue.destination as! SearchBalaiUjiVC
  227. destinationVC.delegate = self
  228. }
  229. }
  230. }
  231. extension BalaiUjiVC: UIDocumentInteractionControllerDelegate {
  232. func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
  233. guard let navVC = self.navigationController else {
  234. return self
  235. }
  236. return navVC
  237. }
  238. }