This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.smooch.rnsmooch"> | ||
package="com.el173.rnsmooch"> | ||
</manifest> |
119 changes: 119 additions & 0 deletions
119
android/src/main/java/com/smooch/rnsmooch/ReactNativeSmooch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> getUserProperties(ReadableMap properties) { | ||
ReadableMapKeySetIterator iterator = properties.keySetIterator(); | ||
Map<String, Object> 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; | ||
} | ||
|
||
} |