diff --git a/lib/app_wrappers/HandleNotifications.dart b/lib/app_wrappers/HandleNotifications.dart new file mode 100644 index 0000000..b023cc4 --- /dev/null +++ b/lib/app_wrappers/HandleNotifications.dart @@ -0,0 +1,98 @@ +import 'dart:async'; +import 'dart:convert'; + +import 'package:flutter/material.dart'; +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:flutter_logs/flutter_logs.dart'; +import 'package:locus/constants/notifications.dart'; +import 'package:locus/constants/values.dart'; +import 'package:locus/main.dart'; +import 'package:locus/screens/ViewDetailsScreen.dart'; +import 'package:locus/services/view_service/index.dart'; +import 'package:locus/utils/PageRoute.dart'; +import 'package:permission_handler/permission_handler.dart'; +import 'package:provider/provider.dart'; + +class HandleNotifications extends StatefulWidget { + const HandleNotifications({super.key}); + + @override + State createState() => _HandleNotificationsState(); +} + +class _HandleNotificationsState extends State { + late final StreamSubscription _subscription; + + @override + void initState() { + super.initState(); + _subscription = + selectedNotificationsStream.stream.listen(_handleNotification); + } + + @override + void dispose() { + _subscription.cancel(); + + super.dispose(); + } + + void _handleNotification(final NotificationResponse notification) { + FlutterLogs.logInfo( + LOG_TAG, + "Notification", + "Notification received: ${notification.payload}", + ); + + if (notification.payload == null) { + FlutterLogs.logWarn( + LOG_TAG, + "Notification", + "----> but no payload, so ignoring.", + ); + return; + } + + try { + final data = jsonDecode(notification.payload!); + final type = NotificationActionType.values[data["type"]]; + + FlutterLogs.logInfo( + LOG_TAG, + "Notification", + "Type is $type." + ); + + switch (type) { + case NotificationActionType.openTaskView: + final viewService = context.read(); + + Navigator.of(context).push( + NativePageRoute( + context: context, + builder: (_) => + ViewDetailsScreen( + view: viewService.getViewById(data["taskViewID"]), + ), + ), + ); + break; + case NotificationActionType.openPermissionsSettings: + openAppSettings(); + + break; + } + } catch (error) { + FlutterLogs.logError( + LOG_TAG, + "Notification", + "Error handling notification: $error", + ); + } + } + + @override + Widget build(BuildContext context) { + return const SizedBox.shrink(); + } +} diff --git a/lib/main.dart b/lib/main.dart index a8a33aa..8cbb640 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -7,6 +7,7 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_logs/flutter_logs.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:locus/App.dart'; +import 'package:locus/app_wrappers/HandleNotifications.dart'; import 'package:locus/app_wrappers/LocationHistoryUpdater.dart'; import 'package:locus/app_wrappers/RegisterBackgroundListeners.dart'; import 'package:locus/app_wrappers/UniLinksHandler.dart'; @@ -106,6 +107,7 @@ void main() async { UpdateLastLocationToSettings(), RegisterBackgroundListeners(), UpdateLocaleToSettings(), + HandleNotifications(), App(), ], ), diff --git a/lib/screens/LocationsOverviewScreen.dart b/lib/screens/LocationsOverviewScreen.dart index 06e979f..3fb1012 100644 --- a/lib/screens/LocationsOverviewScreen.dart +++ b/lib/screens/LocationsOverviewScreen.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:convert'; import 'dart:core'; import 'dart:io'; import 'dart:math'; @@ -29,7 +28,6 @@ import 'package:locus/screens/locations_overview_screen_widgets/OutOfBoundMarker import 'package:locus/screens/locations_overview_screen_widgets/ShareLocationSheet.dart'; import 'package:locus/screens/locations_overview_screen_widgets/ViewLocationPopup.dart'; import 'package:locus/services/current_location_service.dart'; -import 'package:locus/services/manager_service/background_locator.dart'; import 'package:locus/services/manager_service/helpers.dart'; import 'package:locus/services/settings_service/index.dart'; import 'package:locus/services/task_service/index.dart'; @@ -50,22 +48,16 @@ import 'package:locus/widgets/MapActionsContainer.dart'; import 'package:locus/widgets/Paper.dart'; import 'package:material_design_icons_flutter/material_design_icons_flutter.dart'; import 'package:modal_bottom_sheet/modal_bottom_sheet.dart'; -import 'package:permission_handler/permission_handler.dart'; import 'package:provider/provider.dart'; -import '../constants/notifications.dart'; import '../constants/values.dart'; import '../init_quick_actions.dart'; -import '../main.dart'; import '../services/app_update_service.dart'; import '../services/location_point_service.dart'; import '../services/log_service.dart'; -import '../services/manager_service/background_fetch.dart'; -import '../utils/PageRoute.dart'; import '../utils/color.dart'; import '../utils/platform.dart'; import '../utils/theme.dart'; -import 'ViewDetailsScreen.dart'; import 'locations_overview_screen_widgets/ViewDetailsSheet.dart'; // After this threshold, locations will not be merged together anymore @@ -137,7 +129,6 @@ class _LocationsOverviewScreenState extends State final locationFetchers = context.read(); _handleViewAlarmChecker(); - _handleNotifications(); locationFetchers.addAll(viewService.views); @@ -397,46 +388,6 @@ class _LocationsOverviewScreenState extends State ); } - void _handleNotifications() { - selectedNotificationsStream.stream.listen((notification) { - FlutterLogs.logInfo( - LOG_TAG, - "Notification", - "Notification received: ${notification.payload}", - ); - - try { - final data = jsonDecode(notification.payload ?? "{}"); - final type = NotificationActionType.values[data["type"]]; - - switch (type) { - case NotificationActionType.openTaskView: - final viewService = context.read(); - - Navigator.of(context).push( - NativePageRoute( - context: context, - builder: (_) => ViewDetailsScreen( - view: viewService.getViewById(data["taskViewID"]), - ), - ), - ); - break; - case NotificationActionType.openPermissionsSettings: - openAppSettings(); - - break; - } - } catch (error) { - FlutterLogs.logError( - LOG_TAG, - "Notification", - "Error handling notification: $error", - ); - } - }); - } - void _showUpdateDialogIfRequired() async { final l10n = AppLocalizations.of(context); final appUpdateService = context.read();