forked from eatvio/eatvio_app
feat(notifications): support payment-required and start-date-set types (#3054)
This commit is contained in:
@@ -1011,6 +1011,125 @@ class MealReminderNotification implements BaseNotificationModel {
|
||||
}
|
||||
}
|
||||
|
||||
/// Данные уведомления: в API-ленте лежат во вложенном `data`, а в realtime-событии могут быть в корне.
|
||||
Map<String, dynamic> _notificationData(Map<String, dynamic> json) {
|
||||
final data = json['data'];
|
||||
return data is Map<String, dynamic> ? data : json;
|
||||
}
|
||||
|
||||
DateTime? parseOptionalDate(dynamic value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
final raw = value.toString();
|
||||
if (raw.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
return DateTime.tryParse(raw)?.toLocal();
|
||||
}
|
||||
|
||||
class ClientPlanPaymentRequiredNotification implements BaseNotificationModel {
|
||||
@override
|
||||
final NotificationType type;
|
||||
@override
|
||||
final bool isRead;
|
||||
final String planName;
|
||||
final int planId;
|
||||
final String authorName;
|
||||
final int authorId;
|
||||
final String authorPhotoPath;
|
||||
final String chatId;
|
||||
final String chatUrl;
|
||||
final DateTime? startDate;
|
||||
final DateTime? paymentDeadlineAt;
|
||||
@override
|
||||
final DateTime createdAt;
|
||||
|
||||
ClientPlanPaymentRequiredNotification({
|
||||
required this.type,
|
||||
required this.isRead,
|
||||
required this.planName,
|
||||
required this.planId,
|
||||
required this.authorName,
|
||||
required this.authorId,
|
||||
required this.authorPhotoPath,
|
||||
required this.chatId,
|
||||
required this.chatUrl,
|
||||
required this.startDate,
|
||||
required this.paymentDeadlineAt,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
factory ClientPlanPaymentRequiredNotification.fromJson(Map<String, dynamic> json, NotificationType type, {bool? isRead, String? createdAt}) {
|
||||
final data = _notificationData(json);
|
||||
return ClientPlanPaymentRequiredNotification(
|
||||
type: type,
|
||||
isRead: isRead ?? false,
|
||||
planName: data['plan_name'] ?? '',
|
||||
planId: data['plan_id'] ?? 0,
|
||||
authorName: data['author_name'] ?? '',
|
||||
authorId: data['author_id'] ?? 0,
|
||||
authorPhotoPath: data['author_photo_path'] ?? '',
|
||||
chatId: data['chat_id'] ?? '',
|
||||
chatUrl: data['chat_url'] ?? '',
|
||||
startDate: parseOptionalDate(data['start_date']),
|
||||
paymentDeadlineAt: parseOptionalDate(data['payment_deadline_at']),
|
||||
createdAt: parseDate(createdAt ?? json['created_at'] ?? data['created_at'] ?? DateTime.now().toIso8601String()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ConsultantPlanStartDateSetNotification implements BaseNotificationModel {
|
||||
@override
|
||||
final NotificationType type;
|
||||
@override
|
||||
final bool isRead;
|
||||
final String planName;
|
||||
final int planId;
|
||||
final String clientName;
|
||||
final int userId;
|
||||
final String userPhotoPath;
|
||||
final String chatId;
|
||||
final String chatUrl;
|
||||
final DateTime? startDate;
|
||||
final DateTime? paymentDeadlineAt;
|
||||
@override
|
||||
final DateTime createdAt;
|
||||
|
||||
ConsultantPlanStartDateSetNotification({
|
||||
required this.type,
|
||||
required this.isRead,
|
||||
required this.planName,
|
||||
required this.planId,
|
||||
required this.clientName,
|
||||
required this.userId,
|
||||
required this.userPhotoPath,
|
||||
required this.chatId,
|
||||
required this.chatUrl,
|
||||
required this.startDate,
|
||||
required this.paymentDeadlineAt,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
factory ConsultantPlanStartDateSetNotification.fromJson(Map<String, dynamic> json, NotificationType type, {bool? isRead, String? createdAt}) {
|
||||
final data = _notificationData(json);
|
||||
return ConsultantPlanStartDateSetNotification(
|
||||
type: type,
|
||||
isRead: isRead ?? false,
|
||||
planName: data['plan_name'] ?? '',
|
||||
planId: data['plan_id'] ?? 0,
|
||||
clientName: data['user_name'] ?? '',
|
||||
userId: data['user_id'] ?? 0,
|
||||
userPhotoPath: data['user_photo_path'] ?? '',
|
||||
chatId: data['chat_id'] ?? '',
|
||||
chatUrl: data['chat_url'] ?? '',
|
||||
startDate: parseOptionalDate(data['start_date']),
|
||||
paymentDeadlineAt: parseOptionalDate(data['payment_deadline_at']),
|
||||
createdAt: parseDate(createdAt ?? json['created_at'] ?? data['created_at'] ?? DateTime.now().toIso8601String()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UnknownNotification implements BaseNotificationModel {
|
||||
@override
|
||||
final NotificationType type;
|
||||
@@ -2476,6 +2595,8 @@ enum NotificationType {
|
||||
clientStartPlanSubscriptionToAuthor,
|
||||
clientCancelPlanSubscriptionToAuthor,
|
||||
clientCancelPlanSubscriptionToSubscriber,
|
||||
clientPlanPaymentRequired,
|
||||
consultantPlanStartDateSet,
|
||||
userDailyReportCompleted,
|
||||
chosenPlanFinishedToConsultant,
|
||||
stayOnTrackToCustomer,
|
||||
@@ -2597,6 +2718,8 @@ enum NotificationType {
|
||||
'client-start-plan-subscription-to-author' => NotificationType.clientStartPlanSubscriptionToAuthor,
|
||||
'client-cancel-plan-subscription-to-author' => NotificationType.clientCancelPlanSubscriptionToAuthor,
|
||||
'client-cancel-plan-subscription-to-subscriber' => NotificationType.clientCancelPlanSubscriptionToSubscriber,
|
||||
'client-plan-payment-required' => NotificationType.clientPlanPaymentRequired,
|
||||
'consultant-plan-start-date-set' => NotificationType.consultantPlanStartDateSet,
|
||||
'user-daily-report-completed' => NotificationType.userDailyReportCompleted,
|
||||
'chosen-plan-finished-to-consultant' => NotificationType.chosenPlanFinishedToConsultant,
|
||||
'stay-on-track-to-customer' => NotificationType.stayOnTrackToCustomer,
|
||||
|
||||
@@ -344,6 +344,18 @@ class NotificationsCubit extends Cubit<NotificationsState> {
|
||||
isRead: isRead,
|
||||
createdAt: createdAt,
|
||||
),
|
||||
NotificationType.clientPlanPaymentRequired => ClientPlanPaymentRequiredNotification.fromJson(
|
||||
notificationJson,
|
||||
type,
|
||||
isRead: isRead,
|
||||
createdAt: createdAt,
|
||||
),
|
||||
NotificationType.consultantPlanStartDateSet => ConsultantPlanStartDateSetNotification.fromJson(
|
||||
notificationJson,
|
||||
type,
|
||||
isRead: isRead,
|
||||
createdAt: createdAt,
|
||||
),
|
||||
NotificationType.unknown => UnknownNotification(type: type, createdAt: parseDate(createdAt ?? jsonEncode(notificationJson))),
|
||||
};
|
||||
} catch (e, st) {
|
||||
|
||||
@@ -134,11 +134,54 @@ class NotificationWidget extends StatelessWidget {
|
||||
CommentComplaintNotification() => _CommentComplaintWidget(notification: notification as CommentComplaintNotification),
|
||||
CommentDeletedNotification() => _CommentDeletedWidget(notification: notification as CommentDeletedNotification),
|
||||
UserSubscribedNotification() => _UserSubscribedWidget(notification: notification as UserSubscribedNotification),
|
||||
ClientPlanPaymentRequiredNotification() => _ClientPlanPaymentRequiredWidget(
|
||||
notification: notification as ClientPlanPaymentRequiredNotification,
|
||||
),
|
||||
ConsultantPlanStartDateSetNotification() => _ConsultantPlanStartDateSetWidget(
|
||||
notification: notification as ConsultantPlanStartDateSetNotification,
|
||||
),
|
||||
UnknownNotification() => const SizedBox.shrink(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
VoidCallback? _chatTap(BuildContext context, String chatUrl) {
|
||||
final uri = Uri.tryParse(chatUrl);
|
||||
final uuid = (uri != null && uri.pathSegments.isNotEmpty) ? uri.pathSegments.last : '';
|
||||
if (uuid.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
return () => context.push('${RouteConstants.chatScreen}?uuid=$uuid');
|
||||
}
|
||||
|
||||
class _ClientPlanPaymentRequiredWidget extends StatelessWidget {
|
||||
const _ClientPlanPaymentRequiredWidget({required this.notification});
|
||||
final ClientPlanPaymentRequiredNotification notification;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return _BaseNotificationWidget(
|
||||
isRead: notification.isRead,
|
||||
notificationsText: 'Необходимо оплатить консультацию «${notification.planName}» у консультанта ${notification.authorName}',
|
||||
createdAt: notification.createdAt,
|
||||
onTap: _chatTap(context, notification.chatUrl),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ConsultantPlanStartDateSetWidget extends StatelessWidget {
|
||||
const _ConsultantPlanStartDateSetWidget({required this.notification});
|
||||
final ConsultantPlanStartDateSetNotification notification;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return _BaseNotificationWidget(
|
||||
isRead: notification.isRead,
|
||||
notificationsText: 'Дата старта установлена: «${notification.planName}». Клиент ${notification.clientName} ещё не оплатил',
|
||||
createdAt: notification.createdAt,
|
||||
onTap: _chatTap(context, notification.chatUrl),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _navigateToBottomNavRoute(BuildContext context, String path, [Object? extra]) {
|
||||
final controller = BottomNavScope.maybeOf(context);
|
||||
if (controller != null) {
|
||||
|
||||
@@ -485,6 +485,18 @@ class NotificationsService {
|
||||
isRead: readDate != null,
|
||||
createdAt: createdAt,
|
||||
),
|
||||
NotificationType.clientPlanPaymentRequired => ClientPlanPaymentRequiredNotification.fromJson(
|
||||
(notificationJson['data'] as Map<String, dynamic>?) ?? notificationJson,
|
||||
type,
|
||||
isRead: readDate != null,
|
||||
createdAt: createdAt,
|
||||
),
|
||||
NotificationType.consultantPlanStartDateSet => ConsultantPlanStartDateSetNotification.fromJson(
|
||||
(notificationJson['data'] as Map<String, dynamic>?) ?? notificationJson,
|
||||
type,
|
||||
isRead: readDate != null,
|
||||
createdAt: createdAt,
|
||||
),
|
||||
NotificationType.unknown => UnknownNotification(type: type, createdAt: parseDate(createdAt)),
|
||||
};
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user