BalaiUjiVC.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 limit: Int = 5
  33. private var page: Int = 1
  34. private var jenis: String = "ID"
  35. private var search: String = ""
  36. private var status: 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. ConnectionHelper.checkIpAddress { [weak self] success in
  64. guard let self = self else { return }
  65. DispatchQueue.main.async {
  66. if success {
  67. self.presenter.loadData(status: self.status, limit: self.limit, page: self.page, jenis: self.jenis, search: self.search)
  68. } else {
  69. CommonHelper.showNotAllowConnection(in: self)
  70. }
  71. }
  72. }
  73. } else {
  74. showNoConnection()
  75. }
  76. }
  77. }
  78. // Actions
  79. @IBAction func onBtnBackTap(_ sender: Any) {
  80. performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
  81. }
  82. @IBAction func onBtnSearchTap(_ sender: Any) {
  83. if !loading { performSegue(withIdentifier: searchSegueId, sender: self) }
  84. }
  85. @IBAction func navToMapBalaiUji(_ sender: Any) {
  86. if !loading { performSegue(withIdentifier: mapsSegueId, sender: self) }
  87. }
  88. @IBAction func onBtnLuarNegeriDataTap(_ sender: Any) {
  89. if !loading {
  90. if ConnectionHelper.isConnectedToNetwork() {
  91. ConnectionHelper.checkIpAddress { [weak self] success in
  92. guard let self = self else { return }
  93. DispatchQueue.main.async {
  94. if success {
  95. self.progressLoading.show()
  96. self.loading = true
  97. let filename = "2019_Pengakuan_Balai_Uji_Asing.pdf";
  98. let destination: DownloadRequest.Destination = { _, _ in
  99. let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
  100. let fileURL = documentsURL.appendingPathComponent(filename)
  101. return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
  102. }
  103. let urlRequest = "https://sertifikasi.postel.go.id/esertifikasi_mobile/assets/file/2019_Pengakuan_Balai_Uji_Asing.pdf"
  104. AF.download(urlRequest, to: destination).validate().responseData { response in
  105. if let url = response.fileURL {
  106. self.documentInteractorController = UIDocumentInteractionController()
  107. self.documentInteractorController.delegate = self
  108. self.documentInteractorController.url = url
  109. self.documentInteractorController.presentPreview(animated: true)
  110. }
  111. DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
  112. self.progressLoading.hide()
  113. self.loading = false
  114. }
  115. }
  116. } else {
  117. CommonHelper.showNotAllowConnection(in: self)
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. @IBAction func unwindToBalaiUji(segue: UIStoryboardSegue) {}
  125. }
  126. // View Delegate Protocols
  127. extension BalaiUjiVC: BalaiUjiViewDelegate {
  128. func showNoConnection() {
  129. let alertNoConnection = UIAlertController(
  130. title: alertNoConnectionTitle,
  131. message: alertNoConnectionMsg,
  132. preferredStyle: .alert
  133. )
  134. alertNoConnection.addAction(
  135. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  136. )
  137. self.present(alertNoConnection, animated: true, completion: nil)
  138. }
  139. func showLoading() {
  140. loading = true
  141. progressLoading.show()
  142. }
  143. func hideLoading() {
  144. loading = false
  145. progressLoading.hide()
  146. }
  147. func showNoDataLayout() {
  148. if page == 1 {
  149. noDataLayout.show()
  150. }
  151. }
  152. func hideNoDataLayout() {
  153. noDataLayout.hide()
  154. }
  155. func setDataList(dataList: [BalaiUji]) {
  156. if page == 1 {
  157. noDataLayout.hide()
  158. }
  159. if dataList.count > 0 {
  160. for data in dataList {
  161. self.dataList.append(data)
  162. balaiUjiTV.beginUpdates()
  163. balaiUjiTV.insertRows(
  164. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  165. with: .none
  166. )
  167. balaiUjiTV.endUpdates()
  168. }
  169. page += 1
  170. loadMore = true
  171. } else {
  172. lastDataLoadedEmpty = true
  173. loadMore = false
  174. }
  175. }
  176. }
  177. // UITableController Protocols
  178. extension BalaiUjiVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, BalaiUjiCellDelegate {
  179. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  180. return dataList.count
  181. }
  182. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  183. if tableView == balaiUjiTV {
  184. if let cell = tableView.dequeueReusableCell(withIdentifier: balaiUjiCellId, for: indexPath) as? BalaiUjiCell {
  185. let balaiUji = dataList[indexPath.row]
  186. cell.cellDelegate = self
  187. cell.btnWebsite.tag = indexPath.row
  188. cell.updateViews(balaiUji: balaiUji)
  189. return cell
  190. } else {
  191. return BalaiUjiCell()
  192. }
  193. } else {
  194. return BalaiUjiCell()
  195. }
  196. }
  197. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  198. let threshold = -5
  199. let contentOffset = scrollView.contentOffset.y
  200. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  201. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  202. loadData()
  203. }
  204. }
  205. func openWeb(urlWeb: String?) {
  206. if let urlWeb = urlWeb {
  207. if let urlPage = URL(string: urlWeb) {
  208. if UIApplication.shared.canOpenURL(urlPage) {
  209. if #available(iOS 10.0, *) {
  210. UIApplication.shared.open(urlPage)
  211. } else {
  212. UIApplication.shared.openURL(urlPage)
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. // SearchBalaiUji Delegate Protocols
  220. extension BalaiUjiVC: SearchBalaiUjiDelegate {
  221. func onSearch(search: String, parameter: String, jenis: String, jenisLabel: String, valueLabel: String) {
  222. presenter.attachView(view: self)
  223. loadMore = false
  224. loading = false
  225. lastDataLoadedEmpty = false
  226. self.limit = 5
  227. self.page = 1
  228. self.search = search
  229. self.jenis = jenis
  230. // self.status = status
  231. dataList.removeAll()
  232. balaiUjiTV.reloadData()
  233. lblSubtitle.text = jenisLabel
  234. if valueLabel == "LN" {
  235. self.btnDownloadLuarNegeriData.show()
  236. } else {
  237. self.btnDownloadLuarNegeriData.hide()
  238. }
  239. loadData()
  240. }
  241. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  242. if segue.identifier == searchSegueId {
  243. let destinationVC: SearchBalaiUjiVC = segue.destination as! SearchBalaiUjiVC
  244. destinationVC.delegate = self
  245. }
  246. }
  247. }
  248. extension BalaiUjiVC: UIDocumentInteractionControllerDelegate {
  249. func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
  250. guard let navVC = self.navigationController else {
  251. return self
  252. }
  253. return navVC
  254. }
  255. }