| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- //
- // NotificationListVC.swift
- // Sirani
- //
- // Created by Anhar Solehudin on 10/11/20.
- // Copyright © 2020 Anhar Solehudin. All rights reserved.
- //
- import UIKit
- import SkeletonView
- 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
- notifListTV.isSkeletonable = true
-
- 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() {
- self.presenter.loadData(cursor: self.cursor, prev: self.prev)
-
- } 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
- if dataList.isEmpty {
- DispatchQueue.main.async {
- self.view.layoutIfNeeded()
- self.notifListTV.showAnimatedGradientSkeleton()
- }
- } else {
- progress.startAnimating()
- progressLayout.show()
- }
- }
-
- func hideLoading() {
- loading = false
- notifListTV.hideSkeleton()
- 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 {
- self.dataList.append(contentsOf: dataList)
- notifListTV.reloadData()
-
- 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()
- }
- }
- }
- extension NotificationListVC: SkeletonTableViewDataSource {
- func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
- return cellId
- }
- }
|