forked from eatvio/eatvio_app
feat(notifications): added json parsing for notifications and tested impplementation of notifications layoute
This commit is contained in:
@@ -1,22 +1,55 @@
|
||||
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;
|
||||
|
||||
NotificationsCubit({required this.wsManager}) : super(const NotificationsState.empty()) {
|
||||
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 data = jsonDecode(notificationData) as Map<String, dynamic>;
|
||||
// final notification = NotificationModel.fromJson(data);
|
||||
// _notifications.add(notification);
|
||||
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,
|
||||
@@ -24,4 +57,50 @@ class NotificationsCubit extends Cubit<NotificationsState> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
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"
|
||||
};
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:eatvio_app/core/theme/theme_helper.dart';
|
||||
import 'package:eatvio_app/features/notifications/models/notification_model.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@@ -6,9 +7,15 @@ class NotificationWidget extends StatelessWidget {
|
||||
final BaseNotificationModel notification;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// return (notification.type) {
|
||||
// NotificationType.userSubscribedNotification => const _UserSubscribedNotification(),
|
||||
// };
|
||||
return Container();
|
||||
return Container(
|
||||
width: MediaQuery.sizeOf(context).width,
|
||||
height: 60,
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: appTheme.green,
|
||||
borderRadius: BorderRadius.circular(18),
|
||||
),
|
||||
child: Center(child: Text(notification.type.name)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user