Skip to content

Commit ee4d7c6

Browse files
committed
more clean up
1 parent bbd5126 commit ee4d7c6

File tree

10 files changed

+351
-280
lines changed

10 files changed

+351
-280
lines changed

android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public class RNOneSignal extends NativeOneSignalSpec
8585
private boolean hasSetPushSubscriptionObserver = false;
8686
private boolean hasSetUserStateObserver = false;
8787

88-
private HashMap<String, INotificationWillDisplayEvent> notificationWillDisplayCache;
89-
private HashMap<String, INotificationWillDisplayEvent> preventDefaultCache;
88+
private final HashMap<String, INotificationWillDisplayEvent> notificationWillDisplayCache = new HashMap<>();
89+
private final HashMap<String, INotificationWillDisplayEvent> preventDefaultCache = new HashMap<>();
9090

9191
private boolean hasAddedNotificationForegroundListener = false;
9292
private boolean hasAddedInAppMessageLifecycleListener = false;
@@ -96,26 +96,26 @@ public class RNOneSignal extends NativeOneSignalSpec
9696
// Static reference to track current instance for cleanup on reload
9797
private static RNOneSignal currentInstance = null;
9898

99-
private IInAppMessageClickListener rnInAppClickListener = new IInAppMessageClickListener() {
99+
private final IInAppMessageClickListener rnInAppClickListener = new IInAppMessageClickListener() {
100100
@Override
101101
public void onClick(IInAppMessageClickEvent event) {
102102
try {
103103
emitOnInAppMessageClicked(
104104
RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageClickEventToMap(event)));
105105
} catch (JSONException e) {
106-
e.printStackTrace();
106+
logJSONException("onInAppMessageClicked", e);
107107
}
108108
}
109109
};
110110

111-
private IInAppMessageLifecycleListener rnInAppLifecycleListener = new IInAppMessageLifecycleListener() {
111+
private final IInAppMessageLifecycleListener rnInAppLifecycleListener = new IInAppMessageLifecycleListener() {
112112
@Override
113113
public void onWillDisplay(IInAppMessageWillDisplayEvent event) {
114114
try {
115115
emitOnInAppMessageWillDisplay(
116116
RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageWillDisplayEventToMap(event)));
117117
} catch (JSONException e) {
118-
e.printStackTrace();
118+
logJSONException("onInAppMessageWillDisplay", e);
119119
}
120120
}
121121

@@ -125,7 +125,7 @@ public void onDidDisplay(IInAppMessageDidDisplayEvent event) {
125125
emitOnInAppMessageDidDisplay(
126126
RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageDidDisplayEventToMap(event)));
127127
} catch (JSONException e) {
128-
e.printStackTrace();
128+
logJSONException("onInAppMessageDidDisplay", e);
129129
}
130130
}
131131

@@ -135,7 +135,7 @@ public void onWillDismiss(IInAppMessageWillDismissEvent event) {
135135
emitOnInAppMessageWillDismiss(
136136
RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageWillDismissEventToMap(event)));
137137
} catch (JSONException e) {
138-
e.printStackTrace();
138+
logJSONException("onInAppMessageWillDismiss", e);
139139
}
140140
}
141141

@@ -145,23 +145,27 @@ public void onDidDismiss(IInAppMessageDidDismissEvent event) {
145145
emitOnInAppMessageDidDismiss(
146146
RNUtils.convertHashMapToWritableMap(RNUtils.convertInAppMessageDidDismissEventToMap(event)));
147147
} catch (JSONException e) {
148-
e.printStackTrace();
148+
logJSONException("onInAppMessageDidDismiss", e);
149149
}
150150
}
151151
};
152152

153-
private INotificationClickListener rnNotificationClickListener = new INotificationClickListener() {
153+
private final INotificationClickListener rnNotificationClickListener = new INotificationClickListener() {
154154
@Override
155155
public void onClick(INotificationClickEvent event) {
156156
try {
157157
emitOnNotificationClicked(
158158
RNUtils.convertHashMapToWritableMap(RNUtils.convertNotificationClickEventToMap(event)));
159159
} catch (JSONException e) {
160-
e.printStackTrace();
160+
logJSONException("onNotificationClicked", e);
161161
}
162162
}
163163
};
164164

165+
private void logJSONException(String eventName, JSONException exception) {
166+
Logging.error("Failed to serialize payload for " + eventName, exception);
167+
}
168+
165169
private void removeObservers() {
166170
if (!oneSignalInitDone) {
167171
Logging.debug("OneSignal React-Native SDK not initialized yet. Could not remove observers.", null);
@@ -193,8 +197,6 @@ private void removeObservers() {
193197
public RNOneSignal(ReactApplicationContext reactContext) {
194198
super(reactContext);
195199
reactContext.addLifecycleEventListener(this);
196-
notificationWillDisplayCache = new HashMap<String, INotificationWillDisplayEvent>();
197-
preventDefaultCache = new HashMap<String, INotificationWillDisplayEvent>();
198200

199201
// Clean up previous instance if it exists (handles reload scenario)
200202
if (currentInstance != null && currentInstance != this) {
@@ -220,8 +222,9 @@ public void onHostPause() {}
220222
public void onHostResume() {}
221223

222224
@Override
223-
public void onCatalystInstanceDestroy() {
225+
public void invalidate() {
224226
removeObservers();
227+
super.invalidate();
225228
}
226229

227230
@Override
@@ -375,7 +378,7 @@ public void onWillDisplay(INotificationWillDisplayEvent event) {
375378
Logging.error("InterruptedException: " + e.toString(), null);
376379
}
377380
} catch (JSONException e) {
378-
e.printStackTrace();
381+
logJSONException("onNotificationWillDisplay", e);
379382
}
380383
}
381384

@@ -410,7 +413,7 @@ public void addPermissionObserver() {
410413
}
411414
}
412415

413-
public void removePermissionObserver() {
416+
private void removePermissionObserver() {
414417
if (hasSetPermissionObserver) {
415418
OneSignal.getNotifications().removePermissionObserver(this);
416419
hasSetPermissionObserver = false;
@@ -423,7 +426,7 @@ public void onNotificationPermissionChange(boolean permission) {
423426
emitOnPermissionChanged(RNUtils.convertHashMapToWritableMap(RNUtils.convertPermissionToMap(permission)));
424427
Logging.debug("Sending permission change event", null);
425428
} catch (JSONException e) {
426-
e.printStackTrace();
429+
logJSONException("onPermissionChanged", e);
427430
}
428431
}
429432

@@ -568,11 +571,11 @@ public void onPushSubscriptionChange(PushSubscriptionChangedState pushSubscripti
568571
RNUtils.convertPushSubscriptionChangedStateToMap(pushSubscriptionChangedState)));
569572
Logging.debug("Sending subscription change event", null);
570573
} catch (JSONException e) {
571-
e.printStackTrace();
574+
logJSONException("onSubscriptionChanged", e);
572575
}
573576
}
574577

575-
public void removePushSubscriptionObserver() {
578+
private void removePushSubscriptionObserver() {
576579
if (hasSetPushSubscriptionObserver) {
577580
OneSignal.getUser().getPushSubscription().removeObserver(this);
578581
hasSetPushSubscriptionObserver = false;
@@ -713,11 +716,11 @@ public void onUserStateChange(UserChangedState state) {
713716
emitOnUserStateChanged(RNUtils.convertHashMapToWritableMap(RNUtils.convertUserChangedStateToMap(state)));
714717
Logging.debug("Sending user state change event", null);
715718
} catch (JSONException e) {
716-
e.printStackTrace();
719+
logJSONException("onUserStateChanged", e);
717720
}
718721
}
719722

720-
public void removeUserStateObserver() {
723+
private void removeUserStateObserver() {
721724
if (hasSetUserStateObserver) {
722725
OneSignal.getUser().removeObserver(this);
723726
hasSetUserStateObserver = false;

0 commit comments

Comments
 (0)