//
// NotificationViewController.swift
// Service Provider
//
// Created by Mac HD on 01/02/2023.
//
import UIKit
import Firebase
import FirebaseFirestore
import FirebaseAuth
import SVProgressHUD
class NotificationViewController: UIViewController {
@IBOutlet weak var notificationsTableView: UITableView!
@IBOutlet weak var noNotificationsView: UIImageView!
var notifications = [NotificationModel]()
var arrangedNotifications = [NotificationModel]()
override func viewDidLoad() {
super.viewDidLoad()
notificationsTableView.delegate = self
notificationsTableView.dataSource = self
getNotifications()
}
func getNotifications() {
let uid = Auth.auth().currentUser?.uid ?? ""
let db = Firestore.firestore()
db.collection("Notifications")
.whereField("receiverId", isEqualTo: uid)
.order(by: "timestamp", descending: true).addSnapshotListener { querySnapShot, error in
if error != nil {
print("Error : ", error as Any)
return
}
self.notifications.removeAll()
var previousDate = ""
var count = 0
for document in querySnapShot!.documents {
count += 1
var n = NotificationModel()
let date = document.get("timestamp") as? Int ?? 0
let d = String(date)
let newDate = self.getDateTime(d, "d MMM, yyyy")
if newDate != previousDate {
let notification = NotificationModel()
notification.date = newDate
previousDate = newDate
self.notifications.append(notification)
n.date = ""
n.title = document.get("title") as? String ?? ""
n.body = document.get("body") as? String ?? ""
// notification.time = document.documentID
self.notifications.append(n)
} else {
n.title = document.get("title") as? String ?? ""
n.body = document.get("body") as? String ?? ""
self.notifications.append(n)
}
}
if self.notifications.count == 0 {
self.noNotificationsView.isHidden = false
} else {
self.noNotificationsView.isHidden = true
}
print("an count3 : ", self.notifications.count)
self.notificationsTableView.reloadData()
}
}
}
//MARK: - TableView
extension NotificationViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("an count453 : ", arrangedNotifications.count)
return notifications.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let notification = notifications[indexPath.row]
let notificationDate = notifications[indexPath.row].date
print("n date :", notificationDate)
if notification.date == "" {
let cell = notificationsTableView.dequeueReusableCell(withIdentifier: "NotificationTableViewCell", for: indexPath) as! NotificationTableViewCell
cell.selectionStyle = .none
cell.title.text = notification.title
cell.bodyLabel.text = notification.body
// cell.notificationLabel.sizeToFit()
print("doc34 id : ", notification.date)
return cell
} else {
let cell = notificationsTableView.dequeueReusableCell(withIdentifier: "DateTableViewCell", for: indexPath) as! DateTableViewCell
cell.dateLabel.text = notification.date
cell.selectionStyle = .none
print("date423 : ", notification.date)
return cell
}
}
}