Skip to content

Commit 477a65d

Browse files
committed
Using functions instead of classes for permission observers
1 parent 3fa99af commit 477a65d

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

example/lib/main.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class MyApp extends StatefulWidget {
1414
class _MyAppState extends State<MyApp>
1515
with
1616
OneSignalPushSubscriptionObserver,
17-
OneSignalPermissionObserver,
1817
OneSignalNotificationLifecycleListener,
1918
OneSignalNotificationClickListener {
2019
String _debugLabelString = "";
@@ -52,7 +51,11 @@ class _MyAppState extends State<MyApp>
5251
OneSignal.Notifications.clearAll();
5352

5453
OneSignal.User.pushSubscription.addObserver(this);
55-
OneSignal.Notifications.addPermissionObserver(this);
54+
55+
OneSignal.Notifications.addPermissionObserver((state) {
56+
print("Has permission " + state.toString());
57+
});
58+
5659
OneSignal.Notifications.addClickListener(this);
5760
OneSignal.Notifications.addLifecycleListener(this);
5861
OneSignal.InAppMessages.addClickListener((event) {
@@ -90,10 +93,6 @@ class _MyAppState extends State<MyApp>
9093
OneSignal.InAppMessages.paused(false);
9194
}
9295

93-
void onNotificationPermissionDidChange(bool state) {
94-
print("Has permission " + state.toString());
95-
}
96-
9796
void onOSPushSubscriptionChange(OSPushSubscriptionChangedState state) {
9897
print(OneSignal.User.pushSubscription.optedIn);
9998
print(OneSignal.User.pushSubscription.id);

lib/src/notifications.dart

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import 'package:onesignal_flutter/src/defines.dart';
55
import 'package:onesignal_flutter/src/notification.dart';
66
import 'package:onesignal_flutter/src/permission.dart';
77

8+
typedef void OnNotificationPermissionChangeObserver(bool permission);
9+
810
class OneSignalNotificationLifecycleListener {
911
void onWillDisplayNotification(OSNotificationWillDisplayEvent event) {}
1012
}
@@ -23,8 +25,8 @@ class OneSignalNotifications {
2325
// private channels used to bridge to ObjC/Java
2426
MethodChannel _channel = const MethodChannel('OneSignal#notifications');
2527

26-
List<OneSignalPermissionObserver> _observers =
27-
<OneSignalPermissionObserver>[];
28+
List<OnNotificationPermissionChangeObserver> _observers =
29+
<OnNotificationPermissionChangeObserver>[];
2830
// constructor method
2931
OneSignalNotifications() {
3032
this._channel.setMethodCallHandler(_handleMethod);
@@ -108,12 +110,13 @@ class OneSignalNotifications {
108110
/// The OSNotificationPermissionObserver.onNotificationPermissionDidChange method will be fired on the passed-in object
109111
/// when a notification permission setting changes. This happens when the user enables or disables
110112
/// notifications for your app from the system settings outside of your app.
111-
void addPermissionObserver(OneSignalPermissionObserver observer) {
113+
void addPermissionObserver(OnNotificationPermissionChangeObserver observer) {
112114
_observers.add(observer);
113115
}
114116

115117
// Remove a push subscription observer that has been previously added.
116-
void removePermissionObserver(OneSignalPermissionObserver observer) {
118+
void removePermissionObserver(
119+
OnNotificationPermissionChangeObserver observer) {
117120
_observers.remove(observer);
118121
}
119122

@@ -145,7 +148,7 @@ class OneSignalNotifications {
145148

146149
void onNotificationPermissionDidChange(bool permission) {
147150
for (var observer in _observers) {
148-
observer.onNotificationPermissionDidChange(permission);
151+
observer(permission);
149152
}
150153
}
151154

@@ -180,7 +183,3 @@ class OneSignalNotifications {
180183
_clickListeners.remove(listener);
181184
}
182185
}
183-
184-
class OneSignalPermissionObserver {
185-
void onNotificationPermissionDidChange(bool permission) {}
186-
}

0 commit comments

Comments
 (0)