@ -13,11 +13,16 @@ class NotificationsViewModel extends ChangeNotifier {
ErrorHandlerService errorHandlerService ;
List < NotificationResponseModel > notificationsList = [ ] ;
List < NotificationResponseModel > readdNotificationsList = [ ] ;
List < NotificationResponseModel > unreadNotificationsList = [ ] ;
List < NotificationResponseModel > allNotificationsList = [ ] ;
int currentPage = 0 ;
int pagingSize = 50 ;
int notificationStatusID = 2 ; / / Default to status 2 ( e . g . , unread )
int selectedNotificationsTabIndex = 0 ;
NotificationsViewModel ( {
required this . notificationsRepo ,
required this . errorHandlerService ,
@ -35,6 +40,18 @@ class NotificationsViewModel extends ChangeNotifier {
notifyListeners ( ) ;
}
void onNotificationTabChange ( int index ) {
selectedNotificationsTabIndex = index ;
if ( index = = 0 ) {
notificationsList = allNotificationsList ;
} else if ( index = = 1 ) {
notificationsList = unreadNotificationsList ;
} else if ( index = = 2 ) {
notificationsList = readdNotificationsList ;
}
notifyListeners ( ) ;
}
setNotificationStatusID ( int statusID ) {
notificationStatusID = statusID ;
notificationsList . clear ( ) ;
@ -49,6 +66,9 @@ class NotificationsViewModel extends ChangeNotifier {
} ) async {
isNotificationsLoading = true ;
unreadNotificationsCount = 0 ;
notificationsList . clear ( ) ;
notifyListeners ( ) ;
final result = await notificationsRepo . getAllNotifications (
@ -80,17 +100,37 @@ class NotificationsViewModel extends ChangeNotifier {
hasMoreNotifications = false ;
}
notificationsList . addAll ( newNotifications ) ;
for ( var notification in notificationsList ) {
if ( notification . isRead = = false ) {
unreadNotificationsCount + + ;
try {
notificationsList . addAll ( newNotifications ) ;
/ / allNotificationsList . clear ( ) ;
/ / readdNotificationsList . clear ( ) ;
/ / unreadNotificationsList . clear ( ) ;
/ / for ( var notification in notificationsList ) {
/ / if ( notification . isRead = = false ) {
/ / unreadNotificationsCount + + ;
/ / unreadNotificationsList . add ( notification ) ;
/ / } else {
/ / readdNotificationsList . add ( notification ) ;
/ / }
/ / }
/ / Filter out the items directly without a loop
unreadNotificationsList = notificationsList . where ( ( n ) = > n . isRead = = false ) . toList ( ) ;
readdNotificationsList = notificationsList . where ( ( n ) = > n . isRead = = true ) . toList ( ) ;
/ / Calculate count based on the new list length
unreadNotificationsCount = unreadNotificationsList . length ;
currentPage + + ;
allNotificationsList = notificationsList ;
notifyListeners ( ) ;
if ( onSuccess ! = null ) {
onSuccess ( apiResponse ) ;
}
}
currentPage + + ;
notifyListeners ( ) ;
if ( onSuccess ! = null ) {
onSuccess ( apiResponse ) ;
} catch ( ex ) {
print ( ex . toString ( ) ) ;
}
}
} ,