NewsVC.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. ConnectionHelper.checkIpAddress { [weak self] success in
  79. guard let self = self else { return }
  80. DispatchQueue.main.async {
  81. if success {
  82. self.presenter.loadData(cursor: self.cursor, prev: self.prev, jenis: self.jenis, columns: self.columns, keyword: self.keyword)
  83. } else {
  84. CommonHelper.showNotAllowConnection(in: self)
  85. }
  86. }
  87. }
  88. } else {
  89. showNoConnection()
  90. }
  91. }
  92. }
  93. // Actions
  94. @IBAction func onBtnBackTap(_ sender: Any) {
  95. performSegue(withIdentifier: unwindToHomeId, sender: self)
  96. }
  97. @IBAction func onBtnSearchTap(_ sender: Any) {
  98. performSegue(withIdentifier: searchSegueId, sender: self)
  99. }
  100. @IBAction func unwindToNewsList(segue: UIStoryboardSegue) {}
  101. }
  102. // News View Delegate
  103. extension NewsVC: NewsViewDelegate {
  104. func showNoConnection() {
  105. let alertNoConnection = UIAlertController(
  106. title: alertNoConnectionTitle,
  107. message: alertNoConnectionMsg,
  108. preferredStyle: .alert
  109. )
  110. alertNoConnection.addAction(
  111. UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil)
  112. )
  113. self.present(alertNoConnection, animated: true, completion: nil)
  114. }
  115. func showLoading() {
  116. loading = true
  117. progressLoading.show()
  118. }
  119. func hideLoading() {
  120. loading = false
  121. progressLoading.hide()
  122. }
  123. func showNoDataLayout() {
  124. if cursor == 0 {
  125. layoutNoData.show()
  126. }
  127. }
  128. func hideNoDataLayout() {
  129. layoutNoData.hide()
  130. }
  131. func setDataList(dataList: [News]) {
  132. if cursor == 0 {
  133. layoutNoData.hide()
  134. }
  135. if dataList.count > 0 {
  136. for data in dataList {
  137. self.dataList.append(data)
  138. newsTV.beginUpdates()
  139. newsTV.insertRows(
  140. at: [IndexPath(row: self.dataList.count-1, section: 0)],
  141. with: .none
  142. )
  143. newsTV.endUpdates()
  144. }
  145. // let totalData = dataList.count
  146. prev += 1
  147. loadMore = true
  148. } else {
  149. if(self.dataList.count == 0)
  150. {
  151. layoutNoData.show()
  152. }
  153. lastDataLoadedEmpty = true
  154. loadMore = false
  155. }
  156. }
  157. }
  158. // Table View Delegate
  159. extension NewsVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
  160. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  161. return dataList.count
  162. }
  163. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  164. if tableView == newsTV {
  165. if let cell = tableView.dequeueReusableCell(withIdentifier: newsCellId, for: indexPath) as? NewsCell {
  166. let news = dataList[indexPath.row]
  167. cell.updateViews(news: news)
  168. return cell
  169. } else {
  170. return NewsCell()
  171. }
  172. } else {
  173. return NewsCell()
  174. }
  175. }
  176. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  177. let selectedNews = dataList[indexPath.row]
  178. selectedNewsId = selectedNews.id
  179. date = selectedNews.dateNew()
  180. judul = selectedNews.title_news
  181. urlThumb = selectedNews.thumb_filename
  182. performSegue(withIdentifier: detailSegueId, sender: self)
  183. }
  184. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  185. return 140
  186. }
  187. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  188. let threshold = -5
  189. let contentOffset = scrollView.contentOffset.y
  190. let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  191. if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) {
  192. loadData()
  193. }
  194. }
  195. }
  196. // Search Delegate Protocols
  197. extension NewsVC: SearchNewsDelegate {
  198. func onSearch(search: String, parameter: String, jenis: String) {
  199. presenter.attachView(view: self)
  200. loadMore = false
  201. loading = false
  202. lastDataLoadedEmpty = false
  203. self.cursor = 5
  204. self.prev = 1
  205. self.keyword = search
  206. self.jenis = jenis
  207. self.columns = parameter
  208. dataList.removeAll()
  209. newsTV.reloadData()
  210. loadData()
  211. }
  212. }