Skip to content

Commit

Permalink
This should fix the remaining issues with the notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jun 3, 2021
1 parent 233bdd5 commit 19b8df7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 85 deletions.
22 changes: 11 additions & 11 deletions lib/application/notification/notification_bloc.dart
Expand Up @@ -386,7 +386,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
);

if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.title, notif.body, notif.completesAt);
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
}

Expand Down Expand Up @@ -417,7 +417,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
withTimeReduction: s.withTimeReduction,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.title, notif.body, notif.completesAt);
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
}

Expand Down Expand Up @@ -446,7 +446,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.title, notif.body, notif.completesAt);
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
}

Expand All @@ -473,7 +473,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.title, notif.body, notif.completesAt);
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
}

Expand All @@ -500,7 +500,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.title, notif.body, notif.completesAt);
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
}

Expand Down Expand Up @@ -529,7 +529,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.title, notif.body, notif.completesAt);
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
}

Expand Down Expand Up @@ -562,7 +562,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.title, notif.body, notif.completesAt);
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
}

Expand Down Expand Up @@ -590,7 +590,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.title, notif.body, notif.completesAt);
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
}

Expand Down Expand Up @@ -623,7 +623,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.title, notif.body, notif.completesAt);
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
}

Expand Down Expand Up @@ -718,9 +718,9 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
}

Future<void> _afterNotificationWasUpdated(NotificationItem notif) async {
await _notificationService.cancelNotification(notif.key);
await _notificationService.cancelNotification(notif.key, notif.type);
if (notif.showNotification && !notif.remaining.isNegative) {
await _notificationService.scheduleNotification(notif.key, notif.title, notif.body, notif.completesAt);
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
}
}
11 changes: 6 additions & 5 deletions lib/application/notifications/notifications_bloc.dart
Expand Up @@ -63,22 +63,22 @@ class NotificationsBloc extends Bloc<NotificationsEvent, NotificationsState> {

Future<NotificationsState> _deleteNotification(int key, AppNotificationType type) async {
await _dataService.deleteNotification(key, type);
await _notificationService.cancelNotification(key);
await _notificationService.cancelNotification(key, type);
final notifications = [...state.notifications];
notifications.removeWhere((el) => el.key == key && el.type == type);
return state.copyWith.call(notifications: notifications);
}

Future<NotificationsState> _resetNotification(int key, AppNotificationType type) async {
final notif = await _dataService.resetNotification(key, type, _settingsService.serverResetTime);
await _notificationService.cancelNotification(notif.key);
await _notificationService.scheduleNotification(key, notif.title, notif.body, notif.completesAt);
await _notificationService.cancelNotification(key, type);
await _notificationService.scheduleNotification(key, type, notif.title, notif.body, notif.completesAt);
return _afterUpdatingNotification(notif);
}

Future<NotificationsState> _stopNotification(int key, AppNotificationType type) async {
final notif = await _dataService.stopNotification(key, type);
await _notificationService.cancelNotification(key);
await _notificationService.cancelNotification(key, type);
return _afterUpdatingNotification(notif);
}

Expand All @@ -100,7 +100,8 @@ class NotificationsBloc extends Bloc<NotificationsEvent, NotificationsState> {

Future<NotificationsState> _reduceHours(int key, AppNotificationType type, int hours) async {
final notif = await _dataService.reduceNotificationHours(key, type, hours);
await _notificationService.cancelNotification(key);
await _notificationService.cancelNotification(key, type);
await _notificationService.scheduleNotification(key, type, notif.title, notif.body, notif.completesAt);
return _afterUpdatingNotification(notif);
}
}
11 changes: 4 additions & 7 deletions lib/domain/services/notification_service.dart
@@ -1,4 +1,5 @@
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:genshindb/domain/enums/enums.dart';

abstract class NotificationService {
void init();
Expand All @@ -11,15 +12,11 @@ abstract class NotificationService {

Future<bool> requestIOSPermissions();

Future<void> showNotification(int id, String title, String body, {String payload});
Future<void> showNotification(int id, AppNotificationType type, String title, String body, {String payload});

Future<void> cancelNotification(int id);
Future<void> cancelNotification(int id, AppNotificationType type);

Future<void> cancelAllNotifications();

Future<void> scheduleNotification(int id, String title, String body, DateTime toBeDeliveredOn);

Future<void> scheduleDailyNotification(int id, String title, String body);

Future<void> scheduleWeeklyNotification(int id, String title, String body, DateTime startingFrom);
Future<void> scheduleNotification(int id, AppNotificationType type, String title, String body, DateTime toBeDeliveredOn);
}
94 changes: 32 additions & 62 deletions lib/infrastructure/notification_service.dart
@@ -1,6 +1,8 @@
import 'package:enum_to_string/enum_to_string.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
import 'package:genshindb/domain/enums/enums.dart';
import 'package:genshindb/domain/services/logging_service.dart';
import 'package:genshindb/domain/services/notification_service.dart';
import 'package:timezone/data/latest.dart' as tz;
Expand All @@ -11,21 +13,6 @@ const _channelName = 'Notifications';
const _channelDescription = 'Notifications from the app';
const _largeIcon = 'genshin_db';

const _androidPlatformChannelSpecifics = AndroidNotificationDetails(
_channelId,
_channelName,
_channelDescription,
importance: Importance.max,
priority: Priority.high,
enableLights: true,
color: Colors.red,
largeIcon: DrawableResourceAndroidBitmap(_largeIcon),
);

const _iOSPlatformChannelSpecifics = IOSNotificationDetails();

const _platformChannelSpecifics = NotificationDetails(android: _androidPlatformChannelSpecifics, iOS: _iOSPlatformChannelSpecifics);

class NotificationServiceImpl implements NotificationService {
final LoggingService _loggingService;

Expand Down Expand Up @@ -74,31 +61,19 @@ class NotificationServiceImpl implements NotificationService {
}

@override
Future<void> showNotification(int id, String title, String body, {String payload}) {
Future<void> showNotification(int id, AppNotificationType type, String title, String body, {String payload}) {
final specifics = _getPlatformChannelSpecifics(type, body);

if (body.length < 40) {
return _flutterLocalNotificationsPlugin.show(id, title, body, _platformChannelSpecifics, payload: payload);
return _flutterLocalNotificationsPlugin.show(id, title, body, specifics, payload: payload);
}

final androidPlatformChannelSpecificsBigStyle = AndroidNotificationDetails(
_channelId,
_channelName,
_channelDescription,
importance: Importance.max,
priority: Priority.high,
enableLights: true,
color: Colors.red,
styleInformation: BigTextStyleInformation(body),
largeIcon: const DrawableResourceAndroidBitmap(_largeIcon),
);

final platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecificsBigStyle, iOS: _iOSPlatformChannelSpecifics);

return _flutterLocalNotificationsPlugin.show(id, title, body, platformChannelSpecifics, payload: payload);
return _flutterLocalNotificationsPlugin.show(id, title, body, specifics, payload: payload);
}

@override
Future<void> cancelNotification(int id) {
return _flutterLocalNotificationsPlugin.cancel(id);
Future<void> cancelNotification(int id, AppNotificationType type) {
return _flutterLocalNotificationsPlugin.cancel(id, tag: _getTagFromNotificationType(type));
}

@override
Expand All @@ -107,51 +82,46 @@ class NotificationServiceImpl implements NotificationService {
}

@override
Future<void> scheduleNotification(int id, String title, String body, DateTime toBeDeliveredOn) async {
Future<void> scheduleNotification(int id, AppNotificationType type, String title, String body, DateTime toBeDeliveredOn) async {
final now = DateTime.now();
if (toBeDeliveredOn.isBefore(now) || toBeDeliveredOn.isAtSameMomentAs(now)) {
await showNotification(id, title, body);
await showNotification(id, type, title, body);
return;
}
final specifics = _getPlatformChannelSpecifics(type, body);
final scheduledDate = tz.TZDateTime.from(toBeDeliveredOn, _location);
return _flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
scheduledDate,
_platformChannelSpecifics,
specifics,
uiLocalNotificationDateInterpretation: null,
androidAllowWhileIdle: true,
);
}

@override
Future<void> scheduleDailyNotification(int id, String title, String body) async {
final now = DateTime.now();
//Here we set now so the notification will appear starting from tomorrow
return _flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
tz.TZDateTime.from(now, _location),
_platformChannelSpecifics,
uiLocalNotificationDateInterpretation: null,
androidAllowWhileIdle: true,
matchDateTimeComponents: DateTimeComponents.time,
NotificationDetails _getPlatformChannelSpecifics(AppNotificationType type, String body) {
final style = body.length < 40 ? null : BigTextStyleInformation(body);
final _androidPlatformChannelSpecifics = AndroidNotificationDetails(
_channelId,
_channelName,
_channelDescription,
importance: Importance.max,
priority: Priority.high,
enableLights: true,
color: Colors.red,
styleInformation: style,
largeIcon: const DrawableResourceAndroidBitmap(_largeIcon),
tag: _getTagFromNotificationType(type),
);

const _iOSPlatformChannelSpecifics = IOSNotificationDetails();

return NotificationDetails(android: _androidPlatformChannelSpecifics, iOS: _iOSPlatformChannelSpecifics);
}

@override
Future<void> scheduleWeeklyNotification(int id, String title, String body, DateTime startingFrom) async {
return _flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
tz.TZDateTime.from(startingFrom, _location),
_platformChannelSpecifics,
uiLocalNotificationDateInterpretation: null,
androidAllowWhileIdle: true,
matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime,
);
String _getTagFromNotificationType(AppNotificationType type) {
return EnumToString.convertToString(type);
}
}

0 comments on commit 19b8df7

Please sign in to comment.