import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; import '../../models/local_notification_model.dart'; import '../../models/notification/notifications_model.dart'; import '../../models/notification_model.dart'; import '../../services/push_notification_service.dart'; class NotificationsProvider extends ChangeNotifier{ List notifications = []; static final NotificationsProvider notificationsProvider= NotificationsProvider._internal(); NotificationsProvider._internal(); factory NotificationsProvider(){ return notificationsProvider; } addAllNotifications(List data){ for(NotificationsModel x in data){ bool exists = notifications.where((element) => element.id==x.id).isNotEmpty; if(!exists){ notifications.add(x); } } notifyListeners(); } clearNotificationsList(){ notifications.clear(); notifyListeners(); } addNotification(NotificationsModel notification){ notifications.insert(0, notification); notifyListeners(); } PushNotificationService get service => GetIt.I(); List notificationsFromApi = []; int notifPage = 0; int notifTotalPage = -1; bool loading = false; List localNotifs = []; ///************************************************** get All notifications *************************************************************/ Future?> getAllNotifications( {bool isRefresh = false, bool init = false}) async { notifications = []; try { loading = true; if (isRefresh) { notifPage = 0; notifications = []; notifTotalPage = -1; } if (init) { Future.delayed(Duration.zero, () { notifyListeners(); }); } else { notifyListeners(); } final apiResponsePost = await service.getAllNotifications(50, notifPage); if ((apiResponsePost.statusCode == 200 ||apiResponsePost.statusCode == 201) && apiResponsePost.data != null &&apiResponsePost.data!.result != null) { for (var eq in apiResponsePost.data!.result!) { var index = notificationsFromApi.indexWhere((element) => element.id == eq.id); if (index == -1) { notificationsFromApi.add(eq); } } if(apiResponsePost.data != null){ if(apiResponsePost.data!.totalPages!= null){ if(notifPage < apiResponsePost.data!.totalPages! -1){ notifPage++; } } } notifTotalPage = apiResponsePost.data!.totalPages ?? 1; loading = false; if (init) { Future.delayed(Duration.zero, () { notifyListeners(); }); } else { notifyListeners(); } return notificationsFromApi; } else { log("getAllNotifications.statusCode"); log(apiResponsePost.statusCode.toString()); log(apiResponsePost.data.toString()); loading = false; if (init) { Future.delayed(Duration.zero, () { notifyListeners(); }); } else { notifyListeners(); } return notificationsFromApi; } } catch (e, s) { log(e.toString(), stackTrace: s); loading = false; if (init) { Future.delayed(Duration.zero, () { notifyListeners(); }); } else { notifyListeners(); } return []; } } disposeNotifs() { notificationsFromApi.clear(); notifPage = 0; notifTotalPage = -1; loading = false; localNotifs = []; } addNotif(LocalNotificationModel notif) { localNotifs.add(notif); notifyListeners(); } LocalNotificationModel? getNotif(String notifId) { var index = localNotifs.indexWhere((element) => element.id == notifId); if (index != -1) { return localNotifs[index]; } return null; } } final notificationsProvider= NotificationsProvider(); // import 'dart:developer'; // // import 'package:flutter/material.dart'; // import 'package:get_it/get_it.dart'; // // import '../../models/local_notification_model.dart'; // import '../../models/notification_model.dart'; // import '../../services/push_notification_service.dart'; // // // // class NotificationsProvider with ChangeNotifier { // PushNotificationService get service => GetIt.I(); // List notifications = []; // int notifPage = 0; // int notifTotalPage = -1; // bool loading = false; // List localNotifs = []; // // ///************************************************** get All notifications *************************************************************/ // Future?> getAllNotifications( // {bool isRefresh = false, bool init = false}) async { // notifications = []; // try { // loading = true; // if (isRefresh) { // notifPage = 0; // notifications = []; // notifTotalPage = -1; // } // if (init) { // Future.delayed(Duration.zero, () { // notifyListeners(); // }); // } else { // notifyListeners(); // } // final apiResponsePost = await service.getAllNotifications(5, notifPage); // if ((apiResponsePost.statusCode == 200 || // apiResponsePost.statusCode == 201) && apiResponsePost.data != null && // apiResponsePost.data!.result != null) { // for (var eq in apiResponsePost.data!.result!) { // var index = notifications.indexWhere((element) => // element.id == eq.id); // if (index == -1) { // notifications.add(eq); // } // } // notifPage++; // notifTotalPage = apiResponsePost.data!.totalPages ?? 1; // loading = false; // if (init) { // Future.delayed(Duration.zero, () { // notifyListeners(); // }); // } else { // notifyListeners(); // } // return notifications; // } else { // log("getAllNotifications.statusCode"); // log(apiResponsePost.statusCode.toString()); // log(apiResponsePost.data.toString()); // loading = false; // if (init) { // Future.delayed(Duration.zero, () { // notifyListeners(); // }); // } else { // notifyListeners(); // } // return notifications; // } // } catch (e, s) { // log(e.toString(), stackTrace: s); // loading = false; // if (init) { // Future.delayed(Duration.zero, () { // notifyListeners(); // }); // } else { // notifyListeners(); // } // return []; // } // } // // disposeNotifs() { // notifications = []; // notifPage = 0; // notifTotalPage = -1; // loading = false; // localNotifs = []; // } // // addNotif(LocalNotificationModel notif) { // localNotifs.add(notif); // notifyListeners(); // } // // LocalNotificationModel? getNotif(String notifId) { // var index = localNotifs.indexWhere((element) => element.id == notifId); // if (index != -1) { // return localNotifs[index]; // } // return null; // } // }