Skip to content

Commit eb8df00

Browse files
maintaining same output in android as it is from IOS (#32)
Co-authored-by: mnoman09 <m.nomanshoaib09@gmail.com>
1 parent 45f206d commit eb8df00

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyFlutterClient.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ protected void activate(ArgumentsParser argumentsParser, @NonNull Result result)
199199

200200
try {
201201
Variation variation = optimizelyClient.activate(experimentKey, userId, attributes);
202-
String variationKey = variation != null ? variation.getKey() : null;
203-
result.success(createResponse(Collections.singletonMap(RequestParameterKey.VARIATION_KEY, variationKey)));
202+
result.success(createResponse(Collections.singletonMap(RequestParameterKey.VARIATION_KEY, variation.getKey())));
204203
} catch (Exception ex) {
205204
result.success(createResponse(ex.getMessage()));
206205
}
@@ -225,8 +224,7 @@ protected void getVariation(ArgumentsParser argumentsParser, @NonNull Result res
225224

226225
try {
227226
Variation variation = optimizelyClient.getVariation(experimentKey, userId, attributes);
228-
String variationKey = variation != null ? variation.getKey() : null;
229-
result.success(createResponse(Collections.singletonMap(RequestParameterKey.VARIATION_KEY, variationKey)));
227+
result.success(createResponse(Collections.singletonMap(RequestParameterKey.VARIATION_KEY, variation.getKey())));
230228
} catch (Exception ex) {
231229
result.success(createResponse(ex.getMessage()));
232230
}
@@ -276,9 +274,9 @@ protected void setForcedVariation(ArgumentsParser argumentsParser, @NonNull Resu
276274
}
277275

278276
String variationKey = argumentsParser.getVariationKey();
279-
optimizelyClient.setForcedVariation(experimentKey, userId, variationKey);
277+
Boolean success = optimizelyClient.setForcedVariation(experimentKey, userId, variationKey);
280278

281-
result.success(createResponse());
279+
result.success(createResponse(success));
282280
}
283281

284282
protected void decide(ArgumentsParser argumentsParser, @NonNull Result result) {
@@ -532,10 +530,19 @@ protected void addNotificationListener(ArgumentsParser argumentsParser, @NonNull
532530
case NotificationType.ACTIVATE: {
533531
notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(ActivateNotification.class, activateNotification -> {
534532
Map<String, Object> notificationMap = new HashMap<>();
535-
notificationMap.put(ActivateListenerKeys.EXPERIMENT_KEY, activateNotification.getExperiment().getKey());
533+
534+
Map<String, String> experimentMap = new HashMap<>();
535+
experimentMap.put(ActivateListenerKeys.ID, activateNotification.getExperiment().getId());
536+
experimentMap.put(ActivateListenerKeys.KEY, activateNotification.getExperiment().getKey());
537+
538+
Map<String, String> variationMap = new HashMap<>();
539+
variationMap.put(ActivateListenerKeys.ID, activateNotification.getVariation().getId());
540+
variationMap.put(ActivateListenerKeys.KEY, activateNotification.getVariation().getKey());
541+
542+
notificationMap.put(ActivateListenerKeys.EXPERIMENT, experimentMap);
536543
notificationMap.put(ActivateListenerKeys.USER_ID, activateNotification.getUserId());
537544
notificationMap.put(ActivateListenerKeys.ATTRIBUTES, activateNotification.getAttributes());
538-
notificationMap.put(ActivateListenerKeys.VARIATION_KEY, activateNotification.getVariation().getKey());
545+
notificationMap.put(ActivateListenerKeys.VARIATION, variationMap);
539546
invokeNotification(id, NotificationType.ACTIVATE, notificationMap);
540547
});
541548
break;
@@ -599,6 +606,11 @@ private OptimizelyUserContext getUserContext(ArgumentsParser argumentsParser) {
599606
return response;
600607
}
601608

609+
// Create response with success, empty reason and null object response
610+
private Map<String, ?> createResponse(Boolean success) {
611+
return createResponse(success, null, "");
612+
}
613+
602614
// Create response with empty reason and null object response when success is true
603615
private Map<String, ?> createResponse() {
604616
return createResponse(true, null, "");

android/src/main/java/com/optimizely/optimizely_flutter_sdk/helper_classes/Constants.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ public static class DecisionListenerKeys {
8787
}
8888

8989
public static class ActivateListenerKeys {
90-
public static final String EXPERIMENT_KEY = "experimentKey";
90+
public static final String ID = "id";
91+
public static final String KEY = "key";
92+
public static final String EXPERIMENT = "experiment";
9193
public static final String USER_ID = "userId";
9294
public static final String ATTRIBUTES = "attributes";
93-
public static final String VARIATION_KEY = "variationKey";
95+
public static final String VARIATION = "variation";
9496
}
9597

9698
public static class TrackListenerKeys {

0 commit comments

Comments
 (0)