|
| 1 | +/* |
| 2 | + * This file is part of BoxBox (https://github.com/BrightDV/BoxBox). |
| 3 | + * |
| 4 | + * BoxBox is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * BoxBox is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * along with BoxBox. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * Copyright (c) 2022-2025, BrightDV |
| 18 | + */ |
| 19 | + |
| 20 | +import 'package:awesome_notifications/awesome_notifications.dart'; |
| 21 | +import 'package:boxbox/api/formula1.dart'; |
| 22 | +import 'package:hive_flutter/hive_flutter.dart'; |
| 23 | +import 'package:workmanager/workmanager.dart'; |
| 24 | + |
| 25 | +class Notifications { |
| 26 | + Future<void> initializeNotifications() async { |
| 27 | + bool notificationsEnabled = Hive.box('settings') |
| 28 | + .get('notificationsEnabled', defaultValue: false) as bool; |
| 29 | + bool newsNotificationsEnabled = Hive.box('settings') |
| 30 | + .get('newsNotificationsEnabled', defaultValue: false) as bool; |
| 31 | + int refreshInterval = |
| 32 | + Hive.box('settings').get('refreshInterval', defaultValue: 6) as int; |
| 33 | + |
| 34 | + if (notificationsEnabled) { |
| 35 | + await AwesomeNotifications().initialize( |
| 36 | + 'resource://drawable/notification_icon', |
| 37 | + [ |
| 38 | + NotificationChannel( |
| 39 | + channelKey: 'eventTracker', |
| 40 | + channelName: 'New Grand Prix notifications', |
| 41 | + channelDescription: 'Show a notification before each GP.', |
| 42 | + importance: NotificationImportance.High, |
| 43 | + channelShowBadge: true, |
| 44 | + ), |
| 45 | + NotificationChannel( |
| 46 | + channelKey: 'newArticle', |
| 47 | + channelName: 'New article', |
| 48 | + channelDescription: |
| 49 | + 'Show a notification when a new article is published.', |
| 50 | + importance: NotificationImportance.High, |
| 51 | + channelShowBadge: true, |
| 52 | + ), |
| 53 | + ], |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + if (notificationsEnabled && newsNotificationsEnabled) { |
| 58 | + await Workmanager().initialize( |
| 59 | + callbackDispatcher, |
| 60 | + ); |
| 61 | + await Workmanager().registerPeriodicTask( |
| 62 | + 'newsLoader', |
| 63 | + "Load news in background", |
| 64 | + existingWorkPolicy: ExistingWorkPolicy.replace, |
| 65 | + frequency: Duration(hours: refreshInterval), |
| 66 | + initialDelay: Duration(hours: refreshInterval), |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + int createUniqueId() { |
| 72 | + return DateTime.now().millisecondsSinceEpoch.remainder(100000); |
| 73 | + } |
| 74 | + |
| 75 | + Future<void> showArticleNotification( |
| 76 | + Map article, bool useDataSaverMode) async { |
| 77 | + String imageUrl = article['thumbnail']['image']['url']; |
| 78 | + if (useDataSaverMode) { |
| 79 | + if (article['thumbnail']['image']['renditions'] != null) { |
| 80 | + imageUrl = article['thumbnail']['image']['renditions']['2col-retina']; |
| 81 | + } else { |
| 82 | + imageUrl += '.transform/2col-retina/image.jpg'; |
| 83 | + } |
| 84 | + } |
| 85 | + await AwesomeNotifications().createNotification( |
| 86 | + content: NotificationContent( |
| 87 | + id: createUniqueId(), |
| 88 | + channelKey: 'newArticle', |
| 89 | + title: article['title'], |
| 90 | + body: article['metaDescription'], |
| 91 | + largeIcon: imageUrl, |
| 92 | + bigPicture: imageUrl, |
| 93 | + hideLargeIconOnExpand: true, |
| 94 | + notificationLayout: NotificationLayout.BigPicture, |
| 95 | + payload: { |
| 96 | + 'id': article['id'], |
| 97 | + 'title': article['title'], |
| 98 | + }, |
| 99 | + ), |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + @pragma('vm:entry-point') |
| 104 | + void callbackDispatcher() { |
| 105 | + Workmanager().executeTask( |
| 106 | + (task, inputData) async { |
| 107 | + await Hive.initFlutter(); |
| 108 | + Box hiveBox = await Hive.openBox("requests"); |
| 109 | + Box settingsBox = await Hive.openBox("settings"); |
| 110 | + Map cachedNews = hiveBox.get('news', defaultValue: {}) as Map; |
| 111 | + bool useDataSaverMode = |
| 112 | + settingsBox.get('useDataSaverMode', defaultValue: false) as bool; |
| 113 | + try { |
| 114 | + Map fetchedData = await Formula1().getRawNews(0); |
| 115 | + if (cachedNews.isNotEmpty && |
| 116 | + fetchedData['items'][0]['id'] != cachedNews['items'][0]['id']) { |
| 117 | + bool hasBreaking = false; |
| 118 | + for (var article in fetchedData['items']) { |
| 119 | + if (article['id'] == cachedNews['items'][0]['id']) { |
| 120 | + break; |
| 121 | + } else if (article['breaking'] != null && article['breaking']) { |
| 122 | + showArticleNotification(article, useDataSaverMode); |
| 123 | + hasBreaking = true; |
| 124 | + } |
| 125 | + } |
| 126 | + if (!hasBreaking) { |
| 127 | + showArticleNotification( |
| 128 | + fetchedData['items'][0], useDataSaverMode); |
| 129 | + } |
| 130 | + |
| 131 | + hiveBox.put('news', fetchedData); |
| 132 | + } |
| 133 | + return Future.value(true); |
| 134 | + } catch (error, _) { |
| 135 | + return Future.value(false); |
| 136 | + } |
| 137 | + }, |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
0 commit comments