Skip to content

Commit 66fe4ca

Browse files
committed
feat(notifications): add a setting to limit requests to unmetered networks
1 parent c3101fa commit 66fe4ca

28 files changed

+306
-20
lines changed

lib/Screens/Settings/notifications.dart

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,19 @@ class _NotificationsSettingsScreenState
4444
.get('newsNotificationsEnabled', defaultValue: false) as bool;
4545
int refreshInterval =
4646
Hive.box('settings').get('refreshInterval', defaultValue: 6) as int;
47+
String networkConnectionType = Hive.box('settings')
48+
.get('networkConnectionType', defaultValue: 'Wi-Fi') as String;
49+
4750
Map durations = {
4851
2: AppLocalizations.of(context)!.notifications2hours,
4952
6: AppLocalizations.of(context)!.notifications6hours,
5053
12: AppLocalizations.of(context)!.notifications12hours,
5154
24: AppLocalizations.of(context)!.notifications24hours,
5255
};
56+
Map connections = {
57+
'Wi-Fi': AppLocalizations.of(context)!.wifi,
58+
'Any': AppLocalizations.of(context)!.anyNetwork,
59+
};
5360

5461
return Scaffold(
5562
appBar: AppBar(
@@ -97,15 +104,7 @@ class _NotificationsSettingsScreenState
97104
onChanged: notificationsEnabled
98105
? (bool value) async {
99106
if (value) {
100-
int refreshInterval = Hive.box('settings')
101-
.get('refreshInterval', defaultValue: 6) as int;
102-
await Workmanager().registerPeriodicTask(
103-
'newsLoader',
104-
"Load news in background",
105-
existingWorkPolicy: ExistingWorkPolicy.replace,
106-
frequency: Duration(hours: refreshInterval),
107-
initialDelay: Duration(hours: refreshInterval),
108-
);
107+
await Notifications().registerPeriodicTask();
109108
} else {
110109
Workmanager().cancelAll();
111110
}
@@ -119,13 +118,48 @@ class _NotificationsSettingsScreenState
119118
}
120119
: null,
121120
),
121+
ListTile(
122+
title: Text(
123+
AppLocalizations.of(context)!.requiredNetworkConnection,
124+
),
125+
enabled: notificationsEnabled && newsNotificationsEnabled,
126+
trailing: DropdownButton(
127+
value: networkConnectionType,
128+
onChanged: notificationsEnabled && newsNotificationsEnabled
129+
? (String? newValue) async {
130+
if (newValue != null) {
131+
networkConnectionType = newValue;
132+
Hive.box('settings')
133+
.put('networkConnectionType', newValue);
134+
await Notifications().registerPeriodicTask();
135+
136+
setState(() {});
137+
}
138+
}
139+
: null,
140+
items: <String>[
141+
'Wi-Fi',
142+
'Any',
143+
].map<DropdownMenuItem<String>>(
144+
(String value) {
145+
return DropdownMenuItem<String>(
146+
value: value,
147+
child: Text(
148+
connections[value],
149+
style: TextStyle(
150+
fontSize: 12,
151+
),
152+
),
153+
);
154+
},
155+
).toList(),
156+
),
157+
),
122158
ListTile(
123159
title: Text(
124160
AppLocalizations.of(context)!.refreshInterval,
125-
style: TextStyle(),
126161
),
127162
enabled: notificationsEnabled && newsNotificationsEnabled,
128-
//onTap: () {},
129163
trailing: DropdownButton(
130164
value: refreshInterval,
131165
onChanged: notificationsEnabled && newsNotificationsEnabled

lib/config/notifications.dart

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,34 @@ import 'package:hive_flutter/hive_flutter.dart';
2323
import 'package:workmanager/workmanager.dart';
2424

2525
class Notifications {
26+
Constraints getConstraints() {
27+
String networkConnectionType = Hive.box('settings')
28+
.get('networkConnectionType', defaultValue: 'Wi-Fi') as String;
29+
if (networkConnectionType == 'Wi-Fi') {
30+
return Constraints(networkType: NetworkType.unmetered);
31+
} else {
32+
return Constraints(networkType: NetworkType.connected);
33+
}
34+
}
35+
36+
Future<void> registerPeriodicTask() async {
37+
int refreshInterval =
38+
Hive.box('settings').get('refreshInterval', defaultValue: 6) as int;
39+
await Workmanager().registerPeriodicTask(
40+
'newsLoader',
41+
"Load news in background",
42+
existingWorkPolicy: ExistingWorkPolicy.replace,
43+
frequency: Duration(hours: refreshInterval),
44+
initialDelay: Duration(hours: refreshInterval),
45+
constraints: Notifications().getConstraints(),
46+
);
47+
}
48+
2649
Future<void> initializeNotifications() async {
2750
bool notificationsEnabled = Hive.box('settings')
2851
.get('notificationsEnabled', defaultValue: false) as bool;
2952
bool newsNotificationsEnabled = Hive.box('settings')
3053
.get('newsNotificationsEnabled', defaultValue: false) as bool;
31-
int refreshInterval =
32-
Hive.box('settings').get('refreshInterval', defaultValue: 6) as int;
3354

3455
if (notificationsEnabled) {
3556
await AwesomeNotifications().initialize(
@@ -58,13 +79,7 @@ class Notifications {
5879
await Workmanager().initialize(
5980
callbackDispatcher,
6081
);
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-
);
82+
await registerPeriodicTask();
6883
}
6984
}
7085

lib/l10n/app_en.arb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"@alreadyDownloadedArticle": {},
1616
"alreadyDownloading": "Already downloading",
1717
"@alreadyDownloading": {},
18+
"anyNetwork": "Any network",
19+
"@anyNetwork": {},
1820
"apiKey": "API Key",
1921
"@apiKey": {},
2022
"appearance": "Appearance",
@@ -319,6 +321,8 @@
319321
"@refreshInterval": {},
320322
"requestError": "Could not fetch data.",
321323
"@requestError": {},
324+
"requiredNetworkConnection": "Required network connection",
325+
"@requiredNetworkConnection": {},
322326
"results": "Results",
323327
"@results": {},
324328
"running": "Running",
@@ -413,6 +417,8 @@
413417
"@watchHighlightsOnYoutube": {},
414418
"watchOnYouTube": "Watch on YouTube",
415419
"@watchOnYouTube": {},
420+
"wifi": "Wi-Fi",
421+
"@wifi": {},
416422
"worldChampionships": "World Championships",
417423
"@worldChampionships": {},
418424
"yes": "Yes",

lib/l10n/app_fr.arb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"@alreadyDownloadedArticle": {},
1616
"alreadyDownloading": "Téléchargement déjà en cours",
1717
"@alreadyDownloading": {},
18+
"anyNetwork": "Tout réseau",
19+
"@anyNetwork": {},
1820
"apiKey": "Clé API",
1921
"@apiKey": {},
2022
"appearance": "Apparence",
@@ -320,6 +322,8 @@
320322
"@refreshInterval": {},
321323
"requestError": "Impossible de récupérer les données.",
322324
"@requestError": {},
325+
"requiredNetworkConnection": "Connexion réseau nécessaire",
326+
"@requiredNetworkConnection": {},
323327
"results": "Résultats",
324328
"@results": {},
325329
"running": "En cours",
@@ -414,6 +418,8 @@
414418
"@watchHighlightsOnYoutube": {},
415419
"watchOnYouTube": "Voir sur YouTube",
416420
"@watchOnYouTube": {},
421+
"wifi": "Wi-Fi",
422+
"@wifi": {},
417423
"worldChampionships": "Champion du monde",
418424
"@worldChampionships": {},
419425
"yes": "Oui",

lib/l10n/app_localizations.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ abstract class AppLocalizations {
185185
/// **'Already downloading'**
186186
String get alreadyDownloading;
187187

188+
/// No description provided for @anyNetwork.
189+
///
190+
/// In en, this message translates to:
191+
/// **'Any network'**
192+
String get anyNetwork;
193+
188194
/// No description provided for @apiKey.
189195
///
190196
/// In en, this message translates to:
@@ -1091,6 +1097,12 @@ abstract class AppLocalizations {
10911097
/// **'Could not fetch data.'**
10921098
String get requestError;
10931099

1100+
/// No description provided for @requiredNetworkConnection.
1101+
///
1102+
/// In en, this message translates to:
1103+
/// **'Required network connection'**
1104+
String get requiredNetworkConnection;
1105+
10941106
/// No description provided for @results.
10951107
///
10961108
/// In en, this message translates to:
@@ -1373,6 +1385,12 @@ abstract class AppLocalizations {
13731385
/// **'Watch on YouTube'**
13741386
String get watchOnYouTube;
13751387

1388+
/// No description provided for @wifi.
1389+
///
1390+
/// In en, this message translates to:
1391+
/// **'Wi-Fi'**
1392+
String get wifi;
1393+
13761394
/// No description provided for @worldChampionships.
13771395
///
13781396
/// In en, this message translates to:

lib/l10n/app_localizations_ar.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class AppLocalizationsAr extends AppLocalizations {
3232
@override
3333
String get alreadyDownloading => 'Already downloading';
3434

35+
@override
36+
String get anyNetwork => 'Any network';
37+
3538
@override
3639
String get apiKey => 'API Key';
3740

@@ -485,6 +488,9 @@ class AppLocalizationsAr extends AppLocalizations {
485488
@override
486489
String get requestError => 'تعذر جلب البيانات.';
487490

491+
@override
492+
String get requiredNetworkConnection => 'Required network connection';
493+
488494
@override
489495
String get results => 'نتائج';
490496

@@ -626,6 +632,9 @@ class AppLocalizationsAr extends AppLocalizations {
626632
@override
627633
String get watchOnYouTube => 'Watch on YouTube';
628634

635+
@override
636+
String get wifi => 'Wi-Fi';
637+
629638
@override
630639
String get worldChampionships => 'بطولات العالم';
631640

lib/l10n/app_localizations_bn.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class AppLocalizationsBn extends AppLocalizations {
3232
@override
3333
String get alreadyDownloading => 'ইতিমধ্যেই ডাউনলোড হচ্ছে';
3434

35+
@override
36+
String get anyNetwork => 'Any network';
37+
3538
@override
3639
String get apiKey => 'API Key';
3740

@@ -485,6 +488,9 @@ class AppLocalizationsBn extends AppLocalizations {
485488
@override
486489
String get requestError => 'ডেটা আনা যায়নি।';
487490

491+
@override
492+
String get requiredNetworkConnection => 'Required network connection';
493+
488494
@override
489495
String get results => 'ফলাফল';
490496

@@ -626,6 +632,9 @@ class AppLocalizationsBn extends AppLocalizations {
626632
@override
627633
String get watchOnYouTube => 'Watch on YouTube';
628634

635+
@override
636+
String get wifi => 'Wi-Fi';
637+
629638
@override
630639
String get worldChampionships => 'বিশ্ব চ্যাম্পিয়নশিপ';
631640

lib/l10n/app_localizations_de.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class AppLocalizationsDe extends AppLocalizations {
3232
@override
3333
String get alreadyDownloading => 'Wird bereits heruntergeladen';
3434

35+
@override
36+
String get anyNetwork => 'Any network';
37+
3538
@override
3639
String get apiKey => 'API-Schlüssel';
3740

@@ -485,6 +488,9 @@ class AppLocalizationsDe extends AppLocalizations {
485488
@override
486489
String get requestError => 'Daten konnten nicht abgerufen werden.';
487490

491+
@override
492+
String get requiredNetworkConnection => 'Required network connection';
493+
488494
@override
489495
String get results => 'Ergebnisse';
490496

@@ -626,6 +632,9 @@ class AppLocalizationsDe extends AppLocalizations {
626632
@override
627633
String get watchOnYouTube => 'Auf YouTube ansehen';
628634

635+
@override
636+
String get wifi => 'Wi-Fi';
637+
629638
@override
630639
String get worldChampionships => 'Weltmeisterschaften';
631640

lib/l10n/app_localizations_el.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class AppLocalizationsEl extends AppLocalizations {
3232
@override
3333
String get alreadyDownloading => 'Already downloading';
3434

35+
@override
36+
String get anyNetwork => 'Any network';
37+
3538
@override
3639
String get apiKey => 'API Key';
3740

@@ -485,6 +488,9 @@ class AppLocalizationsEl extends AppLocalizations {
485488
@override
486489
String get requestError => 'Could not fetch data.';
487490

491+
@override
492+
String get requiredNetworkConnection => 'Required network connection';
493+
488494
@override
489495
String get results => 'Results';
490496

@@ -626,6 +632,9 @@ class AppLocalizationsEl extends AppLocalizations {
626632
@override
627633
String get watchOnYouTube => 'Watch on YouTube';
628634

635+
@override
636+
String get wifi => 'Wi-Fi';
637+
629638
@override
630639
String get worldChampionships => 'World Championships';
631640

lib/l10n/app_localizations_en.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class AppLocalizationsEn extends AppLocalizations {
3232
@override
3333
String get alreadyDownloading => 'Already downloading';
3434

35+
@override
36+
String get anyNetwork => 'Any network';
37+
3538
@override
3639
String get apiKey => 'API Key';
3740

@@ -485,6 +488,9 @@ class AppLocalizationsEn extends AppLocalizations {
485488
@override
486489
String get requestError => 'Could not fetch data.';
487490

491+
@override
492+
String get requiredNetworkConnection => 'Required network connection';
493+
488494
@override
489495
String get results => 'Results';
490496

@@ -626,6 +632,9 @@ class AppLocalizationsEn extends AppLocalizations {
626632
@override
627633
String get watchOnYouTube => 'Watch on YouTube';
628634

635+
@override
636+
String get wifi => 'Wi-Fi';
637+
629638
@override
630639
String get worldChampionships => 'World Championships';
631640

0 commit comments

Comments
 (0)