BalaiUjiVC.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. presenter.loadData(status: status, limit: limit, page: page, jenis: jenis, 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 page == 1 {
  131. noDataLayout.show()
  132. }
  133. }
  134. func hideNoDataLayout() {
  135. noDataLayout.hide()
  136. }
  137. func setDataList(dataList: [BalaiUji]) {
  138. if page == 1 {
  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. page += 1
  152. loadMore = true
  153. } else {
  154. lastDataLoadedEmpty = true
  155. loadMore = false
  156. }
  157. }
  158. }
  159. // UITableController Protocols
  160. extension BalaiUjiVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, BalaiUjiCellDelegate {
  161. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  162. return dataList.count
  163. }
  164. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  165. if tableView == balaiUjiTV {
  166. if let cell = tableView.dequeueReusableCell(withIdentifier: balaiUjiCellId, for: indexPath) as? BalaiUjiCell {
  167. let balaiUji = dataList[indexPath.row]
  168. cell.cellDelegate = self
  169. cell.btnWebsite.tag = indexPath.row
  170. cell.updateViews(balaiUji: balaiUji)
  171. return cell
  172. } else {
  173. return BalaiUjiCell()
  174. }
  175. } else {
  176. return BalaiUjiCell()
  177. }
  178. }
  179. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  180. let threshold = -5
  181. let contentOffset = scrollView.contentOffset.y
  182. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  183. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  184. loadData()
  185. }
  186. }
  187. func openWeb(urlWeb: String?) {
  188. if let urlWeb = urlWeb {
  189. if let urlPage = URL(string: urlWeb) {
  190. if UIApplication.shared.canOpenURL(urlPage) {
  191. if #available(iOS 10.0, *) {
  192. UIApplication.shared.open(urlPage)
  193. } else {
  194. UIApplication.shared.openURL(urlPage)
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. // SearchBalaiUji Delegate Protocols
  202. extension BalaiUjiVC: SearchBalaiUjiDelegate {
  203. func onSearch(search: String, parameter: String, jenis: String, jenisLabel: String, valueLabel: String) {
  204. presenter.attachView(view: self)
  205. loadMore = false
  206. loading = false
  207. lastDataLoadedEmpty = false
  208. self.limit = 5
  209. self.page = 1
  210. self.search = search
  211. self.jenis = jenis
  212. // self.status = status
  213. dataList.removeAll()
  214. balaiUjiTV.reloadData()
  215. lblSubtitle.text = jenisLabel
  216. if valueLabel == "LN" {
  217. self.btnDownloadLuarNegeriData.show()
  218. } else {
  219. self.btnDownloadLuarNegeriData.hide()
  220. }
  221. loadData()
  222. }
  223. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  224. if segue.identifier == searchSegueId {
  225. let destinationVC: SearchBalaiUjiVC = segue.destination as! SearchBalaiUjiVC
  226. destinationVC.delegate = self
  227. }
  228. }
  229. }
  230. extension BalaiUjiVC: UIDocumentInteractionControllerDelegate {
  231. func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
  232. guard let navVC = self.navigationController else {
  233. return self
  234. }
  235. return navVC
  236. }
  237. }