You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.1 KiB
Dart
114 lines
3.1 KiB
Dart
|
1 week ago
|
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<NotificationResponseModel> 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<void> 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<void> refreshNotifications({
|
||
|
|
Function(dynamic)? onSuccess,
|
||
|
|
Function(String)? onError,
|
||
|
|
}) async {
|
||
|
|
notificationsList.clear();
|
||
|
|
currentPage = 0;
|
||
|
|
hasMoreNotifications = true;
|
||
|
|
await getAllNotifications(onSuccess: onSuccess, onError: onError);
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> loadMoreNotifications({
|
||
|
|
Function(dynamic)? onSuccess,
|
||
|
|
Function(String)? onError,
|
||
|
|
}) async {
|
||
|
|
await getAllNotifications(onSuccess: onSuccess, onError: onError);
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
void dispose() {
|
||
|
|
super.dispose();
|
||
|
|
}
|
||
|
|
}
|