|
| 1 | +package com.el173.rnsmooch; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | + |
| 5 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 6 | +import com.facebook.react.bridge.ReactContextBaseJavaModule; |
| 7 | +import com.facebook.react.bridge.ReactMethod; |
| 8 | +import com.facebook.react.bridge.ReadableMap; |
| 9 | +import com.facebook.react.bridge.ReadableMapKeySetIterator; |
| 10 | +import com.facebook.react.bridge.ReadableType; |
| 11 | +import com.facebook.react.bridge.Promise; |
| 12 | + |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import io.smooch.core.Smooch; |
| 17 | +import io.smooch.core.SmoochCallback; |
| 18 | +import io.smooch.core.User; |
| 19 | +import io.smooch.ui.ConversationActivity; |
| 20 | + |
| 21 | +public class ReactNativeSmooch extends ReactContextBaseJavaModule { |
| 22 | + @Override |
| 23 | + public String getName() { |
| 24 | + return "SmoochManager"; |
| 25 | + } |
| 26 | + |
| 27 | + public ReactNativeSmooch(ReactApplicationContext reactContext) { |
| 28 | + super(reactContext); |
| 29 | + } |
| 30 | + |
| 31 | + @ReactMethod |
| 32 | + public void login(String userId, String jwt, final Promise promise) { |
| 33 | + Smooch.login(userId, jwt, new SmoochCallback() { |
| 34 | + @Override |
| 35 | + public void run(Response response) { |
| 36 | + if (promise != null) { |
| 37 | + if (response.getError() != null) { |
| 38 | + promise.reject("" + response.getStatus(), response.getError()); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + promise.resolve(null); |
| 43 | + } |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + @ReactMethod |
| 49 | + public void logout(final Promise promise) { |
| 50 | + Smooch.logout(new SmoochCallback() { |
| 51 | + @Override |
| 52 | + public void run(Response response) { |
| 53 | + if (response.getError() != null) { |
| 54 | + promise.reject("" + response.getStatus(), response.getError()); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + promise.resolve(null); |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + @ReactMethod |
| 64 | + public void show() { |
| 65 | + ConversationActivity.show(getReactApplicationContext(), Intent.FLAG_ACTIVITY_NEW_TASK); |
| 66 | + // ConversationActivity.builder().withFlags(Intent.FLAG_ACTIVITY_NEW_TASK).show(getReactApplicationContext()); |
| 67 | + } |
| 68 | + |
| 69 | + @ReactMethod |
| 70 | + public void close() { |
| 71 | + ConversationActivity.close(); |
| 72 | + } |
| 73 | + |
| 74 | + @ReactMethod |
| 75 | + public void getUnreadCount(Promise promise) { |
| 76 | + int unreadCount = Smooch.getConversation().getUnreadCount(); |
| 77 | + promise.resolve(unreadCount); |
| 78 | + } |
| 79 | + |
| 80 | + @ReactMethod |
| 81 | + public void setFirstName(String firstName) { |
| 82 | + User.getCurrentUser().setFirstName(firstName); |
| 83 | + } |
| 84 | + |
| 85 | + @ReactMethod |
| 86 | + public void setLastName(String lastName) { |
| 87 | + User.getCurrentUser().setLastName(lastName); |
| 88 | + } |
| 89 | + |
| 90 | + @ReactMethod |
| 91 | + public void setEmail(String email) { |
| 92 | + User.getCurrentUser().setEmail(email); |
| 93 | + } |
| 94 | + |
| 95 | + @ReactMethod |
| 96 | + public void setUserProperties(ReadableMap properties) { |
| 97 | + User.getCurrentUser().addProperties(getUserProperties(properties)); |
| 98 | + } |
| 99 | + |
| 100 | + private Map<String, Object> getUserProperties(ReadableMap properties) { |
| 101 | + ReadableMapKeySetIterator iterator = properties.keySetIterator(); |
| 102 | + Map<String, Object> userProperties = new HashMap<>(); |
| 103 | + |
| 104 | + while (iterator.hasNextKey()) { |
| 105 | + String key = iterator.nextKey(); |
| 106 | + ReadableType type = properties.getType(key); |
| 107 | + if (type == ReadableType.Boolean) { |
| 108 | + userProperties.put(key, properties.getBoolean(key)); |
| 109 | + } else if (type == ReadableType.Number) { |
| 110 | + userProperties.put(key, properties.getDouble(key)); |
| 111 | + } else if (type == ReadableType.String) { |
| 112 | + userProperties.put(key, properties.getString(key)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return userProperties; |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments