| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- //
- // 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() {
- ConnectionHelper.checkIpAddress { [weak self] success in
- guard let self = self else { return }
- DispatchQueue.main.async {
- if success {
- self.presenter.loadDataCategory()
- } else {
- CommonHelper.showNotAllowConnection(in: self)
- }
- }
- }
- } else {
- showNoConnection()
- }
- }
-
- private func loadDataContents(categoryId: Int) {
- if ConnectionHelper.isConnectedToNetwork() {
- if !self.faqContents.isEmpty {
- self.faqContents.removeAll()
- tvFaqContent.reloadData()
- }
-
- ConnectionHelper.checkIpAddress { [weak self] success in
- guard let self = self else { return }
- DispatchQueue.main.async {
- if success {
- self.presenter.loadDataContent(faq_cat_id: categoryId)
- } else {
- CommonHelper.showNotAllowConnection(in: self)
- }
- }
- }
-
- } 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()
- }
- }
- }
- }
|