| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- //
- // BalaiUjiVC.swift
- // Sirani
- //
- // Created by Anhar Solehudin on 03/04/19.
- // Copyright © 2019 Anhar Solehudin. All rights reserved.
- //
- import UIKit
- import Alamofire
- class BalaiUjiVC: UIViewController {
-
- // Outlets & Views
- @IBOutlet weak var lblSubtitle: UILabel!
- @IBOutlet weak var balaiUjiTV: UITableView!
- @IBOutlet weak var progressLoading: UIView!
- @IBOutlet weak var noDataLayout: UIView!
- @IBOutlet weak var btnDownloadLuarNegeriData: UIButton!
-
- // View Id
- private let balaiUjiCellId = "BalaiUjiCellId"
-
- // Segue Id
- private let unwindToHomeSegueId = "unwindBalaiUjiToHome"
- private let searchSegueId = "balaiUjiSearchSegueId"
- private let mapsSegueId = "lokasiBalaiUjiSegueId"
-
- // String Message
- private let alertNoConnectionTitle = "Informasi"
- private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet"
- private let alertNoConnectionActionYes = "OK"
-
- // Initialize
- private var presenter: BalaiUjiPresenter!
- private var dataList: [BalaiUji] = [BalaiUji]()
- private var documentInteractorController: UIDocumentInteractionController!
-
- // Request Params
- private var limit: Int = 5
- private var page: Int = 1
- private var jenis: String = "ID"
- private var search: String = ""
- private var status: String = ""
-
- // Others
- private var loading = false
- private var lastDataLoadedEmpty = false
- private var loadMore = false
-
- // View Lifecycles
- override var preferredStatusBarStyle: UIStatusBarStyle {
- return .lightContent
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- balaiUjiTV.delegate = self
- balaiUjiTV.dataSource = self
-
- presenter = BalaiUjiPresenter(siraniApiServices: SiraniApiServices())
- presenter.attachView(view: self)
- loadData()
- }
-
- override func viewWillAppear(_ animated: Bool) {
- presenter.attachView(view: self)
- }
-
- override func viewDidDisappear(_ animated: Bool) {
- presenter.detachView()
- }
-
- // private functions
- private func loadData() {
- if !loading && !lastDataLoadedEmpty {
- if ConnectionHelper.isConnectedToNetwork() {
- ConnectionHelper.checkIpAddress { [weak self] success in
- guard let self = self else { return }
- DispatchQueue.main.async {
- if success {
- self.presenter.loadData(status: self.status, limit: self.limit, page: self.page, jenis: self.jenis, search: self.search)
- } else {
- CommonHelper.showNotAllowConnection(in: self)
- }
- }
- }
- } else {
- showNoConnection()
- }
- }
- }
-
- // Actions
- @IBAction func onBtnBackTap(_ sender: Any) {
- performSegue(withIdentifier: unwindToHomeSegueId, sender: self)
- }
-
- @IBAction func onBtnSearchTap(_ sender: Any) {
- if !loading { performSegue(withIdentifier: searchSegueId, sender: self) }
- }
-
- @IBAction func navToMapBalaiUji(_ sender: Any) {
- if !loading { performSegue(withIdentifier: mapsSegueId, sender: self) }
- }
-
- @IBAction func onBtnLuarNegeriDataTap(_ sender: Any) {
- if !loading {
- if ConnectionHelper.isConnectedToNetwork() {
- ConnectionHelper.checkIpAddress { [weak self] success in
- guard let self = self else { return }
- DispatchQueue.main.async {
- if success {
- self.progressLoading.show()
- self.loading = true
-
- let filename = "2019_Pengakuan_Balai_Uji_Asing.pdf";
- let destination: DownloadRequest.Destination = { _, _ in
- let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
- let fileURL = documentsURL.appendingPathComponent(filename)
- return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
- }
-
- let urlRequest = "https://sertifikasi.postel.go.id/esertifikasi_mobile/assets/file/2019_Pengakuan_Balai_Uji_Asing.pdf"
- AF.download(urlRequest, to: destination).validate().responseData { response in
- if let url = response.fileURL {
- self.documentInteractorController = UIDocumentInteractionController()
- self.documentInteractorController.delegate = self
- self.documentInteractorController.url = url
- self.documentInteractorController.presentPreview(animated: true)
- }
-
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
- self.progressLoading.hide()
- self.loading = false
- }
- }
- } else {
- CommonHelper.showNotAllowConnection(in: self)
- }
- }
- }
- }
- }
- }
-
-
- @IBAction func unwindToBalaiUji(segue: UIStoryboardSegue) {}
- }
- // View Delegate Protocols
- extension BalaiUjiVC: BalaiUjiViewDelegate {
-
- func showNoConnection() {
- let alertNoConnection = UIAlertController(
- title: alertNoConnectionTitle,
- message: alertNoConnectionMsg,
- preferredStyle: .alert
- )
- alertNoConnection.addAction(
- UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
- )
- self.present(alertNoConnection, animated: true, completion: nil)
- }
-
- func showLoading() {
- loading = true
- progressLoading.show()
- }
-
- func hideLoading() {
- loading = false
- progressLoading.hide()
- }
-
- func showNoDataLayout() {
- if page == 1 {
- noDataLayout.show()
- }
- }
-
- func hideNoDataLayout() {
- noDataLayout.hide()
- }
-
- func setDataList(dataList: [BalaiUji]) {
-
- if page == 1 {
- noDataLayout.hide()
- }
-
- if dataList.count > 0 {
-
- for data in dataList {
- self.dataList.append(data)
- balaiUjiTV.beginUpdates()
- balaiUjiTV.insertRows(
- at: [IndexPath(row: self.dataList.count-1, section: 0)],
- with: .none
- )
- balaiUjiTV.endUpdates()
- }
- page += 1
- loadMore = true
- } else {
- lastDataLoadedEmpty = true
- loadMore = false
- }
- }
- }
- // UITableController Protocols
- extension BalaiUjiVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, BalaiUjiCellDelegate {
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return dataList.count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- if tableView == balaiUjiTV {
- if let cell = tableView.dequeueReusableCell(withIdentifier: balaiUjiCellId, for: indexPath) as? BalaiUjiCell {
-
- let balaiUji = dataList[indexPath.row]
- cell.cellDelegate = self
- cell.btnWebsite.tag = indexPath.row
- cell.updateViews(balaiUji: balaiUji)
-
- return cell
- } else {
- return BalaiUjiCell()
- }
- } else {
- return BalaiUjiCell()
- }
- }
-
- func scrollViewDidScroll(_ scrollView: UIScrollView) {
- let threshold = -5
- let contentOffset = scrollView.contentOffset.y
- let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
-
- if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
- loadData()
- }
- }
-
- func openWeb(urlWeb: String?) {
- if let urlWeb = urlWeb {
- if let urlPage = URL(string: urlWeb) {
- if UIApplication.shared.canOpenURL(urlPage) {
- if #available(iOS 10.0, *) {
- UIApplication.shared.open(urlPage)
- } else {
- UIApplication.shared.openURL(urlPage)
- }
- }
- }
- }
- }
-
- }
- // SearchBalaiUji Delegate Protocols
- extension BalaiUjiVC: SearchBalaiUjiDelegate {
- func onSearch(search: String, parameter: String, jenis: String, jenisLabel: String, valueLabel: String) {
- presenter.attachView(view: self)
-
- loadMore = false
- loading = false
- lastDataLoadedEmpty = false
-
- self.limit = 5
- self.page = 1
- self.search = search
- self.jenis = jenis
- // self.status = status
-
- dataList.removeAll()
- balaiUjiTV.reloadData()
- lblSubtitle.text = jenisLabel
-
- if valueLabel == "LN" {
- self.btnDownloadLuarNegeriData.show()
- } else {
- self.btnDownloadLuarNegeriData.hide()
- }
-
- loadData()
- }
-
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if segue.identifier == searchSegueId {
- let destinationVC: SearchBalaiUjiVC = segue.destination as! SearchBalaiUjiVC
- destinationVC.delegate = self
- }
- }
- }
- extension BalaiUjiVC: UIDocumentInteractionControllerDelegate {
- func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
- guard let navVC = self.navigationController else {
- return self
- }
- return navVC
- }
- }
|