Skip to content

Commit

Permalink
Release 4.0.0
Browse files Browse the repository at this point in the history
Release 4.0.0
  • Loading branch information
SpertsyanKM authored Dec 14, 2022
2 parents 085e97b + 4d405cc commit 0b9f39c
Show file tree
Hide file tree
Showing 119 changed files with 1,906 additions and 1,348 deletions.
4 changes: 2 additions & 2 deletions Editor/QonversionDependencies.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<dependencies>
<androidPackages>
<androidPackage spec="io.qonversion.sandwich:sandwich:0.2.1" />
<androidPackage spec="io.qonversion.sandwich:sandwich:1.0.0" />
<androidPackage spec="com.fasterxml.jackson.core:jackson-databind:2.11.1" />
<androidPackage spec="org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.61" />
</androidPackages>
<iosPods>
<iosPod name="QonversionSandwich" version="0.2.1" />
<iosPod name="QonversionSandwich" version="1.0.0" />
</iosPods>
</dependencies>
55 changes: 55 additions & 0 deletions Runtime/Android/AutomationsWrapperAndroid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using UnityEngine;

namespace QonversionUnity
{
internal class AutomationsWrapperAndroid : IAutomationsWrapper
{
public void Initialize(string gameObjectName)
{
CallAutomations("initialize", gameObjectName);
}

public void SetNotificationsToken(string token)
{
CallAutomations("setNotificationsToken", token);
}

public bool HandleNotification(string notification)
{
return CallAutomations<bool>("handleNotification", notification);
}

public string GetNotificationCustomPayload(string notification)
{
return CallAutomations<string>("getNotificationCustomPayload", notification);
}

public void SubscribeOnAutomationEvents()
{
CallAutomations("subscribeOnAutomationEvents");
}

public void ShowScreen(string screenId, string callbackName)
{
CallAutomations("showScreen", screenId, callbackName);
}

private const string AutomationsWrapper = "com.qonversion.unitywrapper.AutomationsWrapper";

private static T CallAutomations<T>(string methodName, params object[] args)
{
using (var automations = new AndroidJavaClass(AutomationsWrapper))
{
return automations.CallStatic<T>(methodName, args);
}
}

private static void CallAutomations(string methodName, params object[] args)
{
using (var automations = new AndroidJavaClass(AutomationsWrapper))
{
automations.CallStatic(methodName, args);
}
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Android/AutomationsWrapperAndroid.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import androidx.annotation.Nullable;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import org.jetbrains.annotations.NotNull;

Expand All @@ -14,62 +17,127 @@

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

public class AutomationsWrapper implements AutomationsEventListener {
@SuppressWarnings("UnnecessaryLocalVariable")
public class AutomationsWrapper {
private static final String EVENT_SCREEN_SHOWN = "OnAutomationsScreenShown";
private static final String EVENT_ACTION_STARTED = "OnAutomationsActionStarted";
private static final String EVENT_ACTION_FAILED = "OnAutomationsActionFailed";
private static final String EVENT_ACTION_FINISHED = "OnAutomationsActionFinished";
private static final String EVENT_AUTOMATIONS_FINISHED = "OnAutomationsFinished";

public static String TAG = "AutomationsDelegate";
private final MessageSender messageSender;
private final AutomationsSandwich automationsSandwich;
private static MessageSender messageSender;
private static AutomationsSandwich automationsSandwich;

public AutomationsWrapper(MessageSender messageSender) {
this.messageSender = messageSender;
public static synchronized void initialize(String unityListener) {
messageSender = new MessageSender(unityListener);
automationsSandwich = new AutomationsSandwich();
}

public void subscribe() {
automationsSandwich.subscribe(this);
public static synchronized void subscribeOnAutomationEvents() {
automationsSandwich.setDelegate(new EventListener());
}

@Override
public void onAutomationEvent(@NonNull Event event, @Nullable Map<String, ?> data) {
String methodName = "";
switch (event) {
case ScreenShown:
methodName = EVENT_SCREEN_SHOWN;
break;
case ActionStarted:
methodName = EVENT_ACTION_STARTED;
break;
case ActionFinished:
methodName = EVENT_ACTION_FINISHED;
break;
case ActionFailed:
methodName = EVENT_ACTION_FAILED;
break;
case AutomationsFinished:
methodName = EVENT_AUTOMATIONS_FINISHED;
break;
default:
return;
public static synchronized void setNotificationsToken(String token) {
automationsSandwich.setNotificationToken(token);
}

public static synchronized boolean handleNotification(String notification) {
try {
ObjectMapper mapper = new ObjectMapper();

TypeReference<HashMap<String, String>> typeRef
= new TypeReference<HashMap<String, String>>() {
};
Map<String, String> notificationInfo = mapper.readValue(notification, typeRef);

boolean result = automationsSandwich.handleNotification(notificationInfo);

return result;
} catch (Exception e) {
return false;
}
}

@Nullable
public static synchronized String getNotificationCustomPayload(String notification) {
try {
final ObjectMapper mapper = new ObjectMapper();

final TypeReference<HashMap<String, String>> typeRef
= new TypeReference<HashMap<String, String>>() {
};
final Map<String, String> notificationInfo = mapper.readValue(notification, typeRef);

final Map<String, Object> payload = automationsSandwich.getNotificationCustomPayload(notificationInfo);
final String json = mapper.writeValueAsString(payload);

return json;
} catch (Exception e) {
return null;
}
}

public static synchronized void showScreen(String screenId, String unityCallbackName) {
automationsSandwich.showScreen(screenId, new ResultListener() {
@Override
public void onSuccess(@NonNull Map<String, ?> data) {
sendMessageToUnity(data, unityCallbackName);
}

@Override
public void onError(@NonNull SandwichError error) {
handleErrorResponse(error, unityCallbackName);
}
});
}

sendMessageToUnity(data == null ? new HashMap<>() : data, methodName);
private static void handleErrorResponse(@NotNull SandwichError error, @NotNull String methodName) {
final ObjectNode rootNode = Utils.createErrorNode(error);

sendMessageToUnity(rootNode, methodName);
}

static class EventListener implements AutomationsEventListener {
@Override
public void onAutomationEvent(@NonNull Event event, @Nullable Map<String, ?> data) {
String methodName;
switch (event) {
case ScreenShown:
methodName = EVENT_SCREEN_SHOWN;
break;
case ActionStarted:
methodName = EVENT_ACTION_STARTED;
break;
case ActionFinished:
methodName = EVENT_ACTION_FINISHED;
break;
case ActionFailed:
methodName = EVENT_ACTION_FAILED;
break;
case AutomationsFinished:
methodName = EVENT_AUTOMATIONS_FINISHED;
break;
default:
return;
}

sendMessageToUnity(data == null ? new HashMap<>() : data, methodName);
}
}

private void sendMessageToUnity(@NotNull Object objectToConvert, @NotNull String methodName) {
private static void sendMessageToUnity(@NotNull Object objectToConvert, @NotNull String methodName) {
try {
messageSender.sendMessageToUnity(objectToConvert, methodName);
} catch (JsonProcessingException e) {
handleException(e);
}
}

private void handleException(Exception e) {
private static void handleException(Exception e) {
Log.e(TAG, "An error occurred while processing automations flow: " + e.getLocalizedMessage());
}
}
Loading

0 comments on commit 0b9f39c

Please sign in to comment.