Skip to content

Commit

Permalink
Merge pull request #1329 from nextcloud/refactor/neon_framework/move-…
Browse files Browse the repository at this point in the history
…push-notification-callbacks
  • Loading branch information
provokateurin authored Dec 20, 2023
2 parents 5a80232 + 7d37797 commit ab681e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
7 changes: 3 additions & 4 deletions packages/neon_framework/lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import 'package:neon_framework/src/router.dart';
import 'package:neon_framework/src/theme/neon.dart';
import 'package:neon_framework/src/theme/theme.dart';
import 'package:neon_framework/src/utils/findable.dart';
import 'package:neon_framework/src/utils/global.dart';
import 'package:neon_framework/src/utils/global_options.dart';
import 'package:neon_framework/src/utils/localizations.dart';
import 'package:neon_framework/src/utils/provider.dart';
Expand Down Expand Up @@ -149,7 +148,7 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra

if (NeonPlatform.instance.canUsePushNotifications) {
final localNotificationsPlugin = await PushUtils.initLocalNotifications();
Global.onPushNotificationReceived = (final accountID) async {
PushUtils.onPushNotificationReceived = (final accountID) async {
final account = _accountsBloc.accounts.value.tryFind(accountID);
if (account == null) {
return;
Expand All @@ -164,7 +163,7 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra

await _accountsBloc.getAppsBlocFor(account).getAppBloc<NotificationsBlocInterface>(app).refresh();
};
Global.onPushNotificationClicked = (final pushNotificationWithAccountID) async {
PushUtils.onLocalNotificationClicked = (final pushNotificationWithAccountID) async {
final account = _accountsBloc.accounts.value.tryFind(pushNotificationWithAccountID.accountID);
if (account == null) {
return;
Expand All @@ -191,7 +190,7 @@ class _NeonAppState extends State<NeonApp> with WidgetsBindingObserver, tray.Tra

final details = await localNotificationsPlugin.getNotificationAppLaunchDetails();
if (details != null && details.didNotificationLaunchApp && details.notificationResponse?.payload != null) {
await Global.onPushNotificationClicked!(
await PushUtils.onLocalNotificationClicked!(
PushNotification.fromJson(
json.decode(details.notificationResponse!.payload!) as Map<String, dynamic>,
),
Expand Down
11 changes: 0 additions & 11 deletions packages/neon_framework/lib/src/utils/global.dart

This file was deleted.

19 changes: 14 additions & 5 deletions packages/neon_framework/lib/src/utils/push_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import 'package:neon_framework/src/models/push_notification.dart';
import 'package:neon_framework/src/settings/models/storage.dart';
import 'package:neon_framework/src/theme/colors.dart';
import 'package:neon_framework/src/utils/findable.dart';
import 'package:neon_framework/src/utils/global.dart';
import 'package:neon_framework/src/utils/localizations.dart';
import 'package:neon_framework/src/utils/universal_svg_file_loader.dart';
import 'package:nextcloud/notifications.dart' as notifications;
Expand All @@ -26,6 +25,16 @@ import 'package:nextcloud/notifications.dart' as notifications;
class PushUtils {
const PushUtils._();

/// Called when a new push notification was received.
///
/// The callback will only be set if the current flutter engine was opened in the foreground.
static Future<void> Function(String accountID)? onPushNotificationReceived;

/// Called when a local notification was clicked on by the user.
///
/// The callback will only be set if the current flutter engine was opened in the foreground.
static Future<void> Function(PushNotification notification)? onLocalNotificationClicked;

static notifications.RSAKeypair loadRSAKeypair() {
const storage = AppStorage(StorageKeys.notifications);
const keyDevicePrivateKey = 'device-private-key';
Expand Down Expand Up @@ -66,8 +75,8 @@ class PushUtils {

final localNotificationsPlugin = await initLocalNotifications(
onDidReceiveNotificationResponse: (final notification) async {
if (Global.onPushNotificationClicked != null && notification.payload != null) {
await Global.onPushNotificationClicked!(
if (onLocalNotificationClicked != null && notification.payload != null) {
await onLocalNotificationClicked!(
PushNotification.fromJson(
json.decode(notification.payload!) as Map<String, dynamic>,
),
Expand All @@ -90,7 +99,7 @@ class PushUtils {
await localNotificationsPlugin.cancel(_getNotificationID(instance, pushNotification));
} else if (pushNotification.subject.deleteAll ?? false) {
await localNotificationsPlugin.cancelAll();
await Global.onPushNotificationReceived?.call(instance);
await onPushNotificationReceived?.call(instance);
} else if (pushNotification.type == 'background') {
debugPrint('Got unknown background notification ${json.encode(pushNotification.toJson())}');
} else {
Expand Down Expand Up @@ -174,7 +183,7 @@ class PushUtils {
}
}

await Global.onPushNotificationReceived?.call(instance);
await onPushNotificationReceived?.call(instance);
}
}

Expand Down

0 comments on commit ab681e9

Please sign in to comment.