Skip to content

Commit

Permalink
refactor: Add HandleNotifications.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Oct 11, 2023
1 parent 6c0b076 commit 6a8e6d2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 49 deletions.
98 changes: 98 additions & 0 deletions lib/app_wrappers/HandleNotifications.dart
Original file line number Diff line number Diff line change
@@ -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<HandleNotifications> createState() => _HandleNotificationsState();
}

class _HandleNotificationsState extends State<HandleNotifications> {
late final StreamSubscription<NotificationResponse> _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<ViewService>();

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();
}
}
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -106,6 +107,7 @@ void main() async {
UpdateLastLocationToSettings(),
RegisterBackgroundListeners(),
UpdateLocaleToSettings(),
HandleNotifications(),
App(),
],
),
Expand Down
49 changes: 0 additions & 49 deletions lib/screens/LocationsOverviewScreen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:core';
import 'dart:io';
import 'dart:math';
Expand Down Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -137,7 +129,6 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>
final locationFetchers = context.read<LocationFetchers>();

_handleViewAlarmChecker();
_handleNotifications();

locationFetchers.addAll(viewService.views);

Expand Down Expand Up @@ -397,46 +388,6 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>
);
}

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<ViewService>();

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<AppUpdateService>();
Expand Down

0 comments on commit 6a8e6d2

Please sign in to comment.