Skip to content

Commit

Permalink
chore: car clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
IT-MikeS committed Oct 2, 2024
1 parent c0a9285 commit 1c2a77c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

import java.util.Iterator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


public class FirebaseAnalyticsPlugin extends ReflectiveCordovaPlugin {
Expand Down Expand Up @@ -110,42 +108,38 @@ private void logECommerceEvent(JSONObject params, CallbackContext callbackContex

@CordovaMethod
private void setConsent(String consentSetting, CallbackContext callbackContext) throws JSONException {
JSONArray consentSettings;
try {
consentSettings = new JSONArray(consentSetting);
} catch (JSONException e) {
callbackContext.error(e.getMessage());
return;
}

Map<FirebaseAnalytics.ConsentType, FirebaseAnalytics.ConsentStatus> consentMap = new HashMap<>();
Set<FirebaseAnalytics.ConsentType> seenTypes = new HashSet<>();

for (int i = 0; i < consentSettings.length(); i++) {
JSONObject consentItem = consentSettings.getJSONObject(i);
int typeValue = consentItem.getInt("Type");
int statusValue = consentItem.getInt("Status");

FirebaseAnalytics.ConsentType consentType = getConsentType(typeValue);
FirebaseAnalytics.ConsentStatus consentStatus = getConsentStatus(statusValue);

if (consentType != null && consentStatus != null) {
if (seenTypes.contains(consentType)) {
callbackContext.error("Error: Duplicate consent types found in the input array.");
return;
JSONArray consentSettings = new JSONArray(consentSetting);

Map<FirebaseAnalytics.ConsentType, FirebaseAnalytics.ConsentStatus> consentMap = new HashMap<>();

for (int i = 0; i < consentSettings.length(); i++) {
JSONObject consentItem = consentSettings.getJSONObject(i);
int typeValue = consentItem.getInt("Type");
int statusValue = consentItem.getInt("Status");

FirebaseAnalytics.ConsentType consentType = getConsentType(typeValue);
FirebaseAnalytics.ConsentStatus consentStatus = getConsentStatus(statusValue);

if (consentType != null && consentStatus != null) {
if (consentMap.containsKey(consentType)) {
callbackContext.error("Error: Duplicate consent types found in the input array.");
return;
}
consentMap.put(consentType, consentStatus);
} else {
Log.w(TAG, "Invalid consent type or status for item: " + consentItem);
}
seenTypes.add(consentType);
consentMap.put(consentType, consentStatus);
} else {
Log.w(TAG, "Invalid consent type or status for item: " + consentItem);
}
}

if (!consentMap.isEmpty()) {
this.firebaseAnalytics.setConsent(consentMap);
callbackContext.success();
} else {
callbackContext.error("Error: No valid consent types provided.");
if (!consentMap.isEmpty()) {
this.firebaseAnalytics.setConsent(consentMap);
callbackContext.success();
} else {
callbackContext.error("Error: No valid consent types provided.");
}
} catch (JSONException e) {
callbackContext.error(e.getMessage());
}
}

Expand Down
13 changes: 4 additions & 9 deletions src/ios/FirebaseAnalyticsPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ - (void)setConsent:(CDVInvokedUrlCommand*)command
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:error.description];
} else {
NSMutableSet *seenTypes = [NSMutableSet set];
NSMutableDictionary *firebaseConsentDict = [NSMutableDictionary dictionary];
BOOL duplicatesFound = NO;

for (NSDictionary *item in array) {
NSNumber *type = item[@"Type"];
Expand All @@ -167,22 +165,19 @@ - (void)setConsent:(CDVInvokedUrlCommand*)command
FIRConsentStatus consentStatus = [self consentStatusFromNumber:status];

if (consentType && consentStatus) {
if ([seenTypes containsObject:consentType]) {
duplicatesFound = YES;
if (firebaseConsentDict[consentType]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:@"Error: Duplicate consent types found in the input array."];
break;
} else {
[seenTypes addObject:consentType];
firebaseConsentDict[consentType] = consentStatus;
}
} else {
NSLog(@"Warning: Ignoring invalid consent type or status for: %@ & %@", type, status);
}
}

if (duplicatesFound) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:@"Error: Duplicate consent types found in the input array."];
} else {
if (pluginResult == nil) {
if (firebaseConsentDict.count > 0) {
[FIRAnalytics setConsent:firebaseConsentDict];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
Expand Down

0 comments on commit 1c2a77c

Please sign in to comment.