Skip to content

Commit

Permalink
cleanup: extract common functionality into helper methods
Browse files Browse the repository at this point in the history
* Extract functionality to translate `null` and empty string ("") into NULL objects, into helper methods as they are used in multiple places.
  • Loading branch information
nan-li committed Feb 24, 2024
1 parent 12dd6c9 commit 2eed867
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
19 changes: 14 additions & 5 deletions src/android/com/onesignal/cordova/OneSignalObserverController.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ public static boolean addUserStateObserver(CallbackContext callbackContext) {
userStateObserver = new IUserStateObserver() {
@Override
public void onUserStateChange(UserChangedState state) {
UserState user = state.getCurrent();
UserState current = state.getCurrent();

if (!(user instanceof UserState)) {
if (!(current instanceof UserState)) {
return;
}

try {
JSONObject hash = new JSONObject();
hash.put("current", createUserIds(state.getCurrent()));
hash.put("current", createUserIds(current));

CallbackHelper.callbackSuccess(jsUserObserverCallBack, hash);

Expand All @@ -98,14 +98,23 @@ public void onUserStateChange(UserChangedState state) {
return true;
}

/** Helper method to return JSONObject.NULL if string is empty or nil **/
private static Object getStringOrJSONObjectNull(String str) {
if (str != null && !str.isEmpty()) {
return str;
} else {
return JSONObject.NULL;
}
}

private static JSONObject createUserIds(UserState user) {
JSONObject userIds = new JSONObject();
try {
String externalId = user.getExternalId();
String onesignalId = user.getOnesignalId();

userIds.put("externalId", externalId != null && !externalId.isEmpty() ? externalId : JSONObject.NULL);
userIds.put("onesignalId", onesignalId != null && !onesignalId.isEmpty() ? onesignalId : JSONObject.NULL);
userIds.put("externalId", getStringOrJSONObjectNull(externalId));
userIds.put("onesignalId", getStringOrJSONObjectNull(onesignalId));
} catch (JSONException e) {
e.printStackTrace();
}
Expand Down
24 changes: 12 additions & 12 deletions src/ios/OneSignalPush.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ void initOneSignalObject(NSDictionary* launchOptions) {
initialLaunchFired = true;
}

/** Helper method to return NSNull if string is empty or nil **/
NSString* getStringOrNSNull(NSString* string) {
// length method can be used on nil and strings
if (string.length > 0) {
return string;
} else {
return [NSNull null];
}
}

@implementation UIApplication(OneSignalCordovaPush)

static void injectSelectorCordova(Class newClass, SEL newSel, Class addToClass, SEL makeLikeSel) {
Expand Down Expand Up @@ -174,18 +184,8 @@ - (void)onUserStateDidChangeWithState:(OSUserChangedState * _Nonnull)state {

NSMutableDictionary *currentObject = [NSMutableDictionary new];

if (onesignalId && ![onesignalId isEqualToString:@""]) {
currentObject[@"onesignalId"] = onesignalId;
} else {
currentObject[@"onesignalId"] = [NSNull null];
}

if (externalId && ![externalId isEqualToString:@""]) {
currentObject[@"externalId"] = externalId;
} else {
currentObject[@"externalId"] = [NSNull null];
}

currentObject[@"onesignalId"] = getStringOrNSNull(onesignalId);
currentObject[@"externalId"] = getStringOrNSNull(externalId);
result[@"current"] = currentObject;

successCallback(userObserverCallbackId, result);
Expand Down

0 comments on commit 2eed867

Please sign in to comment.