| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- //
- // NewsVC.swift
- // Sirani
- //
- // Created by Anhar Solehudin on 06/04/19.
- // Copyright © 2019 Anhar Solehudin. All rights reserved.
- //
- import UIKit
- import SkeletonView
- 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
- newsTV.isSkeletonable = true
- newsTV.rowHeight = UITableView.automaticDimension
- newsTV.estimatedRowHeight = 120
- newsTV.separatorStyle = .none
- newsTV.backgroundColor = .groupTableViewBackground
- 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() {
- self.presenter.loadData(cursor: self.cursor, prev: self.prev, jenis: self.jenis, columns: self.columns, keyword: self.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
- if dataList.isEmpty {
- DispatchQueue.main.async {
- self.view.layoutIfNeeded()
- self.newsTV.showAnimatedGradientSkeleton()
- }
- } else {
- progressLoading.show()
- }
- }
-
- func hideLoading() {
- loading = false
- newsTV.hideSkeleton()
- 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 {
- self.dataList.append(contentsOf: dataList)
- newsTV.reloadData()
-
- // 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()
- }
- }
- extension NewsVC: SkeletonTableViewDataSource {
- func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
- return newsCellId
- }
- }
|