Skip to content

Commit

Permalink
This should fix the remaining issues with the notifications x2
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jun 4, 2021
1 parent a37d685 commit bc21767
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/application/notification/notification_bloc.dart
Expand Up @@ -571,6 +571,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
if (s.key != null) {
final updated = await _dataService.updateWeeklyBossNotification(
s.key,
_settingsService.serverResetTime,
selectedItemKey,
s.title,
s.body,
Expand Down
1 change: 1 addition & 0 deletions lib/domain/services/data_service.dart
Expand Up @@ -249,6 +249,7 @@ abstract class DataService {

Future<NotificationItem> updateWeeklyBossNotification(
int key,
AppServerResetTimeType serverResetTimeType,
String itemKey,
String title,
String body,
Expand Down
31 changes: 23 additions & 8 deletions lib/infrastructure/data_service.dart
Expand Up @@ -803,7 +803,8 @@ class DataServiceImpl implements DataService {
String note,
}) {
final item = _notificationsResinBox.values.firstWhere((el) => el.key == key);
if (currentResinValue != item.currentResinValue) {
final isCompleted = item.completesAt.isBefore(DateTime.now());
if (currentResinValue != item.currentResinValue || isCompleted) {
item.completesAt = getNotificationDateForResin(currentResinValue);
}
item.itemKey = itemKey;
Expand All @@ -823,7 +824,8 @@ class DataServiceImpl implements DataService {
String note,
}) {
final item = _notificationsExpeditionBox.values.firstWhere((el) => el.key == key);
if (item.expeditionTimeType != expeditionTimeType.index || item.withTimeReduction != withTimeReduction) {
final isCompleted = item.completesAt.isBefore(DateTime.now());
if (item.expeditionTimeType != expeditionTimeType.index || item.withTimeReduction != withTimeReduction || isCompleted) {
final now = DateTime.now();
item.completesAt = now.add(getExpeditionDuration(expeditionTimeType, withTimeReduction));
}
Expand All @@ -844,7 +846,8 @@ class DataServiceImpl implements DataService {
String note,
}) {
final item = _notificationsFarmingMaterialBox.values.firstWhere((el) => el.key == key);
if (item.itemKey != itemKey) {
final isCompleted = item.completesAt.isBefore(DateTime.now());
if (item.itemKey != itemKey || isCompleted) {
final newDuration = _genshinService.getMaterial(itemKey).farmingRespawnDuration;
item.completesAt = DateTime.now().add(newDuration);
}
Expand All @@ -863,7 +866,8 @@ class DataServiceImpl implements DataService {
String note,
}) {
final item = _notificationsFarmingArtifactBox.values.firstWhere((el) => el.key == key);
if (type.index != item.artifactFarmingTimeType) {
final isCompleted = item.completesAt.isBefore(DateTime.now());
if (type.index != item.artifactFarmingTimeType || isCompleted) {
final newDuration = getArtifactFarmingCooldownDuration(type);
item.completesAt = DateTime.now().add(newDuration);
}
Expand All @@ -883,8 +887,9 @@ class DataServiceImpl implements DataService {
String note,
}) {
final item = _notificationsGadgetBox.values.firstWhere((el) => el.key == key);
final gadget = _genshinService.getGadget(item.itemKey);
if (item.itemKey != itemKey) {
final isCompleted = item.completesAt.isBefore(DateTime.now());
if (item.itemKey != itemKey || isCompleted) {
final gadget = _genshinService.getGadget(item.itemKey);
final now = DateTime.now();
item.completesAt = now.add(gadget.cooldownDuration);
item.itemKey = itemKey;
Expand All @@ -904,8 +909,9 @@ class DataServiceImpl implements DataService {
String note,
}) {
final item = _notificationsFurnitureBox.values.firstWhere((el) => el.key == key);
final isCompleted = item.completesAt.isBefore(DateTime.now());
item.itemKey = itemKey;
if (item.furnitureCraftingTimeType != type.index) {
if (item.furnitureCraftingTimeType != type.index || isCompleted) {
item.furnitureCraftingTimeType = type.index;
item.completesAt = DateTime.now().add(getFurnitureDuration(type));
}
Expand All @@ -926,8 +932,12 @@ class DataServiceImpl implements DataService {
String note,
}) {
final item = _notificationsRealmCurrencyBox.values.firstWhere((el) => el.key == key);
final isCompleted = item.completesAt.isBefore(DateTime.now());
item.itemKey = itemKey;
if (item.realmRankType != realmRankType.index || item.realmTrustRank != currentTrustRankLevel || item.realmCurrency != currentRealmCurrency) {
if (item.realmRankType != realmRankType.index ||
item.realmTrustRank != currentTrustRankLevel ||
item.realmCurrency != currentRealmCurrency ||
isCompleted) {
final duration = getRealmCurrencyDuration(currentRealmCurrency, currentTrustRankLevel, realmRankType);
item.completesAt = DateTime.now().add(duration);
item.realmRankType = realmRankType.index;
Expand All @@ -941,16 +951,21 @@ class DataServiceImpl implements DataService {
@override
Future<NotificationItem> updateWeeklyBossNotification(
int key,
AppServerResetTimeType serverResetTimeType,
String itemKey,
String title,
String body,
bool showNotification, {
String note,
}) {
final item = _notificationsWeeklyBossBox.values.firstWhere((el) => el.key == key);
final isCompleted = item.completesAt.isBefore(DateTime.now());
if (item.itemKey != itemKey) {
item.itemKey = itemKey;
}
if (isCompleted) {
item.completesAt = _genshinService.getNextDateForWeeklyBoss(serverResetTimeType);
}

return _updateNotification(item, title, body, note, showNotification);
}
Expand Down
49 changes: 40 additions & 9 deletions lib/infrastructure/notification_service.dart
Expand Up @@ -63,12 +63,8 @@ class NotificationServiceImpl implements NotificationService {
@override
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, specifics, payload: payload);
}

return _flutterLocalNotificationsPlugin.show(id, title, body, specifics, payload: payload);
final newId = _generateUniqueId(id, type);
return _flutterLocalNotificationsPlugin.show(newId, title, body, specifics, payload: payload);
}

@override
Expand All @@ -84,20 +80,23 @@ class NotificationServiceImpl implements NotificationService {
@override
Future<void> scheduleNotification(int id, AppNotificationType type, String title, String body, DateTime toBeDeliveredOn) async {
final now = DateTime.now();
final payload = '${id}_${_getTagFromNotificationType(type)}';
if (toBeDeliveredOn.isBefore(now) || toBeDeliveredOn.isAtSameMomentAs(now)) {
await showNotification(id, type, title, body);
await showNotification(id, type, title, body, payload: payload);
return;
}
final newId = _generateUniqueId(id, type);
final specifics = _getPlatformChannelSpecifics(type, body);
final scheduledDate = tz.TZDateTime.from(toBeDeliveredOn, _location);
return _flutterLocalNotificationsPlugin.zonedSchedule(
id,
newId,
title,
body,
scheduledDate,
specifics,
uiLocalNotificationDateInterpretation: null,
androidAllowWhileIdle: true,
payload: payload,
);
}

Expand All @@ -115,7 +114,6 @@ class NotificationServiceImpl implements NotificationService {
largeIcon: const DrawableResourceAndroidBitmap(_largeIcon),
tag: _getTagFromNotificationType(type),
);

const _iOSPlatformChannelSpecifics = IOSNotificationDetails();

return NotificationDetails(android: _androidPlatformChannelSpecifics, iOS: _iOSPlatformChannelSpecifics);
Expand All @@ -124,4 +122,37 @@ class NotificationServiceImpl implements NotificationService {
String _getTagFromNotificationType(AppNotificationType type) {
return EnumToString.convertToString(type);
}

// For some reason I need to provide a unique id even if I'm providing a custom tag
// That's why we generate this id here
int _generateUniqueId(int id, AppNotificationType type) {
final prefix = _getIdPrefix(type).toString();
final newId = '$prefix$id';
return int.parse(newId);
}

int _getIdPrefix(AppNotificationType type) {
switch (type) {
case AppNotificationType.resin:
return 10;
case AppNotificationType.expedition:
return 20;
case AppNotificationType.farmingMaterials:
return 30;
case AppNotificationType.farmingArtifacts:
return 40;
case AppNotificationType.gadget:
return 50;
case AppNotificationType.furniture:
return 60;
case AppNotificationType.realmCurrency:
return 70;
case AppNotificationType.weeklyBoss:
return 80;
case AppNotificationType.custom:
return 90;
default:
throw Exception('The provided type = $type is not a valid notification type');
}
}
}

0 comments on commit bc21767

Please sign in to comment.