forked from eatvio/eatvio_app
65 lines
3.8 KiB
Dart
65 lines
3.8 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:eatvio_app/features/notifications/models/notification_model.dart';
|
|
|
|
class NotificationsService {
|
|
final Dio dio;
|
|
NotificationsService(this.dio);
|
|
|
|
Future<List<BaseNotificationModel>?> getNotifications({required int page}) async {
|
|
try {
|
|
final response = await dio.get('/api/profile/notifications?page=$page');
|
|
if (response.statusCode == 200) {
|
|
final data = response.data['data'] as List<dynamic>;
|
|
final notifications = data.map((notificationJson) {
|
|
try {
|
|
final stringifiedType = notificationJson['data']['notification_class_name'];
|
|
final readDate = notificationJson['read_at'];
|
|
final type = NotificationType.getTypeFromString(stringifiedType);
|
|
return switch (type) {
|
|
NotificationType.planSubscriptionStartedToAuthor =>
|
|
PlanSubscriptionStartedToAuthorNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.planSubscriptionCanceledToAuthor =>
|
|
PlanSubscriptionCanceledToAuthorNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.chosenPlanStartedToClientNotification =>
|
|
ChosenPlanStartedToClientNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.chosenPlanTomorrowReminderToClientNotification =>
|
|
ChosenPlanTomorrowReminderToClientNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.diaryReminderNotification => DiaryReminderNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.consultantLeftComment => ConsultantLeftCommentNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.personalizedDietIsDefined =>
|
|
PersonalizedDietIsDefinedNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.chosenPlanFinishedToClient =>
|
|
ChosenPlanFinishedToClientNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.planSubscriptionCanceledToCustomer =>
|
|
PlanSubscriptionCanceledToAuthorNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.chosenPlanStartedToConsultantNotification =>
|
|
ChosenPlanStartedToConsultantNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.chosenPlanTomorrowReminderToConsultantNotification =>
|
|
ChosenPlanTomorrowReminderToConsultantNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.planSubscriptionPaidToAuthor =>
|
|
PlanSubscriptionPaidToAuthorNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.chosenPlanFinishedToConsultant =>
|
|
ChosenPlanFinishedToConsultantNotification.fromJson(notificationJson['data'], type, isRead: readDate != null),
|
|
NotificationType.unknown => UnknownNotification(type: type),
|
|
};
|
|
} catch (e) {
|
|
return UnknownNotification(type: NotificationType.unknown);
|
|
}
|
|
}).toList();
|
|
return notifications.where((notification) => notification.type != NotificationType.unknown).toList();
|
|
}
|
|
return null;
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> markNotificationsAsRead() async {
|
|
try {
|
|
await dio.post('/api/profile/mark_notifications_as_readed');
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
}
|