forked from eatvio/eatvio_app
47 lines
3.1 KiB
Dart
47 lines
3.1 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 type = NotificationType.getTypeFromString(stringifiedType);
|
|
return switch (type) {
|
|
NotificationType.planSubscriptionStartedToAuthor => PlanSubscriptionStartedToAuthorNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.planSubscriptionCanceledToAuthor => PlanSubscriptionCanceledToAuthorNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.chosenPlanStartedToClientNotification => ChosenPlanStartedToClientNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.chosenPlanTomorrowReminderToClientNotification =>
|
|
ChosenPlanTomorrowReminderToClientNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.diaryReminderNotification => DiaryReminderNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.consultantLeftComment => ConsultantLeftCommentNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.personalizedDietIsDefined => PersonalizedDietIsDefinedNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.chosenPlanFinishedToClient => ChosenPlanFinishedToClientNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.planSubscriptionCanceledToCustomer => PlanSubscriptionCanceledToAuthorNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.chosenPlanStartedToConsultantNotification => ChosenPlanStartedToConsultantNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.chosenPlanTomorrowReminderToConsultantNotification =>
|
|
ChosenPlanTomorrowReminderToConsultantNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.planSubscriptionPaidToAuthor => PlanSubscriptionPaidToAuthorNotification.fromJson(notificationJson['data'], type),
|
|
NotificationType.chosenPlanFinishedToConsultant => ChosenPlanFinishedToConsultantNotification.fromJson(notificationJson['data'], type),
|
|
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;
|
|
}
|
|
}
|
|
}
|