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; NotificationsRepo notificationsRepo; ErrorHandlerService errorHandlerService; List notificationsList = []; int currentPage = 0; int pagingSize = 14; int notificationStatusID = 2; // Default to status 2 (e.g., unread) 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(); } setNotificationStatusID(int statusID) { notificationStatusID = statusID; notificationsList.clear(); currentPage = 0; hasMoreNotifications = true; notifyListeners(); } Future getAllNotifications({ Function(dynamic)? onSuccess, Function(String)? onError, }) async { isNotificationsLoading = true; 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; } notificationsList.addAll(newNotifications); currentPage++; notifyListeners(); if (onSuccess != null) { onSuccess(apiResponse); } } }, ); } 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(); } }