forked from eatvio/eatvio_app
107 lines
5.3 KiB
Dart
107 lines
5.3 KiB
Dart
import 'dart:convert';
|
|
import 'dart:developer';
|
|
|
|
import 'package:eatvio_app/core/websockets/ws_manager.dart';
|
|
import 'package:eatvio_app/features/notifications/models/notification_model.dart';
|
|
import 'package:eatvio_app/features/notifications/presentation/notifications_cubit/notifications_state.dart';
|
|
import 'package:eatvio_app/features/notifications/repository/notifications_repository.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
class NotificationsCubit extends Cubit<NotificationsState> {
|
|
final List<BaseNotificationModel> _notifications = [];
|
|
final WsManager wsManager;
|
|
final NotificationsRepository notificationsRepository;
|
|
NotificationsCubit({
|
|
required this.wsManager,
|
|
required this.notificationsRepository,
|
|
}) : super(const NotificationsState.empty()) {
|
|
wsManager.newNotificationsStream?.listen((event) {
|
|
_handleNotification(event.data);
|
|
});
|
|
}
|
|
BaseNotificationModel _parseNotification(Map<String, dynamic> notificationJson, NotificationType type) {
|
|
try {
|
|
return switch (type) {
|
|
NotificationType.planSubscriptionStartedToAuthor => PlanSubscriptionStartedToAuthorNotification.fromJson(notificationJson, type),
|
|
NotificationType.planSubscriptionCanceledToAuthor => PlanSubscriptionCanceledToAuthorNotification.fromJson(notificationJson, type),
|
|
NotificationType.chosenPlanStartedToClientNotification => ChosenPlanStartedToClientNotification.fromJson(notificationJson, type),
|
|
NotificationType.chosenPlanTomorrowReminderToClientNotification =>
|
|
ChosenPlanTomorrowReminderToClientNotification.fromJson(notificationJson, type),
|
|
NotificationType.diaryReminderNotification => DiaryReminderNotification.fromJson(notificationJson, type),
|
|
NotificationType.consultantLeftComment => ConsultantLeftCommentNotification.fromJson(notificationJson, type),
|
|
NotificationType.personalizedDietIsDefined => PersonalizedDietIsDefinedNotification.fromJson(notificationJson, type),
|
|
NotificationType.chosenPlanFinishedToClient => ChosenPlanFinishedToClientNotification.fromJson(notificationJson, type),
|
|
NotificationType.planSubscriptionCanceledToCustomer => PlanSubscriptionCanceledToAuthorNotification.fromJson(notificationJson, type),
|
|
NotificationType.chosenPlanStartedToConsultantNotification => ChosenPlanStartedToConsultantNotification.fromJson(notificationJson, type),
|
|
NotificationType.chosenPlanTomorrowReminderToConsultantNotification =>
|
|
ChosenPlanTomorrowReminderToConsultantNotification.fromJson(notificationJson, type),
|
|
NotificationType.planSubscriptionPaidToAuthor => PlanSubscriptionPaidToAuthorNotification.fromJson(notificationJson, type),
|
|
NotificationType.chosenPlanFinishedToConsultant => ChosenPlanFinishedToConsultantNotification.fromJson(notificationJson, type),
|
|
NotificationType.unknown => UnknownNotification(type: type),
|
|
};
|
|
} catch (e) {
|
|
return UnknownNotification(type: NotificationType.unknown);
|
|
}
|
|
}
|
|
|
|
void _handleNotification(String notificationData) {
|
|
final json = jsonDecode(notificationData) as Map<String, dynamic>;
|
|
final stringifiedType = json['notification_class_name'];
|
|
final type = NotificationType.getTypeFromString(stringifiedType);
|
|
final notification = _parseNotification(json, type);
|
|
_notifications.insert(0, notification);
|
|
emit(
|
|
NotificationsState.loaded(
|
|
notifications: _notifications,
|
|
notificationsCount: _notifications.length,
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> getNotifications() async {
|
|
try {
|
|
final notifications = await notificationsRepository.getNotifications(page: 1);
|
|
_notifications.clear();
|
|
_notifications.addAll(notifications ?? []);
|
|
emit(
|
|
NotificationsState.loaded(
|
|
notifications: _notifications,
|
|
notificationsCount: _notifications.length,
|
|
),
|
|
);
|
|
} catch (e, st) {
|
|
log(e.toString(), stackTrace: st);
|
|
}
|
|
}
|
|
}
|
|
|
|
final qwe = {
|
|
'event': 'IlluminateNotificationsEventsBroadcastNotificationCreated',
|
|
'channel': 'private-message-events.36',
|
|
'data': {
|
|
"plan_name": "Little Cheeseburger",
|
|
"plan_id": 18,
|
|
"user_name": "\u041b\u044f\u043f\u0438\u0441 \u041b\u0430\u043f\u0448\u0430",
|
|
"user_id": 53,
|
|
"user_photo_path": "https://dev2.eatvio.com/storage/profile-photos/X2aHwD6IOIFxEK8HM2d2oLuqio5BmocDLeWoH3Gs.jpg",
|
|
"chat_id": "3d14615d-1388-4833-8165-b40daa78f96a",
|
|
"chat_url": "https://dev2.eatvio.com/chat/3d14615d-1388-4833-8165-b40daa78f96a",
|
|
"notification_class_name": "App\\Notifications\\Plan\\PlanSubscriptionStartedToAuthor",
|
|
"database_type": "client-start-plan-subscription-to-author",
|
|
"id": "65cbea6d-1734-406e-b5da-79db270aed90",
|
|
"type": "client-start-plan-subscription-to-author"
|
|
}
|
|
};
|
|
final qwe2 = {
|
|
"plan_name": "Little Cheeseburger",
|
|
"plan_id": 18,
|
|
"user_name": "\u041b\u044f\u043f\u0438\u0441 \u041b\u0430\u043f\u0448\u0430",
|
|
"user_id": 53,
|
|
"user_photo_path": "https://dev2.eatvio.com/storage/profile-photos/X2aHwD6IOIFxEK8HM2d2oLuqio5BmocDLeWoH3Gs.jpg",
|
|
"chat_url": "https://dev2.eatvio.com/chat/3d14615d-1388-4833-8165-b40daa78f96a",
|
|
"notification_class_name": "App\\Notifications\\Plan\\PlanSubscriptionCanceledToAuthor",
|
|
"database_type": "client-cancel-plan-subscription-to-author",
|
|
"id": "cf86d0e7-8ebb-4360-9b8a-b6df97e12aed",
|
|
"type": "client-cancel-plan-subscription-to-author"
|
|
};
|