|
17 | 17 | * Copyright (c) 2022-2025, BrightDV |
18 | 18 | */ |
19 | 19 |
|
20 | | -// import 'package:awesome_notifications/awesome_notifications.dart'; |
| 20 | +import 'package:awesome_notifications/awesome_notifications.dart'; |
| 21 | +import 'package:boxbox/api/formula1.dart'; |
21 | 22 | import 'package:boxbox/helpers/news.dart'; |
22 | 23 | import 'package:boxbox/helpers/racetracks_url.dart'; |
23 | 24 | import 'package:flutter/material.dart'; |
@@ -313,47 +314,64 @@ class RacesList extends StatelessWidget { |
313 | 314 | final List<Race> items; |
314 | 315 | final bool isUpNext; |
315 | 316 | final ScrollController? scrollController; |
| 317 | + final bool isCache; |
316 | 318 |
|
317 | 319 | const RacesList( |
318 | 320 | this.items, |
319 | 321 | this.isUpNext, { |
320 | 322 | Key? key, |
321 | 323 | this.scrollController, |
| 324 | + this.isCache = false, |
322 | 325 | }) : super(key: key); |
323 | 326 |
|
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 | + } |
354 | 369 |
|
355 | 370 | @override |
356 | 371 | Widget build(BuildContext context) { |
| 372 | + if (items.isNotEmpty && isUpNext && !isCache) { |
| 373 | + scheduledNotification(items[0].meetingId); |
| 374 | + } |
357 | 375 | return isUpNext |
358 | 376 | ? //FutureBuilder( |
359 | 377 | // future: scheduledNotification(items[0]), |
|
0 commit comments