mark single notification as read
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
// notification_view_model.dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:tm_app/core/utils/app_toast.dart';
|
||||
import 'package:tm_app/core/utils/result.dart';
|
||||
import 'package:tm_app/data/repositories/notification_repository.dart';
|
||||
@@ -10,9 +9,13 @@ import 'package:tm_app/views/notification/strings/notification_strings.dart';
|
||||
|
||||
class NotificationViewModel with ChangeNotifier {
|
||||
final NotificationsRepository _repository;
|
||||
final HomeViewModel _homeViewModel;
|
||||
|
||||
NotificationViewModel({required NotificationsRepository repositoryViewModel})
|
||||
: _repository = repositoryViewModel {
|
||||
NotificationViewModel({
|
||||
required NotificationsRepository repositoryViewModel,
|
||||
required HomeViewModel homeViewModel,
|
||||
}) : _repository = repositoryViewModel,
|
||||
_homeViewModel = homeViewModel {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
init();
|
||||
});
|
||||
@@ -214,10 +217,10 @@ class NotificationViewModel with ChangeNotifier {
|
||||
void _updateImportantFromAll() {
|
||||
final all = allNotificationResponse?.data ?? [];
|
||||
final filtered =
|
||||
(all.where((n) {
|
||||
all.where((n) {
|
||||
final p = n.priority ?? '';
|
||||
return p.toLowerCase() == 'important';
|
||||
}).toList());
|
||||
}).toList();
|
||||
|
||||
if (allNotificationResponse != null) {
|
||||
importantNotificationResponse = allNotificationResponse!.copyWith(
|
||||
@@ -294,6 +297,9 @@ class NotificationViewModel with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure unread list is empty after marking all as read
|
||||
_checkAndUpdateUnreadState();
|
||||
|
||||
if (context.mounted) {
|
||||
AppToast.success(
|
||||
context,
|
||||
@@ -341,12 +347,100 @@ class NotificationViewModel with ChangeNotifier {
|
||||
}
|
||||
|
||||
void setNotificationFalse(BuildContext context) {
|
||||
final homeViewModel = context.read<HomeViewModel>();
|
||||
homeViewModel.setUnreadNotificationFalse();
|
||||
_homeViewModel.setUnreadNotificationFalse();
|
||||
}
|
||||
|
||||
void checkNotifications(BuildContext context) {
|
||||
final homeViewModel = context.read<HomeViewModel>();
|
||||
homeViewModel.checkUnreadNotifications();
|
||||
void _checkAndUpdateUnreadState() {
|
||||
// Check if all notifications are read
|
||||
final allNotifications = allNotificationResponse?.data ?? [];
|
||||
final hasUnreadNotifications = allNotifications.any((n) => n.seen != true);
|
||||
|
||||
if (!hasUnreadNotifications) {
|
||||
// Clear unread list if all notifications are read
|
||||
unreadNotificationResponse = unreadNotificationResponse?.copyWith(
|
||||
data: [],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
int get unreadCount {
|
||||
return unreadNotificationResponse?.data?.length ?? 0;
|
||||
}
|
||||
|
||||
int get importantCount {
|
||||
return importantNotificationResponse?.data?.length ?? 0;
|
||||
}
|
||||
|
||||
bool get hasUnreadNotifications {
|
||||
return unreadCount > 0;
|
||||
}
|
||||
|
||||
bool get hasImportantNotifications {
|
||||
return importantCount > 0;
|
||||
}
|
||||
|
||||
void checkNotifications() {
|
||||
_homeViewModel.checkUnreadNotifications();
|
||||
}
|
||||
|
||||
Future<void> markAsRead({required String notificationId}) async {
|
||||
final result = await _repository.markAsRead(notificationId: notificationId);
|
||||
|
||||
switch (result) {
|
||||
case Ok<NotificationResponseModel>():
|
||||
// Find and update only the specific notification that matches the ID
|
||||
final updatedNotifications =
|
||||
allNotificationResponse?.data?.map((n) {
|
||||
if (n.id == notificationId) {
|
||||
return n.copyWith(seen: true);
|
||||
}
|
||||
return n;
|
||||
}).toList();
|
||||
|
||||
if (updatedNotifications != null) {
|
||||
allNotificationResponse = allNotificationResponse!.copyWith(
|
||||
data: updatedNotifications,
|
||||
);
|
||||
}
|
||||
|
||||
// Update unread list - remove the notification that was marked as read
|
||||
if (unreadNotificationResponse?.data != null) {
|
||||
final updatedUnreadList =
|
||||
unreadNotificationResponse!.data!
|
||||
.where((n) => n.id != notificationId)
|
||||
.toList();
|
||||
|
||||
unreadNotificationResponse = unreadNotificationResponse!.copyWith(
|
||||
data: updatedUnreadList,
|
||||
);
|
||||
}
|
||||
|
||||
// Update important list - mark the notification as seen if it exists
|
||||
if (importantNotificationResponse?.data != null) {
|
||||
final updatedImportantList =
|
||||
importantNotificationResponse!.data!.map((n) {
|
||||
if (n.id == notificationId) {
|
||||
return n.copyWith(seen: true);
|
||||
}
|
||||
return n;
|
||||
}).toList();
|
||||
|
||||
importantNotificationResponse = importantNotificationResponse!
|
||||
.copyWith(data: updatedImportantList);
|
||||
}
|
||||
|
||||
// Check if all notifications are now read
|
||||
_checkAndUpdateUnreadState();
|
||||
|
||||
notifyListeners();
|
||||
checkNotifications();
|
||||
|
||||
break;
|
||||
case Error<NotificationResponseModel>():
|
||||
_errorMessage = result.error.toString();
|
||||
break;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user