// // NewsVC.swift // Sirani // // Created by Anhar Solehudin on 06/04/19. // Copyright © 2019 Anhar Solehudin. All rights reserved. // import UIKit class NewsVC: UIViewController { // Outlets @IBOutlet weak var newsTV: UITableView! @IBOutlet weak var layoutNoData: UIView! @IBOutlet weak var progressLoading: UIView! // Segue private let unwindToHomeId = "unwindNewsToHomeSegueId" private let detailSegueId = "newsDetailSegueId" private let searchSegueId = "searchNewsSegueId" // View Id private let newsCellId = "NewsListCellId" // Constant Values private let alertNoConnectionTitle = "Informasi" private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet" private let alertNoConnectionActionYes = "OK" // Initialize private var presenter: NewsPresenter! private var dataList = [News]() // Request Params private var jenis: String = "" private var columns: String = "" private var keyword: String = "" private var cursor: Int = 5 private var prev: Int = 1 // Others private var loading = false private var lastDataLoadedEmpty = false private var selectedNewsId = 0 private var date = "" private var judul = "" private var urlThumb = "" private var loadMore = false // View Lifecycles override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } override func viewDidLoad() { super.viewDidLoad() newsTV.delegate = self newsTV.dataSource = self let footer = UIView() footer.backgroundColor = #colorLiteral(red: 0.9814968705, green: 0.9760531783, blue: 1, alpha: 1) newsTV.tableFooterView = footer presenter = NewsPresenter(siraniApiServices: SiraniApiServices()) presenter.attachView(view: self) loadData() } override func viewWillAppear(_ animated: Bool) { presenter.attachView(view: self) } override func viewDidDisappear(_ animated: Bool) { presenter.detachView() } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let detailsVC = segue.destination as? NewsDetailVC { detailsVC.newsId = selectedNewsId detailsVC.date = date detailsVC.judul = judul detailsVC.urlThumb = urlThumb.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! detailsVC.segueId = detailsVC.SEGUE_NEWS_LIST_ID } if let searchVC = segue.destination as? SearchNewsVC { searchVC.delegate = self } } // private functions private func loadData() { if !loading && !lastDataLoadedEmpty { if ConnectionHelper.isConnectedToNetwork() { presenter.loadData(cursor: cursor, prev: prev, jenis: jenis, columns: columns, keyword: keyword) } else { showNoConnection() } } } // Actions @IBAction func onBtnBackTap(_ sender: Any) { performSegue(withIdentifier: unwindToHomeId, sender: self) } @IBAction func onBtnSearchTap(_ sender: Any) { performSegue(withIdentifier: searchSegueId, sender: self) } @IBAction func unwindToNewsList(segue: UIStoryboardSegue) {} } // News View Delegate extension NewsVC: NewsViewDelegate { func showNoConnection() { let alertNoConnection = UIAlertController( title: alertNoConnectionTitle, message: alertNoConnectionMsg, preferredStyle: .alert ) alertNoConnection.addAction( UIAlertAction(title: alertNoConnectionActionYes, style: .default, handler: nil) ) self.present(alertNoConnection, animated: true, completion: nil) } func showLoading() { loading = true progressLoading.show() } func hideLoading() { loading = false progressLoading.hide() } func showNoDataLayout() { if cursor == 0 { layoutNoData.show() } } func hideNoDataLayout() { layoutNoData.hide() } func setDataList(dataList: [News]) { if cursor == 0 { layoutNoData.hide() } if dataList.count > 0 { for data in dataList { self.dataList.append(data) newsTV.beginUpdates() newsTV.insertRows( at: [IndexPath(row: self.dataList.count-1, section: 0)], with: .none ) newsTV.endUpdates() } // let totalData = dataList.count prev += 1 loadMore = true } else { if(self.dataList.count == 0) { layoutNoData.show() } lastDataLoadedEmpty = true loadMore = false } } } // Table View Delegate extension NewsVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataList.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView == newsTV { if let cell = tableView.dequeueReusableCell(withIdentifier: newsCellId, for: indexPath) as? NewsCell { let news = dataList[indexPath.row] cell.updateViews(news: news) return cell } else { return NewsCell() } } else { return NewsCell() } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let selectedNews = dataList[indexPath.row] selectedNewsId = selectedNews.id date = selectedNews.dateNew() judul = selectedNews.title_news urlThumb = selectedNews.thumb_filename performSegue(withIdentifier: detailSegueId, sender: self) } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 140 } func scrollViewDidScroll(_ scrollView: UIScrollView) { let threshold = -5 let contentOffset = scrollView.contentOffset.y let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height; if loadMore && !loading && (Int(maximumOffset - contentOffset) <= threshold) { loadData() } } } // Search Delegate Protocols extension NewsVC: SearchNewsDelegate { func onSearch(search: String, parameter: String, jenis: String) { presenter.attachView(view: self) loadMore = false loading = false lastDataLoadedEmpty = false self.cursor = 5 self.prev = 1 self.keyword = search self.jenis = jenis self.columns = parameter dataList.removeAll() newsTV.reloadData() loadData() } }