Skip to content

Commit

Permalink
feat: error handling for cancelling notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimozkn committed Oct 20, 2024
1 parent 1c1b33f commit 6613dcd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 27 deletions.
42 changes: 34 additions & 8 deletions app/lib/screens/study/dashboard/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import 'package:provider/provider.dart';
import 'package:studyu_app/models/app_state.dart';
import 'package:studyu_app/routes.dart';
import 'package:studyu_app/util/app_analytics.dart';
import 'package:studyu_app/util/error_handler.dart';
import 'package:studyu_app/util/localization.dart';
import 'package:studyu_app/util/schedule_notifications.dart';
import 'package:studyu_core/core.dart';
import 'package:studyu_flutter_common/studyu_flutter_common.dart';

import '../../../models/app_error.dart';

class Settings extends StatefulWidget {
const Settings({super.key});

Expand Down Expand Up @@ -186,14 +189,26 @@ class OptOutAlertDialog extends StatelessWidget {
label: Text(AppLocalizations.of(context)!.opt_out),
style: ElevatedButton.styleFrom(backgroundColor: Colors.orange[800]),
onPressed: () async {
await subject!.softDelete();
await deleteActiveStudyReference();
if (context.mounted) await cancelNotifications(context);
if (context.mounted) {
Navigator.pushNamedAndRemoveUntil(
try {
await subject!.softDelete();
await deleteActiveStudyReference();
if (context.mounted) await cancelNotifications(context);
if (context.mounted) {
Navigator.pushNamedAndRemoveUntil(
context,
Routes.studySelection,
(_) => false,
);
}
} on SocketException catch (_) {
ErrorHandler.showSnackbar(context, "Connection error");
} on AppError catch (e) {
ErrorHandler.showSnackbar(context, e.message);
} catch (e) {
StudyULogger.error(e.toString());
ErrorHandler.showSnackbar(
context,
Routes.studySelection,
(_) => false,
"An error occured while opting out. Please try again later",
);
}
},
Expand Down Expand Up @@ -231,7 +246,18 @@ class DeleteAlertDialog extends StatelessWidget {
(_) => false,
);
}
} on SocketException catch (_) {}
} on SocketException catch (_) {
ErrorHandler.showSnackbar(context, "Connection error");
} on AppError catch (e) {
ErrorHandler.showSnackbar(context, e.message);
} catch (e) {
StudyULogger.error(e.toString());

ErrorHandler.showSnackbar(
context,
"An error occured while deleting data. Please try again later",
);
}
},
),
],
Expand Down
55 changes: 36 additions & 19 deletions app/lib/util/schedule_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:provider/provider.dart';
import 'package:studyu_app/models/app_error.dart';
import 'package:studyu_app/models/app_state.dart';
import 'package:studyu_app/util/notifications.dart';
import 'package:studyu_core/core.dart';
Expand Down Expand Up @@ -166,28 +167,44 @@ List<StudyNotification> _buildNotificationList(
Future<void>? cancelNotifications(BuildContext context) async {
if (kIsWeb) return Future.value(); // Notifications not supported on web
final appState = context.read<AppState>();
final notificationsPlugin =
appState.studyNotifications?.flutterLocalNotificationsPlugin;

await notificationsPlugin?.cancelAll();
await FlutterLocalNotificationsPlugin().cancelAll();
final list1 = await notificationsPlugin?.pendingNotificationRequests() ?? [];
final list2 =
await FlutterLocalNotificationsPlugin().pendingNotificationRequests();
StudyNotifications.scheduledNotificationsDebug = 'cleared';
if (context.mounted) context.read<AppState>().studyNotifications = null;
if (StudyNotifications.debug) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Notifications cancelled and pending notifications are empty: ${list1.isEmpty && list2.isEmpty}',

try {
final notificationsPlugin =
appState.studyNotifications?.flutterLocalNotificationsPlugin;

await notificationsPlugin?.cancelAll();
await FlutterLocalNotificationsPlugin().cancelAll();

final list1 =
await notificationsPlugin?.pendingNotificationRequests() ?? [];
final list2 =
await FlutterLocalNotificationsPlugin().pendingNotificationRequests();

StudyNotifications.scheduledNotificationsDebug = 'cleared';
if (context.mounted) context.read<AppState>().studyNotifications = null;

if (StudyNotifications.debug) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Notifications cancelled and pending notifications are empty: ${list1.isEmpty && list2.isEmpty}',
),
),
),
);
);
}
print('Notifications cancelled');
}
print('Notifications cancelled');
} catch (e) {
StudyULogger.error('Error cancelling notifications: $e');
if (context.mounted) context.read<AppState>().studyNotifications = null;

throw AppError(
AppErrorTypes.notification,
'An error occured when cancelling notifications',
);
}

return Future.value();
}

Expand Down

0 comments on commit 6613dcd

Please sign in to comment.