-
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.
Merge pull request #81 from xmtp/daniel-revamp-example
feat: revamp example app, implement prelim android content types
- Loading branch information
Showing
47 changed files
with
2,411 additions
and
1,511 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
135 changes: 135 additions & 0 deletions
135
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,135 @@ | ||
package expo.modules.xmtpreactnativesdk.wrappers | ||
|
||
import android.util.Base64 | ||
import com.google.gson.GsonBuilder | ||
import com.google.gson.JsonObject | ||
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.ContentTypeReply | ||
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.Reply | ||
import org.xmtp.android.library.codecs.ReplyCodec | ||
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()) | ||
Client.register(ReplyCodec()) | ||
// TODO: | ||
//Client.register(CompositeCodec()) | ||
//Client.register(GroupChatMemberAddedCodec()) | ||
//Client.register(GroupChatTitleChangedCodec()) | ||
//Client.register(RemoteAttachmentCodec()) | ||
} | ||
|
||
fun fromJsonObject(obj: JsonObject): ContentJson { | ||
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 if (obj.has("reply")) { | ||
val reply = obj.get("reply").asJsonObject | ||
val nested = fromJsonObject(reply.get("content").asJsonObject) | ||
if (nested.type.id == ContentTypeReply.id) { | ||
throw Exception("Reply cannot contain a reply") | ||
} | ||
if (nested.content == null) { | ||
throw Exception("Bad reply content") | ||
} | ||
return ContentJson(ContentTypeReply, Reply( | ||
reference = reply.get("reference").asString, | ||
content = nested.content, | ||
contentType = nested.type, | ||
)) | ||
} else { | ||
throw Exception("Unknown content type") | ||
} | ||
} | ||
|
||
fun fromJson(json: String): ContentJson { | ||
val obj = JsonParser.parseString(json).asJsonObject | ||
return fromJsonObject(obj); | ||
} | ||
|
||
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, | ||
) | ||
) | ||
|
||
ContentTypeReply.id -> mapOf( | ||
"reply" to mapOf( | ||
"reference" to (content as Reply).reference, | ||
"content" to ContentJson(content.contentType, content.content).toJsonMap(), | ||
) | ||
) | ||
|
||
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,80 @@ | ||
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"; | ||
import { Button } from "react-native"; | ||
|
||
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", | ||
headerStyle: { | ||
backgroundColor: "rgb(49 0 110)", | ||
}, | ||
headerTintColor: "#fff", | ||
headerTitleStyle: { | ||
fontWeight: "bold", | ||
}, | ||
}} | ||
/> | ||
<Navigator.Screen | ||
name="test" | ||
component={TestScreen} | ||
options={{ title: "Unit Tests" }} | ||
/> | ||
<Navigator.Screen | ||
name="home" | ||
component={HomeScreen} | ||
options={({ navigation }) => ({ | ||
title: "My Conversations", | ||
headerStyle: { | ||
backgroundColor: "rgb(49 0 110)", | ||
}, | ||
headerTintColor: "#fff", | ||
headerTitleStyle: { | ||
fontWeight: "bold", | ||
}, | ||
headerRight: () => ( | ||
<Button | ||
onPress={() => navigation.navigate("conversationCreate")} | ||
title="New" | ||
color="#fff" | ||
/> | ||
), | ||
})} | ||
/> | ||
<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", | ||
}, | ||
}); |
Oops, something went wrong.