import 'package:flutter/material.dart'; import 'package:hmg_patient_app_new/features/notifications/models/resp_models/notification_response_model.dart'; import 'package:hmg_patient_app_new/features/notifications/notifications_repo.dart'; import 'package:hmg_patient_app_new/services/error_handler_service.dart'; class NotificationsViewModel extends ChangeNotifier { bool isNotificationsLoading = false; bool hasMoreNotifications = true; int unreadNotificationsCount = 0; NotificationsRepo notificationsRepo; ErrorHandlerService errorHandlerService; List notificationsList = []; List readdNotificationsList = []; List unreadNotificationsList = []; List 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, }); initNotificationsViewModel({int? statusID}) { notificationsList.clear(); currentPage = 0; hasMoreNotifications = true; isNotificationsLoading = true; // if (statusID != null) { // notificationStatusID = statusID; // } // getAllNotifications(); 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(); currentPage = 0; hasMoreNotifications = true; notifyListeners(); } Future getAllNotifications({ Function(dynamic)? onSuccess, Function(String)? onError, }) async { isNotificationsLoading = true; unreadNotificationsCount = 0; notificationsList.clear(); notifyListeners(); final result = await notificationsRepo.getAllNotifications( notificationStatusID: notificationStatusID, pagingSize: pagingSize, currentPage: currentPage, ); result.fold( (failure) async { isNotificationsLoading = false; notifyListeners(); await errorHandlerService.handleError(failure: failure); if (onError != null) { onError(failure.toString()); } }, (apiResponse) { isNotificationsLoading = false; if (apiResponse.messageStatus == 2) { notifyListeners(); if (onError != null) { onError(apiResponse.errorMessage ?? "Unknown error"); } } else if (apiResponse.messageStatus == 1) { final newNotifications = apiResponse.data ?? []; if (newNotifications.isEmpty || newNotifications.length < pagingSize) { hasMoreNotifications = false; } 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); } } catch (ex) { print(ex.toString()); } } }, ); } Future markAsRead({ int notificationID = 0, Function(dynamic)? onSuccess, Function(String)? onError, }) async { final result = await notificationsRepo.markAsRead(notificationID: notificationID); result.fold( (failure) async {}, (apiResponse) { refreshNotifications(); }, ); } Future refreshNotifications({ Function(dynamic)? onSuccess, Function(String)? onError, }) async { notificationsList.clear(); currentPage = 0; hasMoreNotifications = true; await getAllNotifications(onSuccess: onSuccess, onError: onError); } Future loadMoreNotifications({ Function(dynamic)? onSuccess, Function(String)? onError, }) async { await getAllNotifications(onSuccess: onSuccess, onError: onError); } @override void dispose() { super.dispose(); } }