Skip to content

Commit 729f9d6

Browse files
committed
Using functions instead of classes for push sub observers
1 parent 477a65d commit 729f9d6

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

example/lib/main.dart

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class MyApp extends StatefulWidget {
1313

1414
class _MyAppState extends State<MyApp>
1515
with
16-
OneSignalPushSubscriptionObserver,
1716
OneSignalNotificationLifecycleListener,
1817
OneSignalNotificationClickListener {
1918
String _debugLabelString = "";
@@ -50,7 +49,12 @@ class _MyAppState extends State<MyApp>
5049

5150
OneSignal.Notifications.clearAll();
5251

53-
OneSignal.User.pushSubscription.addObserver(this);
52+
OneSignal.User.pushSubscription.addObserver((state) {
53+
print(OneSignal.User.pushSubscription.optedIn);
54+
print(OneSignal.User.pushSubscription.id);
55+
print(OneSignal.User.pushSubscription.token);
56+
print(state.current.jsonRepresentation());
57+
});
5458

5559
OneSignal.Notifications.addPermissionObserver((state) {
5660
print("Has permission " + state.toString());
@@ -93,13 +97,6 @@ class _MyAppState extends State<MyApp>
9397
OneSignal.InAppMessages.paused(false);
9498
}
9599

96-
void onOSPushSubscriptionChange(OSPushSubscriptionChangedState state) {
97-
print(OneSignal.User.pushSubscription.optedIn);
98-
print(OneSignal.User.pushSubscription.id);
99-
print(OneSignal.User.pushSubscription.token);
100-
print(state.current.jsonRepresentation());
101-
}
102-
103100
void onClickNotification(OSNotificationClickEvent event) {
104101
print('NOTIFICATION CLICK LISTENER CALLED WITH EVENT: $event');
105102
this.setState(() {

lib/src/pushsubscription.dart

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import 'dart:async';
22
import 'package:flutter/services.dart';
33
import 'package:onesignal_flutter/src/subscription.dart';
44

5+
typedef void OnPushSubscriptionChangeObserver(
6+
OSPushSubscriptionChangedState stateChanges);
7+
58
class OneSignalPushSubscription {
69
MethodChannel _channel = const MethodChannel('OneSignal#pushsubscription');
710

811
String? _id;
912
String? _token;
1013
bool? _optedIn;
1114

12-
List<OneSignalPushSubscriptionObserver> _observers =
13-
<OneSignalPushSubscriptionObserver>[];
15+
List<OnPushSubscriptionChangeObserver> _observers =
16+
<OnPushSubscriptionChangeObserver>[];
1417
// constructor method
1518
OneSignalPushSubscription() {
1619
this._channel.setMethodCallHandler(_handleMethod);
@@ -49,12 +52,12 @@ class OneSignalPushSubscription {
4952
/// The OSPushSubscriptionObserver.onOSPushSubscriptionChanged method will be fired on the passed-in
5053
// object when the push subscription changes. This method returns the current OSPushSubscriptionState
5154
// at the time of adding this observer.
52-
void addObserver(OneSignalPushSubscriptionObserver observer) {
55+
void addObserver(OnPushSubscriptionChangeObserver observer) {
5356
_observers.add(observer);
5457
}
5558

5659
// Remove a push subscription observer that has been previously added.
57-
void removeObserver(OneSignalPushSubscriptionObserver observer) {
60+
void removeObserver(OnPushSubscriptionChangeObserver observer) {
5861
_observers.remove(observer);
5962
}
6063

@@ -81,12 +84,7 @@ class OneSignalPushSubscription {
8184
this._optedIn = stateChanges.current.optedIn;
8285

8386
for (var observer in _observers) {
84-
observer.onOSPushSubscriptionChange(stateChanges);
87+
observer(stateChanges);
8588
}
8689
}
8790
}
88-
89-
class OneSignalPushSubscriptionObserver {
90-
void onOSPushSubscriptionChange(
91-
OSPushSubscriptionChangedState stateChanges) {}
92-
}

0 commit comments

Comments
 (0)