|
1 | 1 | import 'package:core/core.dart'; |
2 | 2 |
|
3 | | -// Helper to find a headline by its ID from the fixtures. |
4 | | -// This avoids repetitive code and ensures we're using the headline data. |
5 | | -Headline _headlineById(String id) { |
6 | | - return headlinesFixturesData.firstWhere((headline) => headline.id == id); |
7 | | -} |
8 | | - |
9 | 3 | /// A list of initial in-app notification data to be loaded into the in-memory |
10 | 4 | /// data repository. |
11 | 5 | /// |
12 | 6 | /// This data provides realistic, client-facing notifications for users, |
13 | 7 | /// suitable for a news app demo environment. |
14 | | -final List<InAppNotification> inAppNotificationsFixturesData = [ |
15 | | - // --- Notifications for User: kAdminUserId --- |
| 8 | +final List<InAppNotification> inAppNotificationsFixturesData = |
| 9 | + _generateAdminNotifications(); |
16 | 10 |
|
17 | | - // 1. Unread Breaking News Notification |
18 | | - InAppNotification( |
19 | | - id: kInAppNotificationId1, |
20 | | - userId: kAdminUserId, |
21 | | - payload: PushNotificationPayload( |
22 | | - title: _headlineById(kHeadlineId11).title, |
23 | | - body: _headlineById(kHeadlineId11).excerpt, |
24 | | - imageUrl: _headlineById(kHeadlineId11).imageUrl, |
25 | | - data: { |
26 | | - 'notificationId': kInAppNotificationId1, |
27 | | - 'notificationType': |
28 | | - PushNotificationSubscriptionDeliveryType.breakingOnly.name, |
29 | | - 'contentType': 'headline', |
30 | | - 'headlineId': kHeadlineId11, |
31 | | - }, |
32 | | - ), |
33 | | - createdAt: DateTime.now().subtract(const Duration(minutes: 15)), |
34 | | - readAt: null, |
35 | | - ), |
36 | | - // 2. Read Notification on a followed topic (Science) |
37 | | - InAppNotification( |
38 | | - id: kInAppNotificationId2, |
39 | | - userId: kAdminUserId, |
40 | | - payload: PushNotificationPayload( |
41 | | - title: _headlineById(kHeadlineId24).title, |
42 | | - body: _headlineById(kHeadlineId24).excerpt, |
43 | | - imageUrl: _headlineById(kHeadlineId24).imageUrl, |
44 | | - data: { |
45 | | - 'notificationId': kInAppNotificationId2, |
46 | | - 'notificationType': |
47 | | - PushNotificationSubscriptionDeliveryType.breakingOnly.name, |
48 | | - 'contentType': 'headline', |
49 | | - 'headlineId': kHeadlineId24, |
50 | | - }, |
51 | | - ), |
52 | | - createdAt: DateTime.now().subtract(const Duration(days: 1)), |
53 | | - readAt: DateTime.now().subtract(const Duration(hours: 12)), |
54 | | - ), |
55 | | - // 3. Unread Notification on another followed topic (Business/Tech) |
56 | | - InAppNotification( |
57 | | - id: kInAppNotificationId3, |
58 | | - userId: kAdminUserId, |
59 | | - payload: PushNotificationPayload( |
60 | | - title: _headlineById(kHeadlineId37).title, |
61 | | - body: _headlineById(kHeadlineId37).excerpt, |
62 | | - imageUrl: _headlineById(kHeadlineId37).imageUrl, |
63 | | - data: { |
64 | | - 'notificationId': kInAppNotificationId3, |
65 | | - 'notificationType': |
66 | | - PushNotificationSubscriptionDeliveryType.breakingOnly.name, |
67 | | - 'contentType': 'headline', |
68 | | - 'headlineId': kHeadlineId37, |
69 | | - }, |
70 | | - ), |
71 | | - createdAt: DateTime.now().subtract(const Duration(hours: 2)), |
72 | | - readAt: null, |
73 | | - ), |
| 11 | +/// Generates a list of 21 breaking news notifications for the admin user. |
| 12 | +/// |
| 13 | +/// This approach makes it easy to scale up the number of notifications and |
| 14 | +/// vary their properties (e.g., read status, creation time) programmatically. |
| 15 | +List<InAppNotification> _generateAdminNotifications() { |
| 16 | + final notificationIds = List.generate( |
| 17 | + 21, |
| 18 | + (index) => 'in_app_notification_${index + 1}', |
| 19 | + ); |
| 20 | + final headlineIds = headlinesFixturesData.map((e) => e.id).toList(); |
74 | 21 |
|
75 | | - // --- Notifications for User: kUser1Id --- |
76 | | - // 4. Unread Breaking News Notification |
77 | | - InAppNotification( |
78 | | - id: kInAppNotificationId4, |
79 | | - userId: kUser1Id, |
80 | | - payload: PushNotificationPayload( |
81 | | - title: _headlineById(kHeadlineId1).title, |
82 | | - body: _headlineById(kHeadlineId1).excerpt, |
83 | | - imageUrl: _headlineById(kHeadlineId1).imageUrl, |
84 | | - data: { |
85 | | - 'notificationId': kInAppNotificationId4, |
86 | | - 'notificationType': |
87 | | - PushNotificationSubscriptionDeliveryType.breakingOnly.name, |
88 | | - 'contentType': 'headline', |
89 | | - 'headlineId': kHeadlineId1, |
90 | | - }, |
91 | | - ), |
92 | | - createdAt: DateTime.now().subtract(const Duration(minutes: 5)), |
93 | | - readAt: null, |
94 | | - ), |
95 | | - // 5. Read Notification on a followed topic (Sports) |
96 | | - InAppNotification( |
97 | | - id: kInAppNotificationId5, |
98 | | - userId: kUser1Id, |
99 | | - payload: PushNotificationPayload( |
100 | | - title: _headlineById(kHeadlineId2).title, |
101 | | - body: _headlineById(kHeadlineId2).excerpt, |
102 | | - imageUrl: _headlineById(kHeadlineId2).imageUrl, |
103 | | - data: { |
104 | | - 'notificationId': kInAppNotificationId5, |
105 | | - 'notificationType': |
106 | | - PushNotificationSubscriptionDeliveryType.breakingOnly.name, |
107 | | - 'contentType': 'headline', |
108 | | - 'headlineId': kHeadlineId2, |
109 | | - }, |
110 | | - ), |
111 | | - createdAt: DateTime.now().subtract(const Duration(days: 2)), |
112 | | - readAt: DateTime.now().subtract(const Duration(days: 1)), |
113 | | - ), |
114 | | - // 6. Unread Notification on another followed topic (Business) |
115 | | - InAppNotification( |
116 | | - id: kInAppNotificationId6, |
117 | | - userId: kUser1Id, |
118 | | - payload: PushNotificationPayload( |
119 | | - title: _headlineById(kHeadlineId7).title, |
120 | | - body: _headlineById(kHeadlineId7).excerpt, |
121 | | - imageUrl: _headlineById(kHeadlineId7).imageUrl, |
122 | | - data: { |
123 | | - 'notificationId': kInAppNotificationId6, |
124 | | - 'notificationType': |
125 | | - PushNotificationSubscriptionDeliveryType.breakingOnly.name, |
126 | | - 'contentType': 'headline', |
127 | | - 'headlineId': kHeadlineId7, |
128 | | - }, |
129 | | - ), |
130 | | - createdAt: DateTime.now().subtract(const Duration(hours: 4)), |
131 | | - readAt: null, |
132 | | - ), |
133 | | -]; |
| 22 | + return List.generate(21, (index) { |
| 23 | + final headline = headlinesFixturesData[index % headlineIds.length]; |
| 24 | + final notificationId = notificationIds[index]; |
| 25 | + final isRead = index < 2; |
| 26 | + |
| 27 | + return InAppNotification( |
| 28 | + id: notificationId, |
| 29 | + userId: kAdminUserId, |
| 30 | + payload: PushNotificationPayload( |
| 31 | + title: headline.title, |
| 32 | + body: headline.excerpt, |
| 33 | + imageUrl: headline.imageUrl, |
| 34 | + data: { |
| 35 | + 'notificationId': notificationId, |
| 36 | + 'notificationType': |
| 37 | + PushNotificationSubscriptionDeliveryType.breakingOnly.name, |
| 38 | + 'contentType': 'headline', |
| 39 | + 'headlineId': headline.id, |
| 40 | + }, |
| 41 | + ), |
| 42 | + createdAt: DateTime.now().subtract(Duration(days: index * 2)), |
| 43 | + readAt: isRead ? DateTime.now().subtract(Duration(hours: index)) : null, |
| 44 | + ); |
| 45 | + }); |
| 46 | +} |
0 commit comments