Skip to content

Commit c3101fa

Browse files
committed
fix(notifications): wrong frequency applied; initialize notifications only if enabled
1 parent 9c529f9 commit c3101fa

File tree

5 files changed

+165
-120
lines changed

5 files changed

+165
-120
lines changed

lib/Screens/Settings/notifications.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import 'package:awesome_notifications/awesome_notifications.dart';
21+
import 'package:boxbox/config/notifications.dart';
2122
import 'package:boxbox/l10n/app_localizations.dart';
2223
import 'package:flutter/material.dart';
2324
import 'package:hive_flutter/hive_flutter.dart';
@@ -70,10 +71,12 @@ class _NotificationsSettingsScreenState
7071
bool isAllowedAfterRequest = await AwesomeNotifications()
7172
.requestPermissionToSendNotifications();
7273
if (isAllowedAfterRequest) {
74+
await Notifications().initializeNotifications();
7375
notificationsEnabled = value;
7476
Hive.box('settings').put('notificationsEnabled', value);
7577
}
7678
} else {
79+
await Notifications().initializeNotifications();
7780
notificationsEnabled = value;
7881
Hive.box('settings').put('notificationsEnabled', value);
7982
}
@@ -94,12 +97,14 @@ class _NotificationsSettingsScreenState
9497
onChanged: notificationsEnabled
9598
? (bool value) async {
9699
if (value) {
100+
int refreshInterval = Hive.box('settings')
101+
.get('refreshInterval', defaultValue: 6) as int;
97102
await Workmanager().registerPeriodicTask(
98103
'newsLoader',
99104
"Load news in background",
100105
existingWorkPolicy: ExistingWorkPolicy.replace,
101-
frequency: const Duration(seconds: 30),
102-
initialDelay: const Duration(hours: 2),
106+
frequency: Duration(hours: refreshInterval),
107+
initialDelay: Duration(hours: refreshInterval),
103108
);
104109
} else {
105110
Workmanager().cancelAll();

lib/config/notifications.dart

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
}

lib/l10n/app_localizations_ta.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AppLocalizationsTa extends AppLocalizations {
1515
String get aboutDescription => 'பெட்டி, பெட்டி! gplv3+இன் கீழ் உரிமம் பெற்ற நகல் லிப்ரே மென்பொருள். விளம்பரங்கள் அல்லது டிராக்கர்கள் இல்லாமல் ஃபார்முலா 1 மற்றும் ஃபார்முலா ஈ ஆகியவற்றைப் பின்பற்ற பயனர்களை அனுமதிப்பதே இதன் நோக்கம்.';
1616

1717
@override
18-
String get aboutBottomLine => '❤ ❤ by by by by.';
18+
String get aboutBottomLine => '❤ உடன் ஒளிமிக்கடிவி.';
1919

2020
@override
2121
String get addCustomFeed => 'தனிப்பயன் ஊட்டத்தைச் சேர்க்கவும்';
@@ -33,7 +33,7 @@ class AppLocalizationsTa extends AppLocalizations {
3333
String get alreadyDownloading => 'ஏற்கனவே பதிவிறக்குகிறது';
3434

3535
@override
36-
String get apiKey => 'API Key';
36+
String get apiKey => 'பநிஇ விசை';
3737

3838
@override
3939
String get appearance => 'தோற்றம்';
@@ -225,7 +225,7 @@ class AppLocalizationsTa extends AppLocalizations {
225225
String get fontDescription => 'கட்டுரைகளில் பயன்படுத்தப்படும் எழுத்துரு.';
226226

227227
@override
228-
String get fullScreenGestures => 'Enter/exit fullscreen gestures';
228+
String get fullScreenGestures => 'முழுத்திரை சைகைகளை உள்ளிடவும்/வெளியேறவும்';
229229

230230
@override
231231
String get gallery => 'கேலரி';
@@ -381,10 +381,10 @@ class AppLocalizationsTa extends AppLocalizations {
381381
String get offline => 'இணைப்பில்லாத. தரவு புதுப்பித்த நிலையில் இருக்காது.';
382382

383383
@override
384-
String get offtrack => 'You went off-track!';
384+
String get offtrack => 'நீங்கள் பாதையை விட்டுச் சென்றீர்கள்!';
385385

386386
@override
387-
String get offtrackSub => 'Back on track';
387+
String get offtrackSub => 'மீண்டும் பாதையில்';
388388

389389
@override
390390
String get openInBrowser => 'வலை உலாவியில் திறக்கவும்';
@@ -465,10 +465,10 @@ class AppLocalizationsTa extends AppLocalizations {
465465
String get raceFirstLetter => 'R';
466466

467467
@override
468-
String get raceStartsIn => 'இனம் தொடங்குகிறது:';
468+
String get raceStartsIn => 'போட்டி தொடங்குகிறது:';
469469

470470
@override
471-
String get raceStartsOn => 'இனம் தொடங்குகிறது:';
471+
String get raceStartsOn => 'அன்று போட்டி தொடங்குகிறது:';
472472

473473
@override
474474
String get refresh => 'புதுப்பிப்பு';
@@ -516,10 +516,10 @@ class AppLocalizationsTa extends AppLocalizations {
516516
String get sessionRunning => 'அமர்வு இயங்கும்';
517517

518518
@override
519-
String get sessionStartsIn => 'அமர்வு தொடங்குகிறது:';
519+
String get sessionStartsIn => 'இல் அமர்வு தொடங்குகிறது:';
520520

521521
@override
522-
String get sessionStartsOn => 'அமர்வு தொடங்குகிறது:';
522+
String get sessionStartsOn => 'அன்று அமர்வு தொடங்குகிறது:';
523523

524524
@override
525525
String get settings => 'அமைப்புகள்';
@@ -585,10 +585,10 @@ class AppLocalizationsTa extends AppLocalizations {
585585
String get unavailableOffline => 'ஆஃப்லைனில் கிடைக்கவில்லை';
586586

587587
@override
588-
String get updateApiKey => 'Update API Key';
588+
String get updateApiKey => 'பநிஇ விசையைப் புதுப்பி';
589589

590590
@override
591-
String get updateApiKeySub => 'Update the API key of the official website.\nUpdate this only if you now what you are doing.';
591+
String get updateApiKeySub => 'அதிகாரப்பூர்வ வலைத்தளத்தின் API விசையைப் புதுப்பி.\nநீங்கள் இப்போது என்ன செய்கிறீர்கள் என்றால் மட்டுமே இதைப் புதுப்பி.';
592592

593593
@override
594594
String get updates => 'புதிய பதிப்புகள்';
@@ -600,13 +600,13 @@ class AppLocalizationsTa extends AppLocalizations {
600600
String get useOfficialDataSourceSub => 'அதிகாரப்பூர்வ மொபைல் பயன்பாட்டின் தரவு மூலத்தைப் பயன்படுத்தவும். இது விரைவான முடிவுகளைத் தருகிறது, ஆனால் எல்லா விவரங்களும் கிடைக்கவில்லை. மேலும் விவரங்களுக்கு நீண்ட அழுத்தம்.';
601601

602602
@override
603-
String get useOfficialWebview => 'நேரடி அமர்வுகளுக்கு அதிகாரப்பூர்வ வெப்வியூவைப் பயன்படுத்தவும்';
603+
String get useOfficialWebview => 'ஃ1-டேசு.காம் க்கு பதிலாக நேரடி அமர்வுகளுக்கு அதிகாரப்பூர்வ வெப்வியூவைப் பயன்படுத்தவும்';
604604

605605
@override
606-
String get victory => 'செய்';
606+
String get victory => 'வெற்றி';
607607

608608
@override
609-
String get victories => 'செய்';
609+
String get victories => 'வெற்றிகள்';
610610

611611
@override
612612
String get videos => 'வீடியோக்கள்';
@@ -624,7 +624,7 @@ class AppLocalizationsTa extends AppLocalizations {
624624
String get watchHighlightsOnYoutube => 'YouTube இல் சிறப்பம்சங்களைப் பாருங்கள்';
625625

626626
@override
627-
String get watchOnYouTube => 'Watch on YouTube';
627+
String get watchOnYouTube => 'வலைகுழாயில் பாருங்கள்';
628628

629629
@override
630630
String get worldChampionships => 'உலக சாம்பியன்சிப்புகள்';

0 commit comments

Comments
 (0)