@@ -23,6 +23,7 @@ import 'dart:async';
2323
2424import 'package:adaptive_theme/adaptive_theme.dart' ;
2525import 'package:background_downloader/background_downloader.dart' ;
26+ import 'package:boxbox/api/formula1.dart' ;
2627import 'package:boxbox/config/notifications.dart' ;
2728import 'package:boxbox/config/router.dart' ;
2829import 'package:awesome_notifications/awesome_notifications.dart' ;
@@ -36,6 +37,7 @@ import 'package:go_router/go_router.dart';
3637import 'package:hive_flutter/hive_flutter.dart' ;
3738import 'package:receive_sharing_intent/receive_sharing_intent.dart' ;
3839import 'package:timeago/timeago.dart' as timeago;
40+ import 'package:workmanager/workmanager.dart' ;
3941
4042void main () async {
4143 WidgetsFlutterBinding .ensureInitialized ();
@@ -58,6 +60,45 @@ void main() async {
5860 runApp (const MyApp ());
5961}
6062
63+ @pragma ('vm:entry-point' )
64+ void callbackDispatcher () {
65+ Workmanager ().executeTask (
66+ (task, inputData) async {
67+ await Hive .initFlutter ();
68+ Box hiveBox = await Hive .openBox ("requests" );
69+ Box settingsBox = await Hive .openBox ("settings" );
70+ Map cachedNews = hiveBox.get ('news' , defaultValue: {}) as Map ;
71+ bool useDataSaverMode =
72+ settingsBox.get ('useDataSaverMode' , defaultValue: false ) as bool ;
73+ try {
74+ Map fetchedData = await Formula1 ().getRawNews (0 );
75+ if (cachedNews.isNotEmpty &&
76+ fetchedData['items' ][0 ]['id' ] != cachedNews['items' ][0 ]['id' ]) {
77+ bool hasBreaking = false ;
78+ for (var article in fetchedData['items' ]) {
79+ if (article['id' ] == cachedNews['items' ][0 ]['id' ]) {
80+ break ;
81+ } else if (article['breaking' ] != null && article['breaking' ]) {
82+ Notifications ()
83+ .showArticleNotification (article, useDataSaverMode);
84+ hasBreaking = true ;
85+ }
86+ }
87+ if (! hasBreaking) {
88+ Notifications ().showArticleNotification (
89+ fetchedData['items' ][0 ], useDataSaverMode);
90+ }
91+
92+ hiveBox.put ('news' , fetchedData);
93+ }
94+ return Future .value (true );
95+ } catch (error, _) {
96+ return Future .value (false );
97+ }
98+ },
99+ );
100+ }
101+
61102void setTimeagoLocaleMessages () {
62103 timeago.setLocaleMessages ('ar' , timeago.ArMessages ());
63104 timeago.setLocaleMessages ('ar_short' , timeago.ArShortMessages ());
0 commit comments