-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render and send messages for a conversation (#44)
* save state with passing conversations * make client instance a singleton to share across the app * fix import order * rename to clientmanager since it manages the global client * Add create conversation bottom sheet (#38) * make conversation parcelable * set client in convo detail view model * add bottom sheet to create a conversation * remove parcelable in favor of querying by topic * remove parcelize plugin * no need for api anymore * use new bottom sheet on button press * fetch a conversation by topic and add test * check peer address is the same as well * add message adapter to render messages * send messages * render messages in message list * fix duplicate conversations * we need to actually mutate the object so that it returns a wallet signature * fix message width --------- Co-authored-by: Naomi Plasterer <[email protected]> Co-authored-by: Naomi Plasterer <[email protected]>
- Loading branch information
1 parent
8053d6d
commit 6e68384
Showing
16 changed files
with
350 additions
and
18 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
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
10 changes: 10 additions & 0 deletions
10
example/src/main/java/org/xmtp/android/example/extension/StringExtension.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,10 @@ | ||
package org.xmtp.android.example.extension | ||
|
||
fun String.truncatedAddress(): String { | ||
if (length > 6) { | ||
val start = 6 | ||
val end = lastIndex - 3 | ||
return replaceRange(start, end, "...") | ||
} | ||
return this | ||
} |
18 changes: 18 additions & 0 deletions
18
example/src/main/java/org/xmtp/android/example/extension/ViewExtension.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,18 @@ | ||
package org.xmtp.android.example.extension | ||
|
||
import android.view.View | ||
import android.view.ViewGroup.MarginLayoutParams | ||
|
||
fun View.margins( | ||
left: Int = 0, | ||
top: Int = 0, | ||
right: Int = 0, | ||
bottom: Int = 0 | ||
) { | ||
val layoutParams = layoutParams as MarginLayoutParams | ||
val marginLeft = left.let { if (it > 0) it else layoutParams.leftMargin } | ||
val marginTop = top.let { if (it > 0) it else layoutParams.topMargin } | ||
val marginRight = right.let { if (it > 0) it else layoutParams.rightMargin } | ||
val marginBottom = bottom.let { if (it > 0) it else layoutParams.bottomMargin } | ||
layoutParams.setMargins(marginLeft, marginTop, marginRight, marginBottom) | ||
} |
45 changes: 45 additions & 0 deletions
45
example/src/main/java/org/xmtp/android/example/message/MessageAdapter.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,45 @@ | ||
package org.xmtp.android.example.message | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import org.xmtp.android.example.conversation.ConversationDetailViewModel | ||
import org.xmtp.android.example.databinding.ListItemMessageBinding | ||
|
||
class MessageAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | ||
|
||
private val listItems = mutableListOf<ConversationDetailViewModel.MessageListItem>() | ||
|
||
fun setData(newItems: List<ConversationDetailViewModel.MessageListItem>) { | ||
listItems.clear() | ||
listItems.addAll(newItems) | ||
notifyItemRangeChanged(0, newItems.size) | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | ||
val inflater = LayoutInflater.from(parent.context) | ||
return when (viewType) { | ||
ConversationDetailViewModel.MessageListItem.ITEM_TYPE_MESSAGE -> { | ||
val binding = ListItemMessageBinding.inflate(inflater, parent, false) | ||
MessageViewHolder(binding) | ||
} | ||
else -> throw IllegalArgumentException("Unsupported view type $viewType") | ||
} | ||
} | ||
|
||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | ||
val item = listItems[position] | ||
when (holder) { | ||
is MessageViewHolder -> { | ||
holder.bind(item as ConversationDetailViewModel.MessageListItem.Message) | ||
} | ||
else -> throw IllegalArgumentException("Unsupported view holder") | ||
} | ||
} | ||
|
||
override fun getItemViewType(position: Int) = listItems[position].itemType | ||
|
||
override fun getItemCount() = listItems.count() | ||
|
||
override fun getItemId(position: Int) = listItems[position].id.hashCode().toLong() | ||
} |
42 changes: 42 additions & 0 deletions
42
example/src/main/java/org/xmtp/android/example/message/MessageViewHolder.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,42 @@ | ||
package org.xmtp.android.example.message | ||
|
||
import android.graphics.Color | ||
import androidx.constraintlayout.widget.ConstraintLayout | ||
import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams.PARENT_ID | ||
import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams.UNSET | ||
import androidx.recyclerview.widget.RecyclerView | ||
import org.xmtp.android.example.ClientManager | ||
import org.xmtp.android.example.R | ||
import org.xmtp.android.example.conversation.ConversationDetailViewModel | ||
import org.xmtp.android.example.databinding.ListItemMessageBinding | ||
import org.xmtp.android.example.extension.margins | ||
|
||
class MessageViewHolder( | ||
private val binding: ListItemMessageBinding | ||
) : RecyclerView.ViewHolder(binding.root) { | ||
|
||
private val marginLarge = binding.root.resources.getDimensionPixelSize(R.dimen.message_margin) | ||
private val marginSmall = binding.root.resources.getDimensionPixelSize(R.dimen.padding) | ||
private val backgroundMe = Color.LTGRAY | ||
private val backgroundPeer = | ||
binding.root.resources.getColor(R.color.teal_700, binding.root.context.theme) | ||
|
||
fun bind(item: ConversationDetailViewModel.MessageListItem.Message) { | ||
val isFromMe = ClientManager.client.address == item.message.senderAddress | ||
val params = binding.messageContainer.layoutParams as ConstraintLayout.LayoutParams | ||
if (isFromMe) { | ||
params.rightToRight = PARENT_ID | ||
params.leftToLeft = UNSET | ||
binding.messageRow.margins(left = marginLarge, right = marginSmall) | ||
binding.messageContainer.setCardBackgroundColor(backgroundMe) | ||
binding.messageBody.setTextColor(Color.BLACK) | ||
} else { | ||
params.leftToLeft = PARENT_ID | ||
params.rightToRight = UNSET | ||
binding.messageRow.margins(right = marginLarge, left = marginSmall) | ||
binding.messageContainer.setCardBackgroundColor(backgroundPeer) | ||
binding.messageBody.setTextColor(Color.WHITE) | ||
} | ||
binding.messageBody.text = item.message.body | ||
} | ||
} |
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,5 @@ | ||
<vector android:autoMirrored="true" android:height="24dp" | ||
android:tint="#000000" android:viewportHeight="24" | ||
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z"/> | ||
</vector> |
Oops, something went wrong.