- Add getUnreadCount() endpoint to NotificationsServiceProtocol - Add NotificationUnreadCountResponse model - Add badgeCount and fetchUnreadCount() to NotificationsViewModel - Update markAsRead/markAllAsRead to decrement badge count - Create MainTabView with Home, Challenges, Clubs, Notifications tabs - Add unread badge on notification tab using .badge() modifier - Support injected ViewModel in NotificationsView for shared state - Add badge count tests to NotificationServiceTests - Fetch unread count on app launch and tab switch Co-Authored-By: Paperclip <noreply@paperclip.ing>
83 lines
2.4 KiB
Swift
83 lines
2.4 KiB
Swift
import Foundation
|
|
import SwiftUI
|
|
|
|
@MainActor
|
|
class NotificationsViewModel: ObservableObject {
|
|
@Published var notifications: [NotificationItem] = []
|
|
@Published var isLoading: Bool = false
|
|
@Published var badgeCount: Int = 0
|
|
@Published var lastRefreshDate: Date?
|
|
@Published var error: NotificationError?
|
|
|
|
private let notificationsService: NotificationsServiceProtocol
|
|
|
|
init(notificationsService: NotificationsServiceProtocol = NotificationsService()) {
|
|
self.notificationsService = notificationsService
|
|
}
|
|
|
|
func fetchNotifications() async {
|
|
isLoading = true
|
|
error = nil
|
|
defer {
|
|
isLoading = false
|
|
lastRefreshDate = Date()
|
|
}
|
|
|
|
do {
|
|
let fetchedNotifications = try await notificationsService.list()
|
|
notifications = fetchedNotifications.sorted { $0.createdAt > $1.createdAt }
|
|
badgeCount = notifications.filter { !$0.isRead }.count
|
|
} catch let error as NotificationError {
|
|
self.error = error
|
|
} catch {
|
|
print("Failed to fetch notifications: \(error)")
|
|
}
|
|
}
|
|
|
|
func refresh() async {
|
|
await fetchNotifications()
|
|
}
|
|
|
|
func fetchUnreadCount() async {
|
|
do {
|
|
let count = try await notificationsService.getUnreadCount()
|
|
badgeCount = count
|
|
} catch {
|
|
print("Failed to fetch unread count: \(error)")
|
|
}
|
|
}
|
|
|
|
func markAsRead(id: String) async {
|
|
guard let index = notifications.firstIndex(where: { $0.id == id }) else { return }
|
|
|
|
do {
|
|
try await notificationsService.markAsRead(id: id)
|
|
notifications[index].isRead = true
|
|
badgeCount = max(0, badgeCount - 1)
|
|
objectWillChange.send()
|
|
} catch {
|
|
print("Failed to mark notification as read: \(error)")
|
|
}
|
|
}
|
|
|
|
func markAllAsRead() async {
|
|
let unreadIds = notifications.filter { !$0.isRead }.map { $0.id }
|
|
guard !unreadIds.isEmpty else { return }
|
|
|
|
do {
|
|
try await notificationsService.markAllAsRead()
|
|
for index in notifications.indices {
|
|
notifications[index].isRead = true
|
|
}
|
|
badgeCount = 0
|
|
objectWillChange.send()
|
|
} catch {
|
|
print("Failed to mark all as read: \(error)")
|
|
}
|
|
}
|
|
|
|
var unreadCount: Int {
|
|
notifications.filter { !$0.isRead }.count
|
|
}
|
|
}
|