// // NotificationListVC.swift // Sirani // // Created by Anhar Solehudin on 10/11/20. // Copyright © 2020 Anhar Solehudin. All rights reserved. // import UIKit class NotificationListVC: UIViewController { @IBOutlet weak var notifListTV: UITableView! @IBOutlet weak var noDataLayout: UIView! @IBOutlet weak var progressLayout: UIView! @IBOutlet weak var progress: UIActivityIndicatorView! // View Id private let cellId = "notificationCellId" // String Message private let alertNoConnectionTitle = "Informasi" private let alertNoConnectionMsg = "Sepertinya Anda sedang tidak tersambung pada koneksi internet" private let alertNoConnectionActionYes = "OK" // Initialize private var presenter: NotificationListPresenter! private var dataList: [NotificationItem] = [NotificationItem]() // Request Params private var cursor: Int = 0 private var prev: Int = 1 // Others private var loading = false private var lastDataLoadedEmpty = false private var loadMore = false private var selectedNotifId: Int = 0 // View Lifecycles override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } override func viewDidLoad() { super.viewDidLoad() notifListTV.delegate = self notifListTV.dataSource = self presenter = NotificationListPresenter(siraniApiServices: SiraniApiServices()) presenter.attachView(view: self) loadData() } override func viewWillAppear(_ animated: Bool) { AppUtility.lockOrientation(.portrait) presenter.attachView(view: self) } override func viewDidAppear(_ animated: Bool) { } override func viewWillDisappear(_ animated: Bool) { AppUtility.lockOrientation(.all) presenter.detachView() } override func viewDidDisappear(_ animated: Bool) { } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let detailsVC = segue.destination as? NotificationDetailVC { detailsVC.notifId = selectedNotifId } } @IBAction func unwindToNotificationList(segue: UIStoryboardSegue) {} @IBAction func onBackTapped(_ sender: Any) { performSegue(withIdentifier: "unwindNotificationListToHome", sender: self) } // private functions private func loadData() { if !loading && !lastDataLoadedEmpty { if ConnectionHelper.isConnectedToNetwork() { if ConnectionHelper.isConnectedToNetwork() { ConnectionHelper.checkIpAddress { [weak self] success in guard let self = self else { return } DispatchQueue.main.async { if success { self.presenter.loadData(cursor: self.cursor, prev: self.prev) } else { CommonHelper.showNotAllowConnection(in: self) } } } } } else { showNoConnection() } } } } // View Delegate Protocols extension NotificationListVC: NotificationListViewDelegate { 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 progress.startAnimating() progressLayout.show() } func hideLoading() { loading = false progress.stopAnimating() progressLayout.hide() } func showNoDataLayout() { if cursor == 0 { noDataLayout.show() } } func hideNoDataLayout() { noDataLayout.hide() } func setDataList(dataList: [NotificationItem]) { if cursor == 0 { noDataLayout.hide() } if dataList.count > 0 { for data in dataList { self.dataList.append(data) notifListTV.beginUpdates() notifListTV.insertRows( at: [IndexPath(row: self.dataList.count-1, section: 0)], with: .none ) notifListTV.endUpdates() } let totalData = dataList.count cursor += totalData loadMore = true } else { lastDataLoadedEmpty = true loadMore = false } } } // UITableController Protocols extension NotificationListVC: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, NotificationCellDelegate { func openNotifDetail(notificationItem: NotificationItem?, position: Int?) { if notificationItem != nil { selectedNotifId = notificationItem!.log_activity_id if position != nil { self.dataList[position!].updateIsRead(isRead: 1) notifListTV.beginUpdates() notifListTV.reloadRows( at: [IndexPath(row: position!, section: 0)], with: .none ) notifListTV.endUpdates() } performSegue(withIdentifier: "NotificationDetailSegueId", sender: self) } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataList.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView == notifListTV { if let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? NotificationCell { let data = dataList[indexPath.row] cell.cellDelegate = self cell.card.tag = indexPath.row cell.updateViews(notificationItem: data, position: indexPath.row) return cell } else { return NotificationCell() } } else { return NotificationCell() } } 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() } } }