NewsVC.swift 7.1 KB

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