-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: revamp example app, implement android types text/attachment/rea…
…ction
- Loading branch information
1 parent
d8ebda2
commit 0169520
Showing
42 changed files
with
1,293 additions
and
1,046 deletions.
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,5 +1,5 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ['universe/native', 'universe/web'], | ||
ignorePatterns: ['build'], | ||
extends: ["universe/native", "universe/web"], | ||
ignorePatterns: ["build"], | ||
}; |
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
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
106 changes: 106 additions & 0 deletions
106
android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/ContentJson.kt
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,106 @@ | ||
package expo.modules.xmtpreactnativesdk.wrappers | ||
|
||
import android.util.Base64 | ||
import com.google.gson.GsonBuilder | ||
import com.google.gson.JsonParser | ||
import com.google.protobuf.ByteString | ||
import org.xmtp.android.library.Client | ||
import org.xmtp.android.library.DecodedMessage | ||
import org.xmtp.proto.message.contents.Content.EncodedContent | ||
import org.xmtp.android.library.codecs.decoded | ||
import org.xmtp.android.library.codecs.ContentTypeAttachment | ||
import org.xmtp.android.library.codecs.ContentTypeId | ||
import org.xmtp.android.library.codecs.ContentTypeReaction | ||
import org.xmtp.android.library.codecs.ContentTypeText | ||
import org.xmtp.android.library.codecs.AttachmentCodec | ||
import org.xmtp.android.library.codecs.Attachment | ||
import org.xmtp.android.library.codecs.ReactionAction | ||
import org.xmtp.android.library.codecs.ReactionSchema | ||
import org.xmtp.android.library.codecs.ReactionCodec | ||
import org.xmtp.android.library.codecs.Reaction | ||
import org.xmtp.android.library.codecs.TextCodec | ||
import org.xmtp.android.library.codecs.id | ||
|
||
import java.lang.Exception | ||
|
||
class ContentJson( | ||
val type: ContentTypeId, | ||
val content: Any?, | ||
) { | ||
constructor(encoded: EncodedContent) : this( | ||
type = encoded.type, | ||
content = encoded.decoded(), | ||
); | ||
|
||
companion object { | ||
init { | ||
Client.register(TextCodec()) | ||
Client.register(AttachmentCodec()) | ||
Client.register(ReactionCodec()) | ||
// TODO: | ||
//Client.register(ReplyCodec()) | ||
//Client.register(CompositeCodec()) | ||
//Client.register(GroupChatMemberAddedCodec()) | ||
//Client.register(GroupChatTitleChangedCodec()) | ||
//Client.register(RemoteAttachmentCodec()) | ||
} | ||
|
||
fun fromJson(json: String): ContentJson { | ||
val obj = JsonParser.parseString(json).asJsonObject | ||
if (obj.has("text")) { | ||
return ContentJson(ContentTypeText, obj.get("text").asString) | ||
} else if (obj.has("attachment")) { | ||
val attachment = obj.get("attachment").asJsonObject | ||
return ContentJson(ContentTypeAttachment, Attachment( | ||
filename = attachment.get("filename").asString, | ||
mimeType = attachment.get("mimeType").asString, | ||
data = ByteString.copyFrom(bytesFrom64(attachment.get("data").asString)), | ||
)) | ||
} else if (obj.has("reaction")) { | ||
val reaction = obj.get("reaction").asJsonObject | ||
return ContentJson(ContentTypeReaction, Reaction( | ||
reference = reaction.get("reference").asString, | ||
action = ReactionAction.valueOf(reaction.get("action").asString), | ||
schema = ReactionSchema.valueOf(reaction.get("schema").asString), | ||
content = reaction.get("content").asString, | ||
)) | ||
} else { | ||
throw Exception("Unknown content type") | ||
} | ||
} | ||
|
||
fun bytesFrom64(bytes64: String): ByteArray = Base64.decode(bytes64, Base64.DEFAULT) | ||
fun bytesTo64(bytes: ByteArray): String = Base64.encodeToString(bytes, Base64.DEFAULT) | ||
} | ||
|
||
fun toJsonMap(): Map<String, Any> { | ||
return when (type.id) { | ||
ContentTypeText.id -> mapOf( | ||
"text" to (content as String? ?: ""), | ||
) | ||
|
||
ContentTypeAttachment.id -> mapOf( | ||
"attachment" to mapOf( | ||
"filename" to (content as Attachment).filename, | ||
"mimeType" to content.mimeType, | ||
"data" to bytesTo64(content.data.toByteArray()), | ||
) | ||
) | ||
|
||
ContentTypeReaction.id -> mapOf( | ||
"reaction" to mapOf( | ||
"reference" to (content as Reaction).reference, | ||
"action" to content.action, | ||
"schema" to content.schema, | ||
"content" to content.content, | ||
) | ||
) | ||
|
||
else -> mapOf( | ||
"unknown" to mapOf( | ||
"contentTypeId" to type.id | ||
) | ||
) | ||
} | ||
} | ||
} |
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
27 changes: 0 additions & 27 deletions
27
android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/EncodeMessageWrapper.kt
This file was deleted.
Oops, something went wrong.
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,45 +1,54 @@ | ||
import { ThirdwebProvider } from "@thirdweb-dev/react-native"; | ||
import React, { useState } from "react"; | ||
import { Button, SafeAreaView, StyleSheet, View } from "react-native"; | ||
import * as XMTP from "xmtp-react-native-sdk"; | ||
import React from "react"; | ||
|
||
import AuthView from "./src/AuthView"; | ||
import HomeView from "./src/HomeView"; | ||
import TestsView from "./src/TestsView"; | ||
import LaunchScreen from "./src/LaunchScreen"; | ||
import TestScreen from "./src/TestScreen"; | ||
import HomeScreen from "./src/HomeScreen"; | ||
import ConversationScreen from "./src/ConversationScreen"; | ||
import ConversationCreateScreen from "./src/ConversationCreateScreen"; | ||
import { NavigationContainer } from "@react-navigation/native"; | ||
import { XmtpContextProvider } from "./src/XmtpContext"; | ||
import { Navigator } from "./src/Navigation"; | ||
import { QueryClient, QueryClientProvider } from "react-query"; | ||
|
||
const queryClient = new QueryClient(); | ||
export default function App() { | ||
const [client, setClient] = useState<XMTP.Client | null>(null); | ||
const [isTesting, setIsTesting] = useState<boolean>(false); | ||
|
||
return isTesting ? ( | ||
<SafeAreaView style={{ flexGrow: 1 }}> | ||
<TestsView /> | ||
</SafeAreaView> | ||
) : ( | ||
return ( | ||
<ThirdwebProvider activeChain="mainnet"> | ||
<SafeAreaView style={{ flexGrow: 1 }}> | ||
{client != null ? ( | ||
<HomeView client={client} /> | ||
) : ( | ||
<View> | ||
<AuthView setClient={setClient} /> | ||
<Button | ||
onPress={() => setIsTesting(true)} | ||
title="Unit tests" | ||
accessibilityLabel="Unit-tests" | ||
/> | ||
</View> | ||
)} | ||
</SafeAreaView> | ||
<QueryClientProvider client={queryClient}> | ||
<XmtpContextProvider> | ||
<NavigationContainer> | ||
<Navigator.Navigator> | ||
<Navigator.Screen | ||
name="launch" | ||
component={LaunchScreen} | ||
options={{ title: "XMTP RN Example" }} | ||
/> | ||
<Navigator.Screen | ||
name="test" | ||
component={TestScreen} | ||
options={{ title: "Unit Tests" }} | ||
/> | ||
<Navigator.Screen | ||
name="home" | ||
component={HomeScreen} | ||
options={{ title: "My Conversations" }} | ||
/> | ||
<Navigator.Screen | ||
name="conversation" | ||
component={ConversationScreen} | ||
options={{ title: "Conversation" }} | ||
initialParams={{ topic: "" }} | ||
/> | ||
<Navigator.Screen | ||
name="conversationCreate" | ||
component={ConversationCreateScreen} | ||
options={{ title: "New Conversation" }} | ||
/> | ||
</Navigator.Navigator> | ||
</NavigationContainer> | ||
</XmtpContextProvider> | ||
</QueryClientProvider> | ||
</ThirdwebProvider> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
backgroundColor: "#fff", | ||
alignItems: "center", | ||
justifyContent: "center", | ||
}, | ||
}); |
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 |
---|---|---|
|
@@ -14,7 +14,7 @@ module.exports = function (api) { | |
__dirname, | ||
"..", | ||
"src", | ||
"index.ts" | ||
"index.ts", | ||
), | ||
}, | ||
}, | ||
|
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
Oops, something went wrong.