FaqVC.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // FaqVC.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 24/03/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. class FaqVC: UIViewController {
  11. @IBOutlet weak var cvFaqCategories: UICollectionView!
  12. @IBOutlet weak var lblCategoryName: UILabel!
  13. @IBOutlet weak var tvFaqContent: UITableView!
  14. @IBOutlet weak var progressLayout: UIView!
  15. @IBOutlet weak var progress: UIActivityIndicatorView!
  16. // String Message
  17. private let alertNoConnectionTitle = "Informasi"
  18. private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet"
  19. private let alertNoConnectionActionYes = "OK"
  20. // Initialize
  21. private var presenter: FaqPresenter!
  22. private var faqCategories: [FaqCategory] = [FaqCategory]()
  23. private var faqContents: [FaqContent] = [FaqContent]()
  24. private var selectedFaqContent: FaqContent? = nil
  25. // others
  26. private var loading = false
  27. // View Lifecycles
  28. override var preferredStatusBarStyle: UIStatusBarStyle {
  29. return .lightContent
  30. }
  31. override func viewDidLoad() {
  32. super.viewDidLoad()
  33. // init collection view
  34. let flowLayout = UICollectionViewFlowLayout()
  35. flowLayout.sectionInset = .init(top: 0, left: 16, bottom: 0, right: 16)
  36. flowLayout.itemSize = CGSize(width: 120, height: 48)
  37. flowLayout.minimumInteritemSpacing = 0
  38. flowLayout.minimumLineSpacing = 8
  39. flowLayout.scrollDirection = .horizontal
  40. cvFaqCategories.backgroundColor = .white
  41. cvFaqCategories.collectionViewLayout = flowLayout
  42. cvFaqCategories.delegate = self
  43. cvFaqCategories.dataSource = self
  44. cvFaqCategories.register(FaqCategoryCell.self, forCellWithReuseIdentifier: "faqCategoryCellId")
  45. // init table view
  46. tvFaqContent.delegate = self
  47. tvFaqContent.dataSource = self
  48. // load data
  49. presenter = FaqPresenter(siraniApiServices: SiraniApiServices())
  50. presenter.attachView(view: self)
  51. loadDataCategories()
  52. }
  53. override func viewWillAppear(_ animated: Bool) {
  54. AppUtility.lockOrientation(.portrait)
  55. presenter.attachView(view: self)
  56. }
  57. override func viewDidAppear(_ animated: Bool) {
  58. }
  59. override func viewWillDisappear(_ animated: Bool) {
  60. AppUtility.lockOrientation(.all)
  61. presenter.detachView()
  62. }
  63. override func viewDidDisappear(_ animated: Bool) {
  64. }
  65. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  66. if let detailsVC = segue.destination as? FaqDetailVC {
  67. detailsVC.faqContent = selectedFaqContent
  68. }
  69. }
  70. @IBAction func unwindToFaqList(segue: UIStoryboardSegue) {}
  71. // private functions
  72. private func loadDataCategories() {
  73. if ConnectionHelper.isConnectedToNetwork() {
  74. self.presenter.loadDataCategory()
  75. } else {
  76. showNoConnection()
  77. }
  78. }
  79. private func loadDataContents(categoryId: Int) {
  80. if ConnectionHelper.isConnectedToNetwork() {
  81. if !self.faqContents.isEmpty {
  82. self.faqContents.removeAll()
  83. tvFaqContent.reloadData()
  84. }
  85. self.presenter.loadDataContent(faq_cat_id: categoryId)
  86. } else {
  87. showNoConnection()
  88. }
  89. }
  90. }
  91. extension FaqVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  92. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  93. if collectionView == cvFaqCategories {
  94. return faqCategories.count
  95. } else {
  96. return 0
  97. }
  98. }
  99. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  100. if collectionView == cvFaqCategories {
  101. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "faqCategoryCellId", for: indexPath) as? FaqCategoryCell {
  102. cell.set(faqCategory: self.faqCategories[indexPath.row])
  103. return cell
  104. } else {
  105. return FaqCategoryCell()
  106. }
  107. } else {
  108. return FaqCategoryCell()
  109. }
  110. }
  111. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  112. if collectionView == cvFaqCategories {
  113. for i in 0...faqCategories.count-1 {
  114. faqCategories[i].updateSelected(selectedValue: false)
  115. }
  116. faqCategories[indexPath.row].updateSelected(selectedValue: true)
  117. lblCategoryName.text = faqCategories[indexPath.row].faq_cat_name
  118. cvFaqCategories.reloadData()
  119. self.loadDataContents(categoryId: faqCategories[indexPath.row].faq_cat_id)
  120. }
  121. }
  122. }
  123. // UITableController Protocols
  124. extension FaqVC: UITableViewDelegate, UITableViewDataSource {
  125. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  126. return faqContents.count
  127. }
  128. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  129. if tableView == tvFaqContent {
  130. if let cell = tableView.dequeueReusableCell(withIdentifier: "faqContentCellId", for: indexPath) as? FaqContentCell {
  131. let data = faqContents[indexPath.row]
  132. cell.updateViews(faqContent: data)
  133. return cell
  134. } else {
  135. return FaqContentCell()
  136. }
  137. } else {
  138. return FaqContentCell()
  139. }
  140. }
  141. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  142. if tableView == tvFaqContent {
  143. selectedFaqContent = faqContents[indexPath.row]
  144. performSegue(withIdentifier: "FaqDetailSegueId", sender: self)
  145. }
  146. }
  147. }
  148. // View Delegate Protocols
  149. extension FaqVC: FaqViewDelegate {
  150. func showNoConnection() {
  151. let alertNoConnection = UIAlertController(
  152. title: alertNoConnectionTitle,
  153. message: alertNoConnectionMsg,
  154. preferredStyle: .alert
  155. )
  156. alertNoConnection.addAction(
  157. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  158. )
  159. self.present(alertNoConnection, animated: true, completion: nil)
  160. }
  161. func showLoading() {
  162. loading = true
  163. progress.startAnimating()
  164. progressLayout.show()
  165. }
  166. func hideLoading() {
  167. loading = false
  168. progress.stopAnimating()
  169. progressLayout.hide()
  170. }
  171. func setFaqCategoryList(faqCategoryList: [FaqCategory]) {
  172. self.faqCategories.removeAll()
  173. if faqCategoryList.count > 0 {
  174. for faqCategory in faqCategoryList {
  175. self.faqCategories.append(faqCategory)
  176. cvFaqCategories.reloadData()
  177. }
  178. faqCategories[0].updateSelected(selectedValue: true)
  179. lblCategoryName.text = faqCategories[0].faq_cat_name
  180. self.loadDataContents(categoryId: faqCategories[0].faq_cat_id)
  181. }
  182. }
  183. func setFaqContentList(faqContentList: [FaqContent]) {
  184. if faqContentList.count > 0 {
  185. for faqContent in faqContentList {
  186. self.faqContents.append(faqContent)
  187. tvFaqContent.beginUpdates()
  188. tvFaqContent.insertRows(
  189. at: [IndexPath(row: self.faqContents.count-1, section: 0)],
  190. with: .none
  191. )
  192. tvFaqContent.endUpdates()
  193. }
  194. }
  195. }
  196. }