FaqVC.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. ConnectionHelper.checkIpAddress { [weak self] success in
  75. guard let self = self else { return }
  76. DispatchQueue.main.async {
  77. if success {
  78. self.presenter.loadDataCategory()
  79. } else {
  80. CommonHelper.showNotAllowConnection(in: self)
  81. }
  82. }
  83. }
  84. } else {
  85. showNoConnection()
  86. }
  87. }
  88. private func loadDataContents(categoryId: Int) {
  89. if ConnectionHelper.isConnectedToNetwork() {
  90. if !self.faqContents.isEmpty {
  91. self.faqContents.removeAll()
  92. tvFaqContent.reloadData()
  93. }
  94. ConnectionHelper.checkIpAddress { [weak self] success in
  95. guard let self = self else { return }
  96. DispatchQueue.main.async {
  97. if success {
  98. self.presenter.loadDataContent(faq_cat_id: categoryId)
  99. } else {
  100. CommonHelper.showNotAllowConnection(in: self)
  101. }
  102. }
  103. }
  104. } else {
  105. showNoConnection()
  106. }
  107. }
  108. }
  109. extension FaqVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  110. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  111. if collectionView == cvFaqCategories {
  112. return faqCategories.count
  113. } else {
  114. return 0
  115. }
  116. }
  117. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  118. if collectionView == cvFaqCategories {
  119. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "faqCategoryCellId", for: indexPath) as? FaqCategoryCell {
  120. cell.set(faqCategory: self.faqCategories[indexPath.row])
  121. return cell
  122. } else {
  123. return FaqCategoryCell()
  124. }
  125. } else {
  126. return FaqCategoryCell()
  127. }
  128. }
  129. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  130. if collectionView == cvFaqCategories {
  131. for i in 0...faqCategories.count-1 {
  132. faqCategories[i].updateSelected(selectedValue: false)
  133. }
  134. faqCategories[indexPath.row].updateSelected(selectedValue: true)
  135. lblCategoryName.text = faqCategories[indexPath.row].faq_cat_name
  136. cvFaqCategories.reloadData()
  137. self.loadDataContents(categoryId: faqCategories[indexPath.row].faq_cat_id)
  138. }
  139. }
  140. }
  141. // UITableController Protocols
  142. extension FaqVC: UITableViewDelegate, UITableViewDataSource {
  143. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  144. return faqContents.count
  145. }
  146. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  147. if tableView == tvFaqContent {
  148. if let cell = tableView.dequeueReusableCell(withIdentifier: "faqContentCellId", for: indexPath) as? FaqContentCell {
  149. let data = faqContents[indexPath.row]
  150. cell.updateViews(faqContent: data)
  151. return cell
  152. } else {
  153. return FaqContentCell()
  154. }
  155. } else {
  156. return FaqContentCell()
  157. }
  158. }
  159. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  160. if tableView == tvFaqContent {
  161. selectedFaqContent = faqContents[indexPath.row]
  162. performSegue(withIdentifier: "FaqDetailSegueId", sender: self)
  163. }
  164. }
  165. }
  166. // View Delegate Protocols
  167. extension FaqVC: FaqViewDelegate {
  168. func showNoConnection() {
  169. let alertNoConnection = UIAlertController(
  170. title: alertNoConnectionTitle,
  171. message: alertNoConnectionMsg,
  172. preferredStyle: .alert
  173. )
  174. alertNoConnection.addAction(
  175. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  176. )
  177. self.present(alertNoConnection, animated: true, completion: nil)
  178. }
  179. func showLoading() {
  180. loading = true
  181. progress.startAnimating()
  182. progressLayout.show()
  183. }
  184. func hideLoading() {
  185. loading = false
  186. progress.stopAnimating()
  187. progressLayout.hide()
  188. }
  189. func setFaqCategoryList(faqCategoryList: [FaqCategory]) {
  190. self.faqCategories.removeAll()
  191. if faqCategoryList.count > 0 {
  192. for faqCategory in faqCategoryList {
  193. self.faqCategories.append(faqCategory)
  194. cvFaqCategories.reloadData()
  195. }
  196. faqCategories[0].updateSelected(selectedValue: true)
  197. lblCategoryName.text = faqCategories[0].faq_cat_name
  198. self.loadDataContents(categoryId: faqCategories[0].faq_cat_id)
  199. }
  200. }
  201. func setFaqContentList(faqContentList: [FaqContent]) {
  202. if faqContentList.count > 0 {
  203. for faqContent in faqContentList {
  204. self.faqContents.append(faqContent)
  205. tvFaqContent.beginUpdates()
  206. tvFaqContent.insertRows(
  207. at: [IndexPath(row: self.faqContents.count-1, section: 0)],
  208. with: .none
  209. )
  210. tvFaqContent.endUpdates()
  211. }
  212. }
  213. }
  214. }