From d4975cc10dbc440b04422ccf8880c265279aa284 Mon Sep 17 00:00:00 2001 From: Hashith Karunarathne Date: Wed, 8 Apr 2020 00:44:13 +0530 Subject: [PATCH] Update the package name --- android/src/main/AndroidManifest.xml | 2 +- .../smooch/rnsmooch/ReactNativeSmooch.java | 119 ++++++++++++++++++ package.json | 2 +- 3 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 android/src/main/java/com/smooch/rnsmooch/ReactNativeSmooch.java diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 34d07e3..2b775dd 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,3 +1,3 @@ + package="com.el173.rnsmooch"> diff --git a/android/src/main/java/com/smooch/rnsmooch/ReactNativeSmooch.java b/android/src/main/java/com/smooch/rnsmooch/ReactNativeSmooch.java new file mode 100644 index 0000000..501370f --- /dev/null +++ b/android/src/main/java/com/smooch/rnsmooch/ReactNativeSmooch.java @@ -0,0 +1,119 @@ +package com.el173.rnsmooch; + +import android.content.Intent; + +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.ReadableMap; +import com.facebook.react.bridge.ReadableMapKeySetIterator; +import com.facebook.react.bridge.ReadableType; +import com.facebook.react.bridge.Promise; + +import java.util.HashMap; +import java.util.Map; + +import io.smooch.core.Smooch; +import io.smooch.core.SmoochCallback; +import io.smooch.core.User; +import io.smooch.ui.ConversationActivity; + +public class ReactNativeSmooch extends ReactContextBaseJavaModule { + @Override + public String getName() { + return "SmoochManager"; + } + + public ReactNativeSmooch(ReactApplicationContext reactContext) { + super(reactContext); + } + + @ReactMethod + public void login(String userId, String jwt, final Promise promise) { + Smooch.login(userId, jwt, new SmoochCallback() { + @Override + public void run(Response response) { + if (promise != null) { + if (response.getError() != null) { + promise.reject("" + response.getStatus(), response.getError()); + return; + } + + promise.resolve(null); + } + } + }); + } + + @ReactMethod + public void logout(final Promise promise) { + Smooch.logout(new SmoochCallback() { + @Override + public void run(Response response) { + if (response.getError() != null) { + promise.reject("" + response.getStatus(), response.getError()); + return; + } + + promise.resolve(null); + } + }); + } + + @ReactMethod + public void show() { + ConversationActivity.show(getReactApplicationContext(), Intent.FLAG_ACTIVITY_NEW_TASK); + // ConversationActivity.builder().withFlags(Intent.FLAG_ACTIVITY_NEW_TASK).show(getReactApplicationContext()); + } + + @ReactMethod + public void close() { + ConversationActivity.close(); + } + + @ReactMethod + public void getUnreadCount(Promise promise) { + int unreadCount = Smooch.getConversation().getUnreadCount(); + promise.resolve(unreadCount); + } + + @ReactMethod + public void setFirstName(String firstName) { + User.getCurrentUser().setFirstName(firstName); + } + + @ReactMethod + public void setLastName(String lastName) { + User.getCurrentUser().setLastName(lastName); + } + + @ReactMethod + public void setEmail(String email) { + User.getCurrentUser().setEmail(email); + } + + @ReactMethod + public void setUserProperties(ReadableMap properties) { + User.getCurrentUser().addProperties(getUserProperties(properties)); + } + + private Map getUserProperties(ReadableMap properties) { + ReadableMapKeySetIterator iterator = properties.keySetIterator(); + Map userProperties = new HashMap<>(); + + while (iterator.hasNextKey()) { + String key = iterator.nextKey(); + ReadableType type = properties.getType(key); + if (type == ReadableType.Boolean) { + userProperties.put(key, properties.getBoolean(key)); + } else if (type == ReadableType.Number) { + userProperties.put(key, properties.getDouble(key)); + } else if (type == ReadableType.String) { + userProperties.put(key, properties.getString(key)); + } + } + + return userProperties; + } + +} diff --git a/package.json b/package.json index 6e8171e..a8b683a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@el173/react-native-smooch", - "version": "1.1.0", + "version": "1.1.1", "description": "A React Native client for smooch.io", "main": "./lib/SmoochClient.js", "repository": {