Skip to content

Commit a825a9c

Browse files
committed
feat(notifications): add session notification (#9)
1 parent 792ea9e commit a825a9c

File tree

4 files changed

+55
-35
lines changed

4 files changed

+55
-35
lines changed

lib/Screens/schedule.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,15 @@ class ScheduleWidget extends StatelessWidget {
141141
: Formula1().formatLastSchedule(schedule, toCome),
142142
toCome,
143143
scrollController: scrollController,
144+
isCache: true,
144145
)
145146
: RequestErrorWidget(snapshot.error.toString())
146147
: schedule['races'] != null
147148
? RacesList(
148149
FormulaE().formatLastSchedule(schedule, toCome),
149150
toCome,
150151
scrollController: scrollController,
152+
isCache: true,
151153
)
152154
: RequestErrorWidget(snapshot.error.toString())
153155
: snapshot.hasData
@@ -182,6 +184,7 @@ class ScheduleWidget extends StatelessWidget {
182184
.formatLastSchedule(schedule, toCome),
183185
toCome,
184186
scrollController: scrollController,
187+
isCache: true,
185188
)
186189
: const LoadingIndicatorUtil()
187190
: schedule['races'] != null
@@ -191,6 +194,7 @@ class ScheduleWidget extends StatelessWidget {
191194
FormulaE().formatLastSchedule(schedule, toCome),
192195
toCome,
193196
scrollController: scrollController,
197+
isCache: true,
194198
)
195199
: const LoadingIndicatorUtil(),
196200
);

lib/api/formula1.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ class Formula1 {
10631063
);
10641064
}
10651065

1066-
List<DateTime> sessionDates = [];
1066+
/* List<DateTime> sessionDates = [];
10671067
List sessionStates = [];
10681068
String gmtOffset =
10691069
formatedResponse['race']['meetingSessions'][0]?['gmtOffset'] ?? '';
@@ -1106,7 +1106,7 @@ class Formula1 {
11061106
sessionStates: sessionStates,
11071107
);
11081108
1109-
formatedResponse['raceCustomBBParameter'] = raceWithSessions;
1109+
formatedResponse['raceCustomBBParameter'] = raceWithSessions; */
11101110

11111111
return formatedResponse;
11121112
}

lib/api/race_components.dart

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
* Copyright (c) 2022-2025, BrightDV
1818
*/
1919

20-
// import 'package:awesome_notifications/awesome_notifications.dart';
20+
import 'package:awesome_notifications/awesome_notifications.dart';
21+
import 'package:boxbox/api/formula1.dart';
2122
import 'package:boxbox/helpers/news.dart';
2223
import 'package:boxbox/helpers/racetracks_url.dart';
2324
import 'package:flutter/material.dart';
@@ -313,47 +314,64 @@ class RacesList extends StatelessWidget {
313314
final List<Race> items;
314315
final bool isUpNext;
315316
final ScrollController? scrollController;
317+
final bool isCache;
316318

317319
const RacesList(
318320
this.items,
319321
this.isUpNext, {
320322
Key? key,
321323
this.scrollController,
324+
this.isCache = false,
322325
}) : super(key: key);
323326

324-
//int createUniqueId() {
325-
// return DateTime.now().millisecondsSinceEpoch.remainder(100000);
326-
//}
327-
//
328-
//Future<void> scheduledNotification(Race race) async {
329-
// DateTime date = DateTime.parse(race.date);
330-
// date.subtract(
331-
// Duration(
332-
// days: 3,
333-
// ),
334-
// );
335-
// await AwesomeNotifications().createNotification(
336-
// content: NotificationContent(
337-
// id: createUniqueId(),
338-
// channelKey: 'eventTracker',
339-
// title: "A Grand-Prix is starting soon.",
340-
// body: "Be ready for the Free Practices!",
341-
// ),
342-
// schedule: NotificationCalendar(
343-
// allowWhileIdle: true,
344-
// repeats: false,
345-
// millisecond: 0,
346-
// second: date.second,
347-
// minute: date.minute,
348-
// hour: date.hour,
349-
// day: date.day,
350-
// month: date.month,
351-
// ),
352-
// );
353-
//}
327+
int createUniqueId() {
328+
return DateTime.now().millisecondsSinceEpoch.remainder(100000);
329+
}
330+
331+
Future<void> scheduledNotification(String meetingId) async {
332+
List<NotificationModel> notifications =
333+
await AwesomeNotifications().listScheduledNotifications();
334+
if (notifications.isNotEmpty &&
335+
notifications[0].content?.payload?['meetingId'] == meetingId) {
336+
return;
337+
}
338+
339+
Map race = await Formula1().getCircuitDetails(meetingId);
340+
for (var session in race['race']['meetingSessions']) {
341+
DateTime sessionDate = DateTime.parse(
342+
session['startTime'] + session['gmtOffset'],
343+
).toLocal().subtract(
344+
Duration(minutes: 5),
345+
);
346+
347+
sessionDate = DateTime.now().add(Duration(seconds: 15));
348+
await AwesomeNotifications().createNotification(
349+
content: NotificationContent(
350+
id: createUniqueId(),
351+
channelKey: 'eventTracker',
352+
title: race['race']['meetingName'],
353+
body: "Be ready! ${session['description']} is starting soon!",
354+
payload: {'meetingId': meetingId, 'session': session['session']},
355+
),
356+
schedule: NotificationCalendar(
357+
allowWhileIdle: true,
358+
repeats: false,
359+
millisecond: 0,
360+
second: sessionDate.second,
361+
minute: sessionDate.minute,
362+
hour: sessionDate.hour,
363+
day: sessionDate.day,
364+
month: sessionDate.month,
365+
),
366+
);
367+
}
368+
}
354369

355370
@override
356371
Widget build(BuildContext context) {
372+
if (items.isNotEmpty && isUpNext && !isCache) {
373+
scheduledNotification(items[0].meetingId);
374+
}
357375
return isUpNext
358376
? //FutureBuilder(
359377
// future: scheduledNotification(items[0]),

lib/main.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ void main() async {
5959
channelKey: 'eventTracker',
6060
channelName: 'New Grand Prix notifications',
6161
channelDescription: 'Show a notification before each GP.',
62-
defaultColor: Colors.white,
6362
importance: NotificationImportance.High,
6463
channelShowBadge: true,
6564
),
@@ -68,7 +67,6 @@ void main() async {
6867
channelName: 'New article',
6968
channelDescription:
7069
'Show a notification when a new article is published.',
71-
defaultColor: Colors.white,
7270
importance: NotificationImportance.High,
7371
channelShowBadge: true,
7472
),

0 commit comments

Comments
 (0)