NewsVC.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // NewsVC.swift
  3. // Sirani
  4. //
  5. // Created by Anhar Solehudin on 06/04/19.
  6. // Copyright © 2019 Anhar Solehudin. All rights reserved.
  7. //
  8. import UIKit
  9. import SkeletonView
  10. class NewsVC: UIViewController {
  11. // Outlets
  12. @IBOutlet weak var newsTV: UITableView!
  13. @IBOutlet weak var layoutNoData: UIView!
  14. @IBOutlet weak var progressLoading: UIView!
  15. // Segue
  16. private let unwindToHomeId = "unwindNewsToHomeSegueId"
  17. private let detailSegueId = "newsDetailSegueId"
  18. private let searchSegueId = "searchNewsSegueId"
  19. // View Id
  20. private let newsCellId = "NewsListCellId"
  21. // Constant Values
  22. private let alertNoConnectionTitle = "Informasi"
  23. private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet"
  24. private let alertNoConnectionActionYes = "OK"
  25. // Initialize
  26. private var presenter: NewsPresenter!
  27. private var dataList = [News]()
  28. // Request Params
  29. private var jenis: String = ""
  30. private var columns: String = ""
  31. private var keyword: String = ""
  32. private var cursor: Int = 5
  33. private var prev: Int = 1
  34. // Others
  35. private var loading = false
  36. private var lastDataLoadedEmpty = false
  37. private var selectedNewsId = 0
  38. private var date = ""
  39. private var judul = ""
  40. private var urlThumb = ""
  41. private var loadMore = false
  42. // View Lifecycles
  43. override var preferredStatusBarStyle: UIStatusBarStyle {
  44. return .lightContent
  45. }
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48. newsTV.delegate = self
  49. newsTV.dataSource = self
  50. newsTV.isSkeletonable = true
  51. newsTV.rowHeight = UITableView.automaticDimension
  52. newsTV.estimatedRowHeight = 120
  53. newsTV.separatorStyle = .none
  54. newsTV.backgroundColor = .groupTableViewBackground
  55. let footer = UIView()
  56. footer.backgroundColor = #colorLiteral(red: 0.9814968705, green: 0.9760531783, blue: 1, alpha: 1)
  57. newsTV.tableFooterView = footer
  58. presenter = NewsPresenter(siraniApiServices: SiraniApiServices())
  59. presenter.attachView(view: self)
  60. loadData()
  61. }
  62. override func viewWillAppear(_ animated: Bool) {
  63. presenter.attachView(view: self)
  64. }
  65. override func viewDidDisappear(_ animated: Bool) {
  66. presenter.detachView()
  67. }
  68. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  69. if let detailsVC = segue.destination as? NewsDetailVC {
  70. detailsVC.newsId = selectedNewsId
  71. detailsVC.date = date
  72. detailsVC.judul = judul
  73. detailsVC.urlThumb = urlThumb.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
  74. detailsVC.segueId = detailsVC.SEGUE_NEWS_LIST_ID
  75. }
  76. if let searchVC = segue.destination as? SearchNewsVC {
  77. searchVC.delegate = self
  78. }
  79. }
  80. // private functions
  81. private func loadData() {
  82. if !loading && !lastDataLoadedEmpty {
  83. if ConnectionHelper.isConnectedToNetwork() {
  84. self.presenter.loadData(cursor: self.cursor, prev: self.prev, jenis: self.jenis, columns: self.columns, keyword: self.keyword)
  85. } else {
  86. showNoConnection()
  87. }
  88. }
  89. }
  90. // Actions
  91. @IBAction func onBtnBackTap(_ sender: Any) {
  92. performSegue(withIdentifier: unwindToHomeId, sender: self)
  93. }
  94. @IBAction func onBtnSearchTap(_ sender: Any) {
  95. performSegue(withIdentifier: searchSegueId, sender: self)
  96. }
  97. @IBAction func unwindToNewsList(segue: UIStoryboardSegue) {}
  98. }
  99. // News View Delegate
  100. extension NewsVC: NewsViewDelegate {
  101. func showNoConnection() {
  102. let alertNoConnection = UIAlertController(
  103. title: alertNoConnectionTitle,
  104. message: alertNoConnectionMsg,
  105. preferredStyle: .alert
  106. )
  107. alertNoConnection.addAction(
  108. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  109. )
  110. self.present(alertNoConnection, animated: true, completion: nil)
  111. }
  112. func showLoading() {
  113. loading = true
  114. if dataList.isEmpty {
  115. DispatchQueue.main.async {
  116. self.view.layoutIfNeeded()
  117. self.newsTV.showAnimatedGradientSkeleton()
  118. }
  119. } else {
  120. progressLoading.show()
  121. }
  122. }
  123. func hideLoading() {
  124. loading = false
  125. newsTV.hideSkeleton()
  126. progressLoading.hide()
  127. }
  128. func showNoDataLayout() {
  129. if cursor == 0 {
  130. layoutNoData.show()
  131. }
  132. }
  133. func hideNoDataLayout() {
  134. layoutNoData.hide()
  135. }
  136. func setDataList(dataList: [News]) {
  137. if cursor == 0 {
  138. layoutNoData.hide()
  139. }
  140. if dataList.count > 0 {
  141. self.dataList.append(contentsOf: dataList)
  142. newsTV.reloadData()
  143. // let totalData = dataList.count
  144. prev += 1
  145. loadMore = true
  146. } else {
  147. if(self.dataList.count == 0)
  148. {
  149. layoutNoData.show()
  150. }
  151. lastDataLoadedEmpty = true
  152. loadMore = false
  153. }
  154. }
  155. }
  156. // Table View Delegate
  157. extension NewsVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
  158. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  159. return dataList.count
  160. }
  161. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  162. if tableView == newsTV {
  163. if let cell = tableView.dequeueReusableCell(withIdentifier: newsCellId, for: indexPath) as? NewsCell {
  164. let news = dataList[indexPath.row]
  165. cell.updateViews(news: news)
  166. return cell
  167. } else {
  168. return NewsCell()
  169. }
  170. } else {
  171. return NewsCell()
  172. }
  173. }
  174. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  175. let selectedNews = dataList[indexPath.row]
  176. selectedNewsId = selectedNews.id
  177. date = selectedNews.dateNew()
  178. judul = selectedNews.title_news
  179. urlThumb = selectedNews.thumb_filename
  180. performSegue(withIdentifier: detailSegueId, sender: self)
  181. }
  182. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  183. return 140
  184. }
  185. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  186. let threshold = -5
  187. let contentOffset = scrollView.contentOffset.y
  188. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  189. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  190. loadData()
  191. }
  192. }
  193. }
  194. // Search Delegate Protocols
  195. extension NewsVC: SearchNewsDelegate {
  196. func onSearch(search: String, parameter: String, jenis: String) {
  197. presenter.attachView(view: self)
  198. loadMore = false
  199. loading = false
  200. lastDataLoadedEmpty = false
  201. self.cursor = 5
  202. self.prev = 1
  203. self.keyword = search
  204. self.jenis = jenis
  205. self.columns = parameter
  206. dataList.removeAll()
  207. newsTV.reloadData()
  208. loadData()
  209. }
  210. }
  211. extension NewsVC: SkeletonTableViewDataSource {
  212. func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
  213. return newsCellId
  214. }
  215. }