Skip to content

Commit

Permalink
Merge pull request #104 from qonversion/feature/sc-36113/automations
Browse files Browse the repository at this point in the history
Automations implementation. Along with updated entitlements callback fix.
  • Loading branch information
SpertsyanKM authored Nov 18, 2024
2 parents 855f535 + d28acef commit 987c318
Show file tree
Hide file tree
Showing 21 changed files with 721 additions and 168 deletions.
14 changes: 14 additions & 0 deletions plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
</js-module>

<!-- Internal files -->
<js-module src="www/AutomationsInternal.js" name="AutomationsInternal" />
<js-module src="www/QonversionInternal.js" name="QonversionInternal" />
<js-module src="www/Mapper.js" name="Mapper" />
<js-module src="www/utils.js" name="utils" />
<!-- Public files -->
<js-module src="www/QonversionConfig.js" name="QonversionConfig" />
<js-module src="www/QonversionConfigBuilder.js" name="QonversionConfigBuilder" />
<js-module src="www/enums.js" name="enums" />
<js-module src="www/Automations.js" name="Automations" />
<js-module src="www/ActionResult.js" name="ActionResult" />
<js-module src="www/AutomationsEvent.js" name="AutomationsEvent" />
<js-module src="www/Entitlement.js" name="Entitlement" />
Expand Down Expand Up @@ -45,6 +47,7 @@
<js-module src="www/User.js" name="User" />
<js-module src="www/UserProperties.js" name="UserProperties" />
<js-module src="www/UserProperty.js" name="UserProperty" />
<js-module src="www/ScreenPresentationConfig.js" name="ScreenPresentationConfig" />
<js-module src="www/SKProduct.js" name="SKProduct" />
<js-module src="www/SKProductDiscount.js" name="SKProductDiscount" />
<js-module src="www/SKSubscriptionPeriod.js" name="SKSubscriptionPeriod" />
Expand All @@ -56,8 +59,12 @@
<feature name="QonversionPlugin" >
<param name="android-package" value="com.qonversion.android.sdk.QonversionPlugin"/>
</feature>
<feature name="AutomationsPlugin" >
<param name="android-package" value="com.qonversion.android.sdk.AutomationsPlugin"/>
</feature>
</config-file>
<framework src="io.qonversion.sandwich:sandwich:5.1.7" />
<source-file src="src/android/AutomationsPlugin.java" target-dir="src/com/qonversion/android/sdk" />
<source-file src="src/android/QonversionPlugin.java" target-dir="src/com/qonversion/android/sdk" />
<source-file src="src/android/EntitiesConverter.java" target-dir="src/com/qonversion/android/sdk" />
<source-file src="src/android/Utils.java" target-dir="src/com/qonversion/android/sdk" />
Expand All @@ -67,9 +74,16 @@
<feature name="QonversionPlugin">
<param name="ios-package" value="CDVQonversionPlugin"/>
</feature>
<feature name="AutomationsPlugin">
<param name="ios-package" value="CDVAutomationsPlugin"/>
</feature>
</config-file>
<header-file src="src/ios/CDVQonversionPlugin.h" />
<source-file src="src/ios/CDVQonversionPlugin.m" />
<header-file src="src/ios/CDVAutomationsPlugin.h" />
<source-file src="src/ios/CDVAutomationsPlugin.m" />
<header-file src="src/ios/QCUtils.h" />
<source-file src="src/ios/QCUtils.m" />
<podspec>
<config>
<source url="https://github.com/CocoaPods/Specs.git"/>
Expand Down
90 changes: 90 additions & 0 deletions plugin/src/android/AutomationsPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.qonversion.android.sdk;

import com.appfeel.cordova.annotated.android.plugin.AnnotatedCordovaPlugin;
import com.appfeel.cordova.annotated.android.plugin.ExecutionThread;
import com.appfeel.cordova.annotated.android.plugin.PluginAction;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import io.qonversion.sandwich.AutomationsEventListener;
import io.qonversion.sandwich.AutomationsSandwich;
import io.qonversion.sandwich.ResultListener;
import io.qonversion.sandwich.SandwichError;

public class AutomationsPlugin extends AnnotatedCordovaPlugin implements AutomationsEventListener {

private AutomationsSandwich automationsSandwich;
private @Nullable CallbackContext automationsEventDelegate = null;

@Override
public void pluginInitialize() {
super.pluginInitialize();
automationsSandwich = new AutomationsSandwich();
}

@PluginAction(thread = ExecutionThread.MAIN, actionName = "subscribe", isAutofinish = false)
public void subscribe(CallbackContext callbackContext) {
automationsEventDelegate = callbackContext;
automationsSandwich.setDelegate(this);

PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}

@PluginAction(thread = ExecutionThread.UI, actionName = "showScreen", isAutofinish = false)
public void showScreen(String screenId, CallbackContext callbackContext) {
automationsSandwich.showScreen(screenId, new ResultListener() {
@Override
public void onSuccess(@NonNull Map<String, ?> map) {
callbackContext.success();
}

@Override
public void onError(@NonNull SandwichError error) {
Utils.rejectWithError(error, callbackContext);
}
});
}

@PluginAction(thread = ExecutionThread.WORKER, actionName = "setScreenPresentationConfig")
public void setScreenPresentationConfig(JSONObject configData, @Nullable String screenId, CallbackContext callbackContext) {
try {
final Map<String, Object> config = EntitiesConverter.toMap(configData);
automationsSandwich.setScreenPresentationConfig(config, screenId);
callbackContext.success();
} catch (JSONException e) {
e.printStackTrace();
}
}

@Override
public void onAutomationEvent(@NonNull AutomationsEventListener.Event event, @Nullable Map<String, ?> payload) {
if (automationsEventDelegate != null) {
try {
JSONObject payloadJson = null;
if (payload != null) {
payloadJson = EntitiesConverter.convertMapToJson(payload);
}

JSONObject data = new JSONObject();
data.put("event", event.getKey());
data.put("payload", payloadJson);

PluginResult result = new PluginResult(PluginResult.Status.OK, data);
result.setKeepCallback(true);
automationsEventDelegate.sendPluginResult(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
11 changes: 6 additions & 5 deletions plugin/src/android/QonversionPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@

import io.qonversion.sandwich.QonversionEventsListener;
import io.qonversion.sandwich.QonversionSandwich;
import io.qonversion.sandwich.ResultListener;
import io.qonversion.sandwich.SandwichError;

public class QonversionPlugin extends AnnotatedCordovaPlugin implements QonversionEventsListener {

private QonversionSandwich qonversionSandwich;

private @Nullable CallbackContext entitlementsUpdateDelegate = null;
private @Nullable CallbackContext automationsEventDelegate = null;

@Override
public void pluginInitialize() {
Expand Down Expand Up @@ -91,8 +90,8 @@ public void purchase(
) {
try {
List<String> contextKeysList = contextKeys == null
? null
: EntitiesConverter.convertArrayToStringList(contextKeys);
? null
: EntitiesConverter.convertArrayToStringList(contextKeys);

qonversionSandwich.purchase(
productId,
Expand Down Expand Up @@ -254,7 +253,9 @@ public void onEntitlementsUpdated(@NonNull Map<String, ?> map) {
if (entitlementsUpdateDelegate != null) {
try {
final JSONObject payload = EntitiesConverter.convertMapToJson(map);
entitlementsUpdateDelegate.success(payload);
PluginResult result = new PluginResult(PluginResult.Status.OK, payload);
result.setKeepCallback(true);
entitlementsUpdateDelegate.sendPluginResult(result);
} catch (JSONException e) {
e.printStackTrace();
}
Expand Down
22 changes: 22 additions & 0 deletions plugin/src/ios/CDVAutomationsPlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// CDVAutomationsPlugin.h
// Qonversion
//
// Created by Kamo Spertsyan on 15.11.2024.
//

#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h>

NS_ASSUME_NONNULL_BEGIN

@interface CDVAutomationsPlugin : CDVPlugin
{}

- (void)subscribe:(CDVInvokedUrlCommand *)command;
- (void)showScreen:(CDVInvokedUrlCommand *)command;
- (void)setScreenPresentationConfig:(CDVInvokedUrlCommand *)command;

@end

NS_ASSUME_NONNULL_END
65 changes: 65 additions & 0 deletions plugin/src/ios/CDVAutomationsPlugin.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// CDVAutomationsPlugin.m
// Qonversion
//
// Created by Kamo Spertsyan on 15.11.2024.
//

#import "CDVAutomationsPlugin.h"
#import "QCUtils.h"
@import QonversionSandwich;

@interface CDVAutomationsPlugin () <AutomationsEventListener>

@property (nonatomic, strong) AutomationsSandwich *automationsSandwich;
@property (nonatomic, strong, nullable) NSString *automationsEventDelegateId;

@end

@implementation CDVAutomationsPlugin

- (void)automationDidTriggerWithEvent:(NSString * _Nonnull)event payload:(NSDictionary<NSString *,id> * _Nullable)payload {
if (self.automationsEventDelegateId) {
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithObject:event forKey:@"event"];
if (payload) {
[result setObject:payload forKey:@"payload"];
}
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
[pluginResult setKeepCallbackAsBool:true];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.automationsEventDelegateId];
}
}

- (AutomationsSandwich *)automationsSandwich {
if (!_automationsSandwich) {
_automationsSandwich = [AutomationsSandwich new];
}

return _automationsSandwich;
}

- (void)subscribe:(CDVInvokedUrlCommand *)command {
self.automationsEventDelegateId = command.callbackId;

[self.automationsSandwich subscribe:self];

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[pluginResult setKeepCallbackAsBool:true];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)showScreen:(CDVInvokedUrlCommand *)command {
__block __weak CDVAutomationsPlugin *weakSelf = self;
NSString *screenId = [command argumentAtIndex:0];
[self.automationsSandwich showScreen:screenId completion:^(NSDictionary<NSString *,id> * _Nullable result, SandwichError * _Nullable error) {
[QCUtils returnCordovaResult:result error:error command:command delegate:weakSelf.commandDelegate];
}];
}

- (void)setScreenPresentationConfig:(CDVInvokedUrlCommand *)command {
NSDictionary *config = [command argumentAtIndex:0];
NSString *screenId = [command argumentAtIndex:1];
[self.automationsSandwich setScreenPresentationConfig:config forScreenId:screenId];
}

@end
Loading

0 comments on commit 987c318

Please sign in to comment.