Skip to content

Commit

Permalink
Added a daily check-in notification type
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jun 4, 2021
1 parent 42985de commit c367762
Show file tree
Hide file tree
Showing 16 changed files with 264 additions and 55 deletions.
87 changes: 58 additions & 29 deletions lib/application/notification/notification_bloc.dart
Expand Up @@ -202,6 +202,10 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
useTwentyFourHoursFormat: _settingsService.useTwentyFourHoursFormat,
);
break;
case AppNotificationType.dailyCheckIn:
images.addAll(_getImagesForDailyCheckIn(itemKey: item.itemKey, selectedImage: item.image));
state = const NotificationState.dailyCheckIn();
break;
default:
throw Exception('Invalid notification type = ${item.type}');
}
Expand Down Expand Up @@ -272,6 +276,10 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
useTwentyFourHoursFormat: _settingsService.useTwentyFourHoursFormat,
);
break;
case AppNotificationType.dailyCheckIn:
images.addAll(_getImagesForDailyCheckIn());
updatedState = const NotificationState.dailyCheckIn();
break;
default:
throw Exception('The provided app notification type = $newValue is not valid');
}
Expand Down Expand Up @@ -348,6 +356,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
furniture: _saveFurnitureNotification,
weeklyBoss: _saveWeeklyBossNotification,
realmCurrency: _saveRealmCurrencyNotification,
dailyCheckIn: _saveDailyCheckInNotification,
);

if (state.key == null) {
Expand Down Expand Up @@ -384,10 +393,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
note: s.note,
showNotification: s.showNotification,
);

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

Future<void> _saveExpeditionNotification(_ExpeditionState s) async {
Expand Down Expand Up @@ -416,9 +422,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
showNotification: s.showNotification,
withTimeReduction: s.withTimeReduction,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
await _afterNotificationWasCreated(notif);
}

Future<void> _saveFarmingArtifactNotification(_FarmingArtifactState s) async {
Expand All @@ -445,9 +449,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
note: s.note,
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
await _afterNotificationWasCreated(notif);
}

Future<void> _saveFarmingMaterialNotification(_FarmingMaterialState s) async {
Expand All @@ -472,9 +474,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
note: s.note,
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
await _afterNotificationWasCreated(notif);
}

Future<void> _saveGadgetNotification(_GadgetState s) async {
Expand All @@ -499,9 +499,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
note: s.note,
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
await _afterNotificationWasCreated(notif);
}

Future<void> _saveFurnitureNotification(_FurnitureState s) async {
Expand All @@ -528,9 +526,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
note: s.note,
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
await _afterNotificationWasCreated(notif);
}

Future<void> _saveRealmCurrencyNotification(_RealmCurrencyState s) async {
Expand Down Expand Up @@ -561,9 +557,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
note: s.note,
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
await _afterNotificationWasCreated(notif);
}

Future<void> _saveWeeklyBossNotification(_WeeklyBossState s) async {
Expand All @@ -590,14 +584,11 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
note: s.note,
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
await _afterNotificationWasCreated(notif);
}

Future<void> _saveCustomNotification(_CustomState s) async {
final selectedItemKey = _getSelectedItemKey();
final now = DateTime.now();
if (s.key != null) {
final updated = await _dataService.updateCustomNotification(
s.key,
Expand All @@ -617,15 +608,37 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
selectedItemKey,
s.title,
s.body,
now,
s.scheduledDate,
s.itemType,
note: s.note,
showNotification: s.showNotification,
);
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
await _afterNotificationWasCreated(notif);
}

Future<void> _saveDailyCheckInNotification(_DailyCheckInState s) async {
final selectedItemKey = _getSelectedItemKey();
if (s.key != null) {
final updated = await _dataService.updateDailyCheckInNotification(
s.key,
selectedItemKey,
s.title,
s.body,
s.showNotification,
note: s.note,
);
await _afterNotificationWasUpdated(updated);
return;
}

final notif = await _dataService.saveDailyCheckInNotification(
selectedItemKey,
s.title,
s.body,
note: s.note,
showNotification: s.showNotification,
);
await _afterNotificationWasCreated(notif);
}

String _getSelectedItemKey() {
Expand Down Expand Up @@ -718,6 +731,22 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
return [NotificationItemImage(itemKey: material.key, image: material.fullImagePath, isSelected: true)];
}

List<NotificationItemImage> _getImagesForDailyCheckIn({String itemKey, String selectedImage}) {
if (selectedImage.isNotNullEmptyOrWhitespace) {
return [NotificationItemImage(itemKey: itemKey, image: selectedImage, isSelected: true)];
}
//TODO: FIGURE OUT HOW CAN I REMOVE THIS KEY FROM HERE
final materials = _genshinService.getMaterials(MaterialType.currency);
final material = materials.firstWhere((el) => el.key == 'Primogem');
return [NotificationItemImage(itemKey: material.key, image: material.fullImagePath, isSelected: true)];
}

Future<void> _afterNotificationWasCreated(NotificationItem notif) async {
if (notif.showNotification) {
await _notificationService.scheduleNotification(notif.key, notif.type, notif.title, notif.body, notif.completesAt);
}
}

Future<void> _afterNotificationWasUpdated(NotificationItem notif) async {
await _notificationService.cancelNotification(notif.key, notif.type);
if (notif.showNotification && !notif.remaining.isNegative) {
Expand Down
18 changes: 18 additions & 0 deletions lib/application/notification/notification_state.dart
Expand Up @@ -203,4 +203,22 @@ abstract class NotificationState implements _$NotificationState {
@required LanguageModel language,
@Default(false) bool useTwentyFourHoursFormat,
}) = _CustomState;

@Implements(_CommonBaseState)
const factory NotificationState.dailyCheckIn({
int key,
@Default(<NotificationItemImage>[]) List<NotificationItemImage> images,
@Default(AppNotificationType.dailyCheckIn) AppNotificationType type,
@Default(true) bool showNotification,
@Default('') String title,
@Default('') String body,
@Default(false) bool isTitleValid,
@Default(false) bool isTitleDirty,
@Default(false) bool isBodyValid,
@Default(false) bool isBodyDirty,
@Default(true) bool isNoteValid,
@Default(false) bool isNoteDirty,
@Default('') String note,
@Default(false) bool showOtherImages,
}) = _DailyCheckInState;
}
2 changes: 2 additions & 0 deletions lib/domain/app_constants.dart
Expand Up @@ -31,6 +31,8 @@ const resinRefillsEach = 8;
// https://game8.co/games/Genshin-Impact/archives/301599
const serverResetHour = 4;

const dailyCheckInResetDuration = Duration(hours: 24);

//key = ascension level
//value = item level
//Remember that you can be level 80 but that doesn't mean you have ascended to level 6
Expand Down
1 change: 1 addition & 0 deletions lib/domain/enums/app_notification_type.dart
Expand Up @@ -8,4 +8,5 @@ enum AppNotificationType {
realmCurrency,
weeklyBoss,
custom,
dailyCheckIn,
}
12 changes: 12 additions & 0 deletions lib/domain/models/entities/notifications/notification_custom.dart
Expand Up @@ -57,4 +57,16 @@ class NotificationCustom extends HiveObject implements NotificationBase {
@required this.notificationItemType,
}) : type = AppNotificationType.custom.index,
originalScheduledDate = completesAt;

NotificationCustom.forDailyCheckIn({
@required this.itemKey,
@required this.createdAt,
@required this.completesAt,
this.note,
@required this.showNotification,
@required this.title,
@required this.body,
}) : type = AppNotificationType.dailyCheckIn.index,
notificationItemType = AppNotificationItemType.material.index,
originalScheduledDate = completesAt;
}
18 changes: 17 additions & 1 deletion lib/domain/services/data_service.dart
Expand Up @@ -163,13 +163,20 @@ abstract class DataService {
String itemKey,
String title,
String body,
DateTime createdAt,
DateTime completesAt,
AppNotificationItemType notificationItemType, {
String note,
bool showNotification = true,
});

Future<NotificationItem> saveDailyCheckInNotification(
String itemKey,
String title,
String body, {
String note,
bool showNotification = true,
});

Future<void> deleteNotification(int key, AppNotificationType type);

Future<NotificationItem> resetNotification(int key, AppNotificationType type, AppServerResetTimeType serverResetTimeType);
Expand Down Expand Up @@ -268,5 +275,14 @@ abstract class DataService {
String note,
});

Future<NotificationItem> updateDailyCheckInNotification(
int key,
String itemKey,
String title,
String body,
bool showNotification, {
String note,
});

Future<NotificationItem> reduceNotificationHours(int key, AppNotificationType type, int hours);
}
54 changes: 52 additions & 2 deletions lib/infrastructure/data_service.dart
Expand Up @@ -681,15 +681,15 @@ class DataServiceImpl implements DataService {
String itemKey,
String title,
String body,
DateTime createdAt,
DateTime completesAt,
AppNotificationItemType notificationItemType, {
String note,
bool showNotification = true,
}) async {
final now = DateTime.now();
final notification = NotificationCustom(
itemKey: itemKey,
createdAt: createdAt,
createdAt: now,
completesAt: completesAt,
showNotification: showNotification,
notificationItemType: notificationItemType.index,
Expand All @@ -701,6 +701,28 @@ class DataServiceImpl implements DataService {
return getNotification(key, AppNotificationType.custom);
}

@override
Future<NotificationItem> saveDailyCheckInNotification(
String itemKey,
String title,
String body, {
String note,
bool showNotification = true,
}) async {
final now = DateTime.now();
final notification = NotificationCustom.forDailyCheckIn(
itemKey: itemKey,
createdAt: now,
completesAt: now.add(dailyCheckInResetDuration),
showNotification: showNotification,
note: note?.trim(),
title: title.trim(),
body: body.trim(),
);
final key = await _notificationsCustomBox.add(notification);
return getNotification(key, AppNotificationType.dailyCheckIn);
}

@override
Future<void> deleteNotification(int key, AppNotificationType type) {
switch (type) {
Expand All @@ -721,6 +743,7 @@ class DataServiceImpl implements DataService {
case AppNotificationType.weeklyBoss:
return _notificationsWeeklyBossBox.delete(key);
case AppNotificationType.custom:
case AppNotificationType.dailyCheckIn:
return _notificationsCustomBox.delete(key);
}
throw Exception('Invalid notification type = $type');
Expand Down Expand Up @@ -778,6 +801,11 @@ class DataServiceImpl implements DataService {
return _mapToNotificationItem(item);
case AppNotificationType.custom:
break;
case AppNotificationType.dailyCheckIn:
final item = _getNotification<NotificationCustom>(key, type);
item.completesAt = DateTime.now().add(const Duration(hours: 24));
await item.save();
return _mapToNotificationItem(item);
}

throw Exception('The provided app notification type = $type is not valid for a reset');
Expand Down Expand Up @@ -993,6 +1021,26 @@ class DataServiceImpl implements DataService {
return _updateNotification(item, title, body, note, showNotification);
}

@override
Future<NotificationItem> updateDailyCheckInNotification(
int key,
String itemKey,
String title,
String body,
bool showNotification, {
String note,
}) async {
final item = _notificationsCustomBox.values.firstWhere((el) => el.key == key);
final isCompleted = item.completesAt.isBefore(DateTime.now());
item.itemKey = itemKey;

if (isCompleted) {
item.completesAt = DateTime.now().add(dailyCheckInResetDuration);
}

return _updateNotification(item, title, body, note, showNotification);
}

@override
Future<NotificationItem> reduceNotificationHours(int key, AppNotificationType type, int hours) {
final notSupportedTypes = [AppNotificationType.realmCurrency, AppNotificationType.resin];
Expand Down Expand Up @@ -1231,6 +1279,7 @@ class DataServiceImpl implements DataService {
case AppNotificationType.weeklyBoss:
return _notificationsWeeklyBossBox.values.firstWhere((el) => el.key == key) as T;
case AppNotificationType.custom:
case AppNotificationType.dailyCheckIn:
return _notificationsCustomBox.values.firstWhere((el) => el.key == key) as T;
}

Expand All @@ -1257,6 +1306,7 @@ class DataServiceImpl implements DataService {
case AppNotificationType.weeklyBoss:
return _mapToNotificationItemFromWeeklyBoss(e as NotificationWeeklyBoss);
case AppNotificationType.custom:
case AppNotificationType.dailyCheckIn:
return _mapToNotificationItemFromCustom(e as NotificationCustom);
}
throw Exception('Invalid notification type = $type');
Expand Down

0 comments on commit c367762

Please sign in to comment.