// // FaqVC.swift // Sirani // // Created by Anhar Solehudin on 24/03/19. // Copyright © 2019 Anhar Solehudin. All rights reserved. // import UIKit import WebKit class FaqVC: UIViewController { @IBOutlet weak var cvFaqCategories: UICollectionView! @IBOutlet weak var lblCategoryName: UILabel! @IBOutlet weak var tvFaqContent: UITableView! @IBOutlet weak var progressLayout: UIView! @IBOutlet weak var progress: UIActivityIndicatorView! // 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: FaqPresenter! private var faqCategories: [FaqCategory] = [FaqCategory]() private var faqContents: [FaqContent] = [FaqContent]() private var selectedFaqContent: FaqContent? = nil // others private var loading = false // View Lifecycles override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } override func viewDidLoad() { super.viewDidLoad() // init collection view let flowLayout = UICollectionViewFlowLayout() flowLayout.sectionInset = .init(top: 0, left: 16, bottom: 0, right: 16) flowLayout.itemSize = CGSize(width: 120, height: 48) flowLayout.minimumInteritemSpacing = 0 flowLayout.minimumLineSpacing = 8 flowLayout.scrollDirection = .horizontal cvFaqCategories.backgroundColor = .white cvFaqCategories.collectionViewLayout = flowLayout cvFaqCategories.delegate = self cvFaqCategories.dataSource = self cvFaqCategories.register(FaqCategoryCell.self, forCellWithReuseIdentifier: "faqCategoryCellId") // init table view tvFaqContent.delegate = self tvFaqContent.dataSource = self // load data presenter = FaqPresenter(siraniApiServices: SiraniApiServices()) presenter.attachView(view: self) loadDataCategories() } override func viewWillAppear(_ animated: Bool) { AppUtility.lockOrientation(.portrait) presenter.attachView(view: self) } override func viewDidAppear(_ animated: Bool) { } override func viewWillDisappear(_ animated: Bool) { AppUtility.lockOrientation(.all) presenter.detachView() } override func viewDidDisappear(_ animated: Bool) { } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let detailsVC = segue.destination as? FaqDetailVC { detailsVC.faqContent = selectedFaqContent } } @IBAction func unwindToFaqList(segue: UIStoryboardSegue) {} // private functions private func loadDataCategories() { if ConnectionHelper.isConnectedToNetwork() { self.presenter.loadDataCategory() } else { showNoConnection() } } private func loadDataContents(categoryId: Int) { if ConnectionHelper.isConnectedToNetwork() { if !self.faqContents.isEmpty { self.faqContents.removeAll() tvFaqContent.reloadData() } self.presenter.loadDataContent(faq_cat_id: categoryId) } else { showNoConnection() } } } extension FaqVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if collectionView == cvFaqCategories { return faqCategories.count } else { return 0 } } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if collectionView == cvFaqCategories { if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "faqCategoryCellId", for: indexPath) as? FaqCategoryCell { cell.set(faqCategory: self.faqCategories[indexPath.row]) return cell } else { return FaqCategoryCell() } } else { return FaqCategoryCell() } } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { if collectionView == cvFaqCategories { for i in 0...faqCategories.count-1 { faqCategories[i].updateSelected(selectedValue: false) } faqCategories[indexPath.row].updateSelected(selectedValue: true) lblCategoryName.text = faqCategories[indexPath.row].faq_cat_name cvFaqCategories.reloadData() self.loadDataContents(categoryId: faqCategories[indexPath.row].faq_cat_id) } } } // UITableController Protocols extension FaqVC: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return faqContents.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView == tvFaqContent { if let cell = tableView.dequeueReusableCell(withIdentifier: "faqContentCellId", for: indexPath) as? FaqContentCell { let data = faqContents[indexPath.row] cell.updateViews(faqContent: data) return cell } else { return FaqContentCell() } } else { return FaqContentCell() } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if tableView == tvFaqContent { selectedFaqContent = faqContents[indexPath.row] performSegue(withIdentifier: "FaqDetailSegueId", sender: self) } } } // View Delegate Protocols extension FaqVC: FaqViewDelegate { 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 progress.startAnimating() progressLayout.show() } func hideLoading() { loading = false progress.stopAnimating() progressLayout.hide() } func setFaqCategoryList(faqCategoryList: [FaqCategory]) { self.faqCategories.removeAll() if faqCategoryList.count > 0 { for faqCategory in faqCategoryList { self.faqCategories.append(faqCategory) cvFaqCategories.reloadData() } faqCategories[0].updateSelected(selectedValue: true) lblCategoryName.text = faqCategories[0].faq_cat_name self.loadDataContents(categoryId: faqCategories[0].faq_cat_id) } } func setFaqContentList(faqContentList: [FaqContent]) { if faqContentList.count > 0 { for faqContent in faqContentList { self.faqContents.append(faqContent) tvFaqContent.beginUpdates() tvFaqContent.insertRows( at: [IndexPath(row: self.faqContents.count-1, section: 0)], with: .none ) tvFaqContent.endUpdates() } } } }