Skip to content

Commit

Permalink
feat(notifications): display a message if the notifications are disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
vareversat committed May 8, 2023
1 parent 577bb9b commit f51c2f7
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 80 deletions.
2 changes: 1 addition & 1 deletion lib/const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Const {
'@mipmap/ic_slice_launcher_adaptive_fore';
static const Duration notificationDurationValueDefaultValue =
Duration(minutes: 60);
static const bool notificationDurationEnabledDefaultValue = true;
static const bool notificationDurationEnabledDefaultValue = false;
static TimeOfDay notificationTimeValueDefaultValue =
const TimeOfDay(hour: 6, minute: 0);
static const bool notificationTimeEnabledDefaultValue = false;
Expand Down
4 changes: 3 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,7 @@
}
},
"favoriteSlotsInterferenceWarning": "This schedule interferes with one or more time slots",
"favoriteTimeSlotEnabledWarning": "Attention, by activating this parameter you will only receive notifications when an event occurs during one of your time slots"
"favoriteTimeSlotEnabledWarning": "Attention, by activating this parameter you will only receive notifications when an event occurs during one of your time slots",
"notificationNotEnabledMessage": "Please note that notifications are not allowed. Please go to the application settings to authorize the sending of notifications",
"notificationNotEnabledOpenSettings": "Open notification settings"
}
4 changes: 3 additions & 1 deletion lib/l10n/app_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,7 @@
}
},
"favoriteSlotsInterferenceWarning": "Este horario interfiere con uno o más intervalos de tiempo",
"favoriteTimeSlotEnabledWarning": "Atención, al activar este parámetro solo recibirás notificaciones cuando ocurra un evento durante una de tus franjas horarias"
"favoriteTimeSlotEnabledWarning": "Atención, al activar este parámetro solo recibirás notificaciones cuando ocurra un evento durante una de tus franjas horarias",
"notificationNotEnabledMessage": "Tenga en cuenta que las notificaciones no están permitidas. Vaya a la configuración de la aplicación para autorizar el envío de notificaciones.",
"notificationNotEnabledOpenSettings": "Abrir configuración de notificaciones"
}
4 changes: 3 additions & 1 deletion lib/l10n/app_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,7 @@
}
},
"favoriteSlotsInterferenceWarning": "Cette prévision interfère avec un ou plusieurs créneaux",
"favoriteTimeSlotEnabledWarning": "Attention, en activant ce paramètres vous allez uniquement recevoir les notifications lorsqu'un évèmenent se produit durant l'un de vos créneaux"
"favoriteTimeSlotEnabledWarning": "Attention, en activant ce paramètres vous allez uniquement recevoir les notifications lorsqu'un évèmenent se produit durant l'un de vos créneaux",
"notificationNotEnabledMessage": "Attention, les notifications ne sont pas autorisées. Veuillez vous rendre dans les paramètres de l'application pour autoriser l'envoi de notification",
"notificationNotEnabledOpenSettings": "Ouvrir les paramètres de notification"
}
58 changes: 58 additions & 0 deletions lib/screens/notification_screen.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:ui';

import 'package:app_settings/app_settings.dart';
import 'package:chabo/bloc/notification/notification_bloc.dart';
import 'package:chabo/cubits/floating_actions_cubit.dart';
import 'package:chabo/cubits/notification_service_cubit.dart';
import 'package:chabo/custom_properties.dart';
import 'package:chabo/custom_widget_state.dart';
import 'package:chabo/dialogs/days_of_the_week_dialog.dart';
Expand Down Expand Up @@ -64,6 +66,62 @@ class _NotificationScreenState extends CustomWidgetState<NotificationScreen> {
builder: (context, notificationState) {
return Column(
children: [
FutureBuilder<bool?>(
future: context
.read<NotificationServiceCubit>()
.state
.areNotificationsEnabled(),
builder: (
BuildContext context,
AsyncSnapshot<bool?> snapshot,
) {
if (snapshot.hasData &&
snapshot.data != null &&
!snapshot.data!) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: const Duration(seconds: 10),
showCloseIcon: true,
backgroundColor:
Theme.of(context).colorScheme.error,
content: Column(
children: [
Text(
AppLocalizations.of(context)!
.notificationNotEnabledMessage,
style: Theme.of(context)
.textTheme
.labelLarge
?.copyWith(
color: Theme.of(context)
.colorScheme
.onError,
),
overflow: TextOverflow.visible,
),
const SizedBox(
height: 20,
),
ElevatedButton(
onPressed: () => AppSettings
.openNotificationSettings(),
child: Text(
AppLocalizations.of(context)!
.notificationNotEnabledOpenSettings,
),
),
],
),
),
);
});
}

return const SizedBox.shrink();
},
),
Column(
children: [
_CustomListTile(
Expand Down
167 changes: 91 additions & 76 deletions lib/service/notification_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,93 +85,109 @@ class NotificationService {
return false;
}

Future<bool> areNotificationsEnabled() async {
if (Platform.isAndroid) {
final AndroidFlutterLocalNotificationsPlugin? androidImplementation =
localNotifications.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();

return await androidImplementation?.areNotificationsEnabled() ?? false;
}

return false;
}

Future<void> computeNotifications(
List<AbstractChabanBridgeForecast> chabanBridgeForecasts,
NotificationState notificationSate,
BuildContext context,
) async {
tz.initializeTimeZones();
int index = 0;
localNotifications.cancelAll().then((value) => null);
localNotifications.cancelAll();
List<DateTime> weekSeparatedChabanBridgeForecast = [];
for (final chabanBridgeForecast in chabanBridgeForecasts) {
/// Compute the slot time linked to a forecast before starting the notification computation
chabanBridgeForecast
.computeSlotInterference(notificationSate.timeSlotsValue);
final hasTimeSlots = chabanBridgeForecast.interferingTimeSlots.isNotEmpty;
if ((notificationSate.openingNotificationEnabled &&
!notificationSate.timeSlotsEnabledForNotifications) ||
(notificationSate.openingNotificationEnabled &&
notificationSate.timeSlotsEnabledForNotifications &&
hasTimeSlots)) {
index += 1;
await _createOpeningScheduledNotifications(
index,
chabanBridgeForecast,
context,
);
}
if ((notificationSate.closingNotificationEnabled &&
!notificationSate.timeSlotsEnabledForNotifications) ||
(notificationSate.closingNotificationEnabled &&
notificationSate.timeSlotsEnabledForNotifications &&
hasTimeSlots)) {
index += 1;
await _createClosingScheduledNotifications(
index,
chabanBridgeForecast,
context,
);
}
if ((notificationSate.timeNotificationEnabled &&
!notificationSate.timeSlotsEnabledForNotifications) ||
(notificationSate.timeNotificationEnabled &&
notificationSate.timeSlotsEnabledForNotifications &&
hasTimeSlots)) {
index += 1;
await _createTimeScheduledNotifications(
index,
chabanBridgeForecast,
context,
notificationSate.timeNotificationValue,
);
}
if ((notificationSate.dayNotificationEnabled &&
!notificationSate.timeSlotsEnabledForNotifications) ||
(notificationSate.dayNotificationEnabled &&
notificationSate.timeSlotsEnabledForNotifications &&
hasTimeSlots)) {
var last = chabanBridgeForecast.circulationClosingDate
.previous(notificationSate.dayNotificationValue.weekPosition);
if (weekSeparatedChabanBridgeForecast.isEmpty ||
weekSeparatedChabanBridgeForecast.last == last) {
weekSeparatedChabanBridgeForecast.add(last);
} else {
if (await _requestPermissions()) {
for (final chabanBridgeForecast in chabanBridgeForecasts) {
/// Compute the slot time linked to a forecast before starting the notification computation
chabanBridgeForecast
.computeSlotInterference(notificationSate.timeSlotsValue);
final hasTimeSlots =
chabanBridgeForecast.interferingTimeSlots.isNotEmpty;
if ((notificationSate.openingNotificationEnabled &&
!notificationSate.timeSlotsEnabledForNotifications) ||
(notificationSate.openingNotificationEnabled &&
notificationSate.timeSlotsEnabledForNotifications &&
hasTimeSlots)) {
index += 1;
await _createDayScheduledNotifications(
await _createOpeningScheduledNotifications(
index,
weekSeparatedChabanBridgeForecast.length,
weekSeparatedChabanBridgeForecast.last,
notificationSate.dayNotificationTimeValue,
chabanBridgeForecast,
context,
);
weekSeparatedChabanBridgeForecast.clear();
weekSeparatedChabanBridgeForecast.add(last);
}
}
if ((notificationSate.durationNotificationEnabled &&
!notificationSate.timeSlotsEnabledForNotifications) ||
(notificationSate.durationNotificationEnabled &&
notificationSate.timeSlotsEnabledForNotifications &&
hasTimeSlots)) {
index += 1;
await _createDurationScheduledNotifications(
index,
chabanBridgeForecast,
context,
notificationSate.durationNotificationValue,
notificationSate.durationNotificationValue.durationToString(context),
);
if ((notificationSate.closingNotificationEnabled &&
!notificationSate.timeSlotsEnabledForNotifications) ||
(notificationSate.closingNotificationEnabled &&
notificationSate.timeSlotsEnabledForNotifications &&
hasTimeSlots)) {
index += 1;
await _createClosingScheduledNotifications(
index,
chabanBridgeForecast,
context,
);
}
if ((notificationSate.timeNotificationEnabled &&
!notificationSate.timeSlotsEnabledForNotifications) ||
(notificationSate.timeNotificationEnabled &&
notificationSate.timeSlotsEnabledForNotifications &&
hasTimeSlots)) {
index += 1;
await _createTimeScheduledNotifications(
index,
chabanBridgeForecast,
context,
notificationSate.timeNotificationValue,
);
}
if ((notificationSate.dayNotificationEnabled &&
!notificationSate.timeSlotsEnabledForNotifications) ||
(notificationSate.dayNotificationEnabled &&
notificationSate.timeSlotsEnabledForNotifications &&
hasTimeSlots)) {
var last = chabanBridgeForecast.circulationClosingDate
.previous(notificationSate.dayNotificationValue.weekPosition);
if (weekSeparatedChabanBridgeForecast.isEmpty ||
weekSeparatedChabanBridgeForecast.last == last) {
weekSeparatedChabanBridgeForecast.add(last);
} else {
index += 1;
await _createDayScheduledNotifications(
index,
weekSeparatedChabanBridgeForecast.length,
weekSeparatedChabanBridgeForecast.last,
notificationSate.dayNotificationTimeValue,
context,
);
weekSeparatedChabanBridgeForecast.clear();
weekSeparatedChabanBridgeForecast.add(last);
}
}
if ((notificationSate.durationNotificationEnabled &&
!notificationSate.timeSlotsEnabledForNotifications) ||
(notificationSate.durationNotificationEnabled &&
notificationSate.timeSlotsEnabledForNotifications &&
hasTimeSlots)) {
index += 1;
await _createDurationScheduledNotifications(
index,
chabanBridgeForecast,
context,
notificationSate.durationNotificationValue,
notificationSate.durationNotificationValue
.durationToString(context),
);
}
}
}
}
Expand Down Expand Up @@ -318,8 +334,7 @@ class NotificationService {
NotificationDetails notificationDetails,
) async {
/// Prevent from creating notification in the past AND make sure that the user enable the notification
if (notificationScheduleTime.isAfter(DateTime.now()) &&
await _requestPermissions()) {
if (notificationScheduleTime.isAfter(DateTime.now())) {
developer.log(
'Creating a notification on channel ${notificationDetails.android!.channelId} with ID $notificationId scheduled at $notificationScheduleTime',
name: 'notification-service.on.scheduleNotification',
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.1"
app_settings:
dependency: "direct main"
description:
name: app_settings
sha256: "66715a323ac36d6c8201035ba678777c0d2ea869e4d7064300d95af10c3bb8cb"
url: "https://pub.dev"
source: hosted
version: "4.2.0"
archive:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies:
stream_transform: ^2.0.0
timezone: ^0.9.1
url_launcher: ^6.1.7
app_settings: ^4.2.0


dev_dependencies:
Expand Down

0 comments on commit f51c2f7

Please sign in to comment.