-
-
Notifications
You must be signed in to change notification settings - Fork 57
Suggestion to delete entire timetable when user starts to delete the timetable #2134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f56361
Suggestion to delete entire timetable when user starts to delete the …
nilsreichardt d2a6bfd
Merge remote-tracking branch 'origin/main' into codex/delete-entire-t…
nilsreichardt 37e2034
Refactoring
nilsreichardt 19141b6
Do not await deletion
nilsreichardt e3b4b99
Refactor & fix bug
nilsreichardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
232 changes: 232 additions & 0 deletions
232
app/lib/timetable/src/lesson_delete_all_suggestion.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| // Copyright (c) 2022 Sharezone UG (haftungsbeschränkt) | ||
| // Licensed under the EUPL-1.2-or-later. | ||
| // | ||
| // You may obtain a copy of the Licence at: | ||
| // https://joinup.ec.europa.eu/software/page/eupl | ||
| // | ||
| // SPDX-License-Identifier: EUPL-1.2 | ||
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:bloc_provider/bloc_provider.dart'; | ||
| import 'package:clock/clock.dart'; | ||
| import 'package:flutter/cupertino.dart'; | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:group_domain_models/group_domain_models.dart'; | ||
| import 'package:overlay_support/overlay_support.dart'; | ||
| import 'package:sharezone/main/application_bloc.dart'; | ||
| import 'package:sharezone/timetable/src/models/lesson.dart'; | ||
| import 'package:sharezone/timetable/timetable_permissions.dart'; | ||
| import 'package:sharezone_widgets/sharezone_widgets.dart'; | ||
| import 'package:sharezone_localizations/sharezone_localizations.dart'; | ||
|
|
||
| class LessonDeleteAllSuggestion { | ||
| static const Duration _window = Duration(seconds: 90); | ||
| static const Duration _cooldown = Duration(seconds: 90); | ||
| static const int _threshold = 3; | ||
|
|
||
| static final List<DateTime> _recentDeletes = <DateTime>[]; | ||
| static DateTime? _lastShownAt; | ||
|
|
||
| static void recordLessonDeletion(BuildContext context) { | ||
| final now = clock.now(); | ||
| _recentDeletes.removeWhere((time) => now.difference(time) > _window); | ||
| _recentDeletes.add(now); | ||
|
|
||
| if (_recentDeletes.length <= _threshold) return; | ||
| _maybeShow(context, now); | ||
| } | ||
|
|
||
| static void _maybeShow(BuildContext context, DateTime now) { | ||
| if (_lastShownAt != null && now.difference(_lastShownAt!) < _cooldown) { | ||
| return; | ||
| } | ||
|
|
||
| _lastShownAt = now; | ||
| showOverlayNotification( | ||
| (overlayContext) => OverlayCard( | ||
| title: Text(overlayContext.l10n.timetableDeleteAllSuggestionTitle), | ||
| content: Text(overlayContext.l10n.timetableDeleteAllSuggestionBody), | ||
| actionText: overlayContext.l10n.timetableDeleteAllSuggestionAction, | ||
| onAction: () async { | ||
| OverlaySupportEntry.of(overlayContext)!.dismiss(); | ||
| await _confirmDeleteAllLessons(overlayContext); | ||
| }, | ||
| onClose: () => OverlaySupportEntry.of(overlayContext)!.dismiss(), | ||
| ), | ||
| duration: const Duration(seconds: 8), | ||
| ); | ||
| } | ||
|
|
||
| static Future<void> _confirmDeleteAllLessons(BuildContext context) async { | ||
| final api = BlocProvider.of<SharezoneContext>(context).api; | ||
| final lessons = await api.timetable.streamLessons().first; | ||
| final groupInfos = await api.course | ||
| .getGroupInfoStream(api.schoolClassGateway) | ||
| .first; | ||
| final deletableLessons = _filterDeletableLessons(lessons, groupInfos); | ||
|
|
||
| if (deletableLessons.isEmpty) { | ||
| showSnackSec( | ||
| text: context.l10n.timetableSettingsDeleteAllLessonsSubtitleNoAccess, | ||
| context: context, | ||
| seconds: 2, | ||
| behavior: SnackBarBehavior.fixed, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| final confirmed = await showDeleteAllLessonsConfirmationDialog( | ||
| context, | ||
| deletableLessonsCount: deletableLessons.length, | ||
| ); | ||
|
|
||
| if (confirmed == true && context.mounted) { | ||
| await api.timetable.deleteLessons(deletableLessons); | ||
| if (!context.mounted) return; | ||
| showSnackSec( | ||
| text: context.l10n.timetableSettingsDeleteAllLessonsConfirmation, | ||
| context: context, | ||
| seconds: 2, | ||
| behavior: SnackBarBehavior.fixed, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| static List<Lesson> _filterDeletableLessons( | ||
| List<Lesson> lessons, | ||
| Map<String, GroupInfo> groupInfos, | ||
| ) { | ||
| return lessons.where((lesson) { | ||
| final role = groupInfos[lesson.groupID]?.myRole; | ||
| if (role == null) return false; | ||
| return hasPermissionToManageLessons(role); | ||
| }).toList(); | ||
| } | ||
| } | ||
|
|
||
| Future<bool?> showDeleteAllLessonsConfirmationDialog( | ||
| BuildContext context, { | ||
| required int deletableLessonsCount, | ||
| }) async { | ||
| if (ThemePlatform.isCupertino) { | ||
| return showCupertinoDialog<bool>( | ||
| context: context, | ||
| builder: | ||
| (context) => DeleteAllLessonsConfirmationDialog( | ||
| deletableLessonsCount: deletableLessonsCount, | ||
| ), | ||
| ); | ||
| } | ||
| return showDialog<bool>( | ||
| context: context, | ||
| builder: | ||
| (context) => DeleteAllLessonsConfirmationDialog( | ||
| deletableLessonsCount: deletableLessonsCount, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| class DeleteAllLessonsConfirmationDialog extends StatefulWidget { | ||
| const DeleteAllLessonsConfirmationDialog({ | ||
| super.key, | ||
| required this.deletableLessonsCount, | ||
| }); | ||
|
|
||
| final int deletableLessonsCount; | ||
|
|
||
| @override | ||
| State<DeleteAllLessonsConfirmationDialog> createState() => | ||
| _DeleteAllLessonsConfirmationDialogState(); | ||
| } | ||
|
|
||
| class _DeleteAllLessonsConfirmationDialogState | ||
| extends State<DeleteAllLessonsConfirmationDialog> { | ||
| static const int _initialCountdownSeconds = 10; | ||
| int _remainingSeconds = _initialCountdownSeconds; | ||
| Timer? _timer; | ||
|
|
||
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| _timer = Timer.periodic(const Duration(seconds: 1), (timer) { | ||
| if (!mounted) return; | ||
| if (_remainingSeconds <= 0) { | ||
| timer.cancel(); | ||
| return; | ||
| } | ||
| setState(() { | ||
| _remainingSeconds -= 1; | ||
| }); | ||
| if (_remainingSeconds <= 0) { | ||
| timer.cancel(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| _timer?.cancel(); | ||
| super.dispose(); | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| if (ThemePlatform.isCupertino) { | ||
| return CupertinoAlertDialog( | ||
| title: Text(context.l10n.timetableSettingsDeleteAllLessonsDialogTitle), | ||
| content: Text( | ||
| context.l10n.timetableSettingsDeleteAllLessonsDialogBody( | ||
| widget.deletableLessonsCount, | ||
| ), | ||
| ), | ||
| actions: <Widget>[ | ||
| CupertinoDialogAction( | ||
| onPressed: () => Navigator.pop(context, false), | ||
| child: Text(context.l10n.commonActionsCancel), | ||
| ), | ||
| CupertinoDialogAction( | ||
| isDestructiveAction: true, | ||
| onPressed: | ||
| _remainingSeconds == 0 | ||
| ? () => Navigator.pop(context, true) | ||
| : null, | ||
| child: Text(_deleteLabel(context)), | ||
| ), | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| return AlertDialog( | ||
| title: Text(context.l10n.timetableSettingsDeleteAllLessonsDialogTitle), | ||
| content: Text( | ||
| context.l10n.timetableSettingsDeleteAllLessonsDialogBody( | ||
| widget.deletableLessonsCount, | ||
| ), | ||
| ), | ||
| actions: <Widget>[ | ||
| TextButton( | ||
| onPressed: () => Navigator.pop(context, false), | ||
| child: Text(context.l10n.commonActionsCancel.toUpperCase()), | ||
| ), | ||
| TextButton( | ||
| onPressed: | ||
| _remainingSeconds == 0 | ||
| ? () => Navigator.pop(context, true) | ||
| : null, | ||
| style: TextButton.styleFrom(foregroundColor: Colors.red), | ||
| child: Text(_deleteLabel(context).toUpperCase()), | ||
| ), | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| String _deleteLabel(BuildContext context) { | ||
| if (_remainingSeconds > 0) { | ||
| return context.l10n.timetableDeleteAllDialogDeleteCountdown( | ||
| _remainingSeconds, | ||
| ); | ||
| } | ||
| return context.l10n.commonActionsDelete; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) 2022 Sharezone UG (haftungsbeschränkt) | ||
| // Licensed under the EUPL-1.2-or-later. | ||
| // | ||
| // You may obtain a copy of the Licence at: | ||
| // https://joinup.ec.europa.eu/software/page/eupl | ||
| // | ||
| // SPDX-License-Identifier: EUPL-1.2 | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:sharezone/timetable/src/lesson_delete_all_suggestion.dart'; | ||
| import 'package:sharezone_localizations/sharezone_localizations.dart'; | ||
|
|
||
| void main() { | ||
| testWidgets( | ||
| 'delete all lessons dialog keeps delete disabled until countdown finishes', | ||
| (tester) async { | ||
| await tester.pumpWidget( | ||
| MaterialApp( | ||
| locale: const Locale('en'), | ||
| supportedLocales: SharezoneLocalizations.supportedLocales, | ||
| localizationsDelegates: SharezoneLocalizations.localizationsDelegates, | ||
| home: Builder( | ||
| builder: (context) { | ||
| return Scaffold( | ||
| body: Center( | ||
| child: ElevatedButton( | ||
| onPressed: | ||
| () => showDeleteAllLessonsConfirmationDialog( | ||
| context, | ||
| deletableLessonsCount: 5, | ||
| ), | ||
| child: const Text('open'), | ||
| ), | ||
| ), | ||
| ); | ||
| }, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| await tester.tap(find.text('open')); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| final deleteButtonFinder = find.widgetWithText( | ||
| TextButton, | ||
| 'DELETE (10)', | ||
| ); | ||
| expect(deleteButtonFinder, findsOneWidget); | ||
| expect( | ||
| tester.widget<TextButton>(deleteButtonFinder).onPressed, | ||
| isNull, | ||
| ); | ||
|
|
||
| await tester.pump(const Duration(seconds: 1)); | ||
| expect(find.text('DELETE (9)'), findsOneWidget); | ||
|
|
||
| await tester.pump(const Duration(seconds: 9)); | ||
| await tester.pump(); | ||
| final enabledDeleteButtonFinder = find.widgetWithText( | ||
| TextButton, | ||
| 'DELETE', | ||
| ); | ||
| expect(enabledDeleteButtonFinder, findsOneWidget); | ||
| expect( | ||
| tester.widget<TextButton>(enabledDeleteButtonFinder).onPressed, | ||
| isNotNull, | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.