Skip to content

Commit

Permalink
more tweaks to the example app to get groups working
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jan 26, 2024
1 parent 4fc84ea commit 093c5e4
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ class ConversationDetailActivity : AppCompatActivity() {
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.subtitle = peerAddress?.truncatedAddress()
supportActionBar?.subtitle = if (peerAddress != null && peerAddress!!.contains(",")) {
val addresses = peerAddress?.split(",")?.toMutableList()
addresses?.joinToString(" -- ") {
it.truncatedAddress()
}
} else {
peerAddress?.truncatedAddress()
}

adapter = MessageAdapter()
binding.list.layoutManager =
Expand Down Expand Up @@ -106,10 +113,12 @@ class ConversationDetailActivity : AppCompatActivity() {
finish()
true
}

R.id.copy_address -> {
copyWalletAddress()
true
}

else -> super.onOptionsItemSelected(item)
}
}
Expand All @@ -130,10 +139,12 @@ class ConversationDetailActivity : AppCompatActivity() {
adapter.setData(uiState.listItems)
}
}

is ConversationDetailViewModel.UiState.Success -> {
binding.refresh.isRefreshing = false
adapter.setData(uiState.listItems)
}

is ConversationDetailViewModel.UiState.Error -> {
binding.refresh.isRefreshing = false
showError(uiState.message)
Expand All @@ -146,10 +157,12 @@ class ConversationDetailActivity : AppCompatActivity() {
is ConversationDetailViewModel.SendMessageState.Error -> {
showError(sendState.message)
}

ConversationDetailViewModel.SendMessageState.Loading -> {
binding.sendButton.isEnabled = false
binding.messageEditText.isEnabled = false
}

ConversationDetailViewModel.SendMessageState.Success -> {
binding.messageEditText.text.clear()
binding.messageEditText.isEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class ConversationDetailViewModel(private val savedStateHandle: SavedStateHandle
val listItems = mutableListOf<MessageListItem>()
try {
if (conversation == null) {
conversation = ClientManager.client.fetchConversation(conversationTopic)
conversation = ClientManager.client.fetchConversation(
conversationTopic,
includeGroups = true
)
}
conversation?.let {
listItems.addAll(
Expand All @@ -69,7 +72,8 @@ class ConversationDetailViewModel(private val savedStateHandle: SavedStateHandle
val streamMessages: StateFlow<MessageListItem?> =
stateFlow(viewModelScope, null) { subscriptionCount ->
if (conversation == null) {
conversation = ClientManager.client.fetchConversation(conversationTopic)
conversation =
ClientManager.client.fetchConversation(conversationTopic, includeGroups = false)
}
if (conversation != null) {
conversation!!.streamMessages()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ class ConversationViewHolder(

fun bind(item: MainViewModel.MainListItem.ConversationItem) {
conversation = item.conversation
binding.peerAddress.text = item.conversation.peerAddress.truncatedAddress()
binding.peerAddress.text = if (item.conversation.peerAddress.contains(",")) {
val addresses = item.conversation.peerAddress.split(",")
addresses.joinToString(" -- ") {
it.truncatedAddress()
}
} else {
item.conversation.peerAddress.truncatedAddress()
}
val messageBody = item.mostRecentMessage?.body.orEmpty()
val isMe = item.mostRecentMessage?.senderAddress == ClientManager.client.address
if (messageBody.isNotBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class NewGroupBottomSheet : BottomSheetDialogFragment() {

binding.addressInput2.addTextChangedListener {
if (viewModel.uiState.value is NewConversationViewModel.UiState.Loading) return@addTextChangedListener
val input = binding.addressInput1.text.trim()
val input = binding.addressInput2.text.trim()
val matcher = ADDRESS_PATTERN.matcher(input)
if (matcher.matches()) {
addresses.add(input.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PushNotificationsService : FirebaseMessagingService() {
GlobalScope.launch(Dispatchers.Main) {
ClientManager.createClient(keysData, applicationContext)
}
val conversation = ClientManager.client.fetchConversation(topic)
val conversation = ClientManager.client.fetchConversation(topic, includeGroups = true)
if (conversation == null) {
Log.e(TAG, "No keys or conversation persisted")
return
Expand Down
6 changes: 4 additions & 2 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,11 @@ class Client() {
return subscribe(topics.map { it.description })
}

fun fetchConversation(topic: String?): Conversation? {
fun fetchConversation(topic: String?, includeGroups: Boolean = false): Conversation? {
if (topic.isNullOrBlank()) return null
return conversations.list().firstOrNull { it.topic == topic }
return conversations.list(includeGroups = includeGroups).firstOrNull {
it.topic == topic
}
}

fun publish(envelopes: List<Envelope>): PublishResponse {
Expand Down
12 changes: 8 additions & 4 deletions library/src/main/java/org/xmtp/android/library/Conversation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.xmtp.android.library
import android.util.Log
import com.google.protobuf.kotlin.toByteString
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import org.xmtp.android.library.codecs.EncodedContent
import org.xmtp.android.library.messages.Envelope
import org.xmtp.android.library.messages.PagingInfoSortDirection
Expand Down Expand Up @@ -54,7 +55,7 @@ sealed class Conversation {
return when (this) {
is V1 -> conversationV1.peerAddress
is V2 -> conversationV2.peerAddress
is Group -> group.memberAddresses().toString()
is Group -> group.memberAddresses().joinToString(",")
}
}

Expand Down Expand Up @@ -107,6 +108,7 @@ sealed class Conversation {
.setKeyMaterial(conversationV2.keyMaterial.toByteString()),
),
).build()

is Group -> TODO()
}
}
Expand Down Expand Up @@ -202,7 +204,7 @@ sealed class Conversation {
return when (this) {
is V1 -> conversationV1.topic.description
is V2 -> conversationV2.topic
is Group -> group.id.toString()
is Group -> group.id.toHex()
}
}

Expand Down Expand Up @@ -241,7 +243,9 @@ sealed class Conversation {
direction = direction,
)

is Group -> group.messages()
is Group -> {
group.messages()
}
}
}

Expand Down Expand Up @@ -286,7 +290,7 @@ sealed class Conversation {
return when (this) {
is V1 -> conversationV1.streamMessages()
is V2 -> conversationV2.streamMessages()
is Group -> TODO()
is Group -> flowOf(group.messages().last()) // TODO fix this
}
}

Expand Down
3 changes: 2 additions & 1 deletion library/src/main/java/org/xmtp/android/library/Group.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {

fun messages(): List<DecodedMessage> {
return runBlocking {
libXMTPGroup.sync()
libXMTPGroup.findMessages(
opts = FfiListMessagesOptions(
sentBeforeNs = null,
Expand All @@ -68,7 +69,7 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
)
).map {
Message(client, it).decode()
}
}.drop(1).reversed()// The first message is something I can't decode because it's about adding members
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.xmtp.android.library.Client
import org.xmtp.android.library.DecodedMessage
import org.xmtp.android.library.XMTPException
import org.xmtp.android.library.codecs.EncodedContent
import org.xmtp.android.library.toHex
import uniffi.xmtpv3.FfiMessage
import java.util.Date

Expand All @@ -17,16 +18,12 @@ data class Message(val client: Client, private val libXMTPMessage: FfiMessage) {
val sentAt: Date
get() = Date(libXMTPMessage.sentAtNs / 1_000_000)

fun text(): String {
return libXMTPMessage.content.decodeToString()
}

fun decode(): DecodedMessage {
try {
return DecodedMessage(
id = id.decodeToString(),
id = id.toHex(),
client = client,
topic = id.decodeToString(),
topic = id.toHex(),
encodedContent = EncodedContent.parseFrom(libXMTPMessage.content),
senderAddress = senderAddress,
sent = sentAt
Expand Down

0 comments on commit 093c5e4

Please sign in to comment.