Skip to content

Commit 8a03af9

Browse files
committed
refactor(notifications): generate admin in-app notifications programmatically
- Replace hardcoded notifications with a generated list of 21 notifications - Add new notification IDs in fixture_ids.dart - Simplify in_app_notifications.dart by removing repetitive code - Improve scalability and maintainability of notification fixtures
1 parent b01f9b4 commit 8a03af9

File tree

2 files changed

+52
-124
lines changed

2 files changed

+52
-124
lines changed

lib/src/fixtures/fixture_ids.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,3 +1051,18 @@ const String kInAppNotificationId3 = 'in_app_notification_3';
10511051
const String kInAppNotificationId4 = 'in_app_notification_4';
10521052
const String kInAppNotificationId5 = 'in_app_notification_5';
10531053
const String kInAppNotificationId6 = 'in_app_notification_6';
1054+
const String kInAppNotificationId7 = 'in_app_notification_7';
1055+
const String kInAppNotificationId8 = 'in_app_notification_8';
1056+
const String kInAppNotificationId9 = 'in_app_notification_9';
1057+
const String kInAppNotificationId10 = 'in_app_notification_10';
1058+
const String kInAppNotificationId11 = 'in_app_notification_11';
1059+
const String kInAppNotificationId12 = 'in_app_notification_12';
1060+
const String kInAppNotificationId13 = 'in_app_notification_13';
1061+
const String kInAppNotificationId14 = 'in_app_notification_14';
1062+
const String kInAppNotificationId15 = 'in_app_notification_15';
1063+
const String kInAppNotificationId16 = 'in_app_notification_16';
1064+
const String kInAppNotificationId17 = 'in_app_notification_17';
1065+
const String kInAppNotificationId18 = 'in_app_notification_18';
1066+
const String kInAppNotificationId19 = 'in_app_notification_19';
1067+
const String kInAppNotificationId20 = 'in_app_notification_20';
1068+
const String kInAppNotificationId21 = 'in_app_notification_21';
Lines changed: 37 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,46 @@
11
import 'package:core/core.dart';
22

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-
93
/// A list of initial in-app notification data to be loaded into the in-memory
104
/// data repository.
115
///
126
/// This data provides realistic, client-facing notifications for users,
137
/// suitable for a news app demo environment.
14-
final List<InAppNotification> inAppNotificationsFixturesData = [
15-
// --- Notifications for User: kAdminUserId ---
8+
final List<InAppNotification> inAppNotificationsFixturesData =
9+
_generateAdminNotifications();
1610

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();
7421

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

Comments
 (0)