Skip to content

Commit

Permalink
Added a button to reduce the required amount of time for a notificati…
Browse files Browse the repository at this point in the history
…on to complete
  • Loading branch information
Wolfteam committed Jun 1, 2021
1 parent a3598c6 commit 80525ce
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 9 deletions.
7 changes: 7 additions & 0 deletions lib/application/notifications/notifications_bloc.dart
Expand Up @@ -36,6 +36,7 @@ class NotificationsBloc extends Bloc<NotificationsEvent, NotificationsState> {
cancelTimer();
return _initialState;
},
reduceHours: (e) async => _reduceHours(e.id, e.type, e.hoursToReduce),
);
yield s;
}
Expand Down Expand Up @@ -92,4 +93,10 @@ class NotificationsBloc extends Bloc<NotificationsEvent, NotificationsState> {
}
return state;
}

Future<NotificationsState> _reduceHours(int key, AppNotificationType type, int hours) async {
final notif = await _dataService.reduceNotificationHours(key, type, hours);
await _notificationService.cancelNotification(key);
return _afterUpdatingNotification(notif);
}
}
6 changes: 6 additions & 0 deletions lib/application/notifications/notifications_event.dart
Expand Up @@ -24,4 +24,10 @@ abstract class NotificationsEvent implements _$NotificationsEvent {
const factory NotificationsEvent.refresh({
@required int ticks,
}) = _Refresh;

const factory NotificationsEvent.reduceHours({
@required int id,
@required AppNotificationType type,
@required int hoursToReduce,
}) = _ReduceHour;
}
2 changes: 2 additions & 0 deletions lib/domain/services/data_service.dart
Expand Up @@ -262,4 +262,6 @@ abstract class DataService {
AppNotificationItemType notificationItemType, {
String note,
});

Future<NotificationItem> reduceNotificationHours(int key, AppNotificationType type, int hours);
}
17 changes: 17 additions & 0 deletions lib/infrastructure/data_service.dart
Expand Up @@ -936,6 +936,23 @@ class DataServiceImpl implements DataService {
return _updateNotification(item, title, body, note, showNotification);
}

@override
Future<NotificationItem> reduceNotificationHours(int key, AppNotificationType type, int hours) {
final notSupportedTypes = [AppNotificationType.realmCurrency, AppNotificationType.resin];
assert(!notSupportedTypes.contains(type));

final item = _getNotification(key, type);
final now = DateTime.now();
var completesAt = item.completesAt.subtract(Duration(hours: hours));

if (completesAt.isBefore(now)) {
completesAt = now;
}

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

void _registerAdapters() {
Hive.registerAdapter(CalculatorCharacterSkillAdapter());
Hive.registerAdapter(CalculatorItemAdapter());
Expand Down
5 changes: 3 additions & 2 deletions lib/l10n/intl_en.arb
Expand Up @@ -304,7 +304,7 @@
"addNotification": "Add Notification",
"editNotification": "Edit Notification",
"xHours": "{value} hours",
"inXHours": "In {value} hours",
"inXHours": "In {value} hour(s)",
"xEachHour": "{value} each hour",
"showNotification": "Show notification",
"expedition": "Expedition",
Expand Down Expand Up @@ -344,5 +344,6 @@
"notifInfoMsgC": "The bell icon indicates that a notification is scheduled to be shown on your device even when the app is not running.",
"notifInfoMsgD": "Some notifications allows you to change the showed image on the list",
"notifInfoMsgE": "Some Android OEMs have their own customized Android OS that can prevent applications from running in the background.\nConsequently, scheduled notifications may not work when the application is in the background on certain devices (e.g. by Xiaomi, Huawei).\nSo if you encounter this problem, you may have to look for a setting that allows you to control which applications run in the background.",
"swipeToSeeMoreOptions": "You can swipe to the right or left to see more options on each item."
"swipeToSeeMoreOptions": "You can swipe to the right or left to see more options on each item.",
"reduceTime": "Reduce Time"
}
5 changes: 3 additions & 2 deletions lib/l10n/intl_es_ES.arb
Expand Up @@ -304,7 +304,7 @@
"addNotification": "Agregar Notificación",
"editNotification": "Editar Notificación",
"xHours": "{value} horas",
"inXHours": "En {value} horas",
"inXHours": "En {value} hora(s)",
"xEachHour": "{value} cada hora",
"showNotification": "Mostrar notificación",
"expedition": "Expedición",
Expand Down Expand Up @@ -344,5 +344,6 @@
"notifInfoMsgC": "El ícono de la campana indica que una notificación esta programada para ser mostrada en tu equipo aun cuando la aplicación no está en ejecución.",
"notifInfoMsgD": "Algunas notificaciones te permiten cambiar la imagen que se muestra en la lista",
"notifInfoMsgE": "Algunos fabricantes de equipos con Android tienen su propia versión personalizada de Android lo cual puede evitar que aplicaciones corran en segundo plano.\nPor ende, las notificaciones programdas podrian no funcionar cuando la aplicación esta en segundo plano en ciertos dispositivos (por ejemplo: Xiaomi, Huawei).\nSi presentas este problema, puede que debas buscar una configuración que te permita controlar que aplicaciones corren en segundo plano.",
"swipeToSeeMoreOptions": "Puedes delizar hacia la derecha o izquierda para ver más opciones en cada item."
"swipeToSeeMoreOptions": "Puedes delizar hacia la derecha o izquierda para ver más opciones en cada item.",
"reduceTime": "Reducir Tiempo"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_it_IT.arb
Expand Up @@ -344,5 +344,6 @@
"notifInfoMsgC": "La campanellina indica che la notifica è programmata per apparire nel tuo dispositivo anche quando l’app non è in esecuzione.",
"notifInfoMsgD": "Alcune notifiche permettono di cambiare immagine di anteprima nella lista",
"notifInfoMsgE": "Alcuni Android OEMs (dispositivi) hanno un Sistema Operativo Android che impedisce di eseguire l’applicazione in background.\nDi conseguenza, alcune notifiche programmate potrebbero non funzionare su alcuni dispositivi (es. Xiaomi, Huawei) quando l’applicazione è in funzione in background.\nSe riscontri questo problema, dovresti cercare l’impostazione che regola la gestione delle applicazioni in background.",
"swipeToSeeMoreOptions": "Scorri a destra o a sinistra per visualizzare più opzioni per ciascun item."
"swipeToSeeMoreOptions": "Scorri a destra o a sinistra per visualizzare più opzioni per ciascun item.",
"reduceTime": "Reduce Time"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_ja.arb
Expand Up @@ -346,5 +346,6 @@
"notifInfoMsgC": "ベルのアイコンは、アプリが実行されていないときでも、デバイスに通知が表示されるようにスケジュールされていることを示します。",
"notifInfoMsgD": "Some notifications allows you to change the showed image on the list",
"notifInfoMsgE": "Some Android OEMs have their own customized Android OS that can prevent applications from running in the background.\nConsequently, scheduled notifications may not work when the application is in the background on certain devices (e.g. by Xiaomi, Huawei).\nSo if you encounter this problem, you may have to look for a setting that allows you to control which applications run in the background.",
"swipeToSeeMoreOptions": "右または左にスワイプすると、各項目の詳細オプションが表示されます。"
"swipeToSeeMoreOptions": "右または左にスワイプすると、各項目の詳細オプションが表示されます。",
"reduceTime": "Reduce Time"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_pt.arb
Expand Up @@ -344,5 +344,6 @@
"notifInfoMsgC": "The bell icon indicates that a notification is scheduled to be shown on your device even when the app is not running.",
"notifInfoMsgD": "Some notifications allows you to change the showed image on the list",
"notifInfoMsgE": "Some Android OEMs have their own customized Android OS that can prevent applications from running in the background.\nConsequently, scheduled notifications may not work when the application is in the background on certain devices (e.g. by Xiaomi, Huawei).\nSo if you encounter this problem, you may have to look for a setting that allows you to control which applications run in the background.",
"swipeToSeeMoreOptions": "You can swipe to the right or left to see more options on each item."
"swipeToSeeMoreOptions": "You can swipe to the right or left to see more options on each item.",
"reduceTime": "Reduce Time"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_ru.arb
Expand Up @@ -344,5 +344,6 @@
"notifInfoMsgC": "Значок колокольчика указывает на то, что на вашем устройстве запланировано отображение уведомления, даже если приложение не запущено.",
"notifInfoMsgD": "Some notifications allows you to change the showed image on the list",
"notifInfoMsgE": "Some Android OEMs have their own customized Android OS that can prevent applications from running in the background.\nConsequently, scheduled notifications may not work when the application is in the background on certain devices (e.g. by Xiaomi, Huawei).\nSo if you encounter this problem, you may have to look for a setting that allows you to control which applications run in the background.",
"swipeToSeeMoreOptions": "Вы можете свайпнуть вправо или влево, чтобы увидеть дополнительные параметры для каждого элемента."
"swipeToSeeMoreOptions": "Вы можете свайпнуть вправо или влево, чтобы увидеть дополнительные параметры для каждого элемента.",
"reduceTime": "Reduce Time"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_zh_CN.arb
Expand Up @@ -344,5 +344,6 @@
"notifInfoMsgC": "铃铛图标表示即使此App未运行,通知也会显示在你的设备上。",
"notifInfoMsgD": "部分通知允许你修改列表中显示的图片",
"notifInfoMsgE": "部分 Android 厂商的系统会阻止应用在后台运行。\n因此,当应用在后台运行时,通知在某些设备(如小米、华为)上不会正常工作。\n如果你遇到了这个问题,你可能需要在设置中打开“允许该应用在后台运行”选项。",
"swipeToSeeMoreOptions": "左滑或右滑可查看每条通知的更多选项。"
"swipeToSeeMoreOptions": "左滑或右滑可查看每条通知的更多选项。",
"reduceTime": "Reduce Time"
}
Expand Up @@ -8,6 +8,7 @@ import 'package:genshindb/domain/extensions/duration_extensions.dart';
import 'package:genshindb/domain/models/models.dart' as models;
import 'package:genshindb/generated/l10n.dart';
import 'package:genshindb/presentation/shared/circle_item.dart';
import 'package:genshindb/presentation/shared/number_picker_dialog.dart';
import 'package:genshindb/presentation/shared/styles.dart';

import '../add_edit_notification_bottom_sheet.dart';
Expand Down Expand Up @@ -75,6 +76,14 @@ class NotificationListTitle extends StatelessWidget {
foregroundColor: Colors.white,
onTap: () => context.read<NotificationsBloc>().add(NotificationsEvent.reset(id: itemKey, type: type)),
),
if (remaining.inHours > 1 && type != AppNotificationType.resin && type != AppNotificationType.realmCurrency)
IconSlideAction(
caption: s.reduceTime,
color: Colors.purpleAccent,
icon: Icons.timelapse,
foregroundColor: Colors.white,
onTap: () => _showReduceTimeModal(context),
),
],
child: ListTile(
contentPadding: Styles.edgeInsetAll5,
Expand Down Expand Up @@ -113,4 +122,26 @@ class NotificationListTitle extends StatelessWidget {
);
context.read<NotificationsBloc>().add(const NotificationsEvent.init());
}

Future<void> _showReduceTimeModal(BuildContext context) async {
final s = S.of(context);
context.read<NotificationsBloc>().cancelTimer();
final hoursToReduce = await showDialog<int>(
context: context,
builder: (_) => NumberPickerDialog(
title: s.reduceTime,
minItemLevel: 1,
maxItemLevel: remaining.inHours,
value: 1,
itemBuilder: (value) => s.inXHours(value),
),
);

context.read<NotificationsBloc>().add(const NotificationsEvent.init());
if (hoursToReduce == null) {
return;
}

context.read<NotificationsBloc>().add(NotificationsEvent.reduceHours(id: itemKey, type: type, hoursToReduce: hoursToReduce));
}
}
5 changes: 5 additions & 0 deletions lib/presentation/shared/number_picker_dialog.dart
Expand Up @@ -2,18 +2,22 @@ import 'package:flutter/material.dart';
import 'package:genshindb/generated/l10n.dart';
import 'package:numberpicker/numberpicker.dart';

typedef ItemBuilder = String Function(String);

class NumberPickerDialog extends StatefulWidget {
final int minItemLevel;
final int maxItemLevel;
final int value;
final String title;
final ItemBuilder itemBuilder;

const NumberPickerDialog({
Key key,
@required this.minItemLevel,
@required this.maxItemLevel,
@required this.value,
@required this.title,
this.itemBuilder,
}) : super(key: key);

@override
Expand Down Expand Up @@ -48,6 +52,7 @@ class _NumberPickerDialogState extends State<NumberPickerDialog> {
minValue: widget.minItemLevel,
maxValue: widget.maxItemLevel,
value: _currentValue,
textMapper: widget.itemBuilder,
infiniteLoop: true,
onChanged: (newValue) => setState(() {
_currentValue = newValue;
Expand Down

0 comments on commit 80525ce

Please sign in to comment.