Commit 89842afc authored by Leonardo Aramaki's avatar Leonardo Aramaki

Toggle reaction when touching on an added reaction

parent deeac58b
......@@ -48,15 +48,19 @@ abstract class BaseViewHolder<T : BaseViewModel<*>>(
if (it.nextDownStreamMessage == null) {
adapter.listener = object : EmojiReactionListener {
override fun onEmojiReactionAdded(messageId: String, emoji: Emoji) {
reactionListener?.onEmojiReactionAdded(messageId, emoji)
override fun onReactionTouched(messageId: String, emojiShortname: String) {
reactionListener?.onReactionTouched(messageId, emojiShortname)
}
override fun onReactionAdded(messageId: String, emoji: Emoji) {
reactionListener?.onReactionAdded(messageId, emoji)
}
}
val context = itemView.context
val manager = FlexboxLayoutManager(context, FlexDirection.ROW)
recyclerView.layoutManager = manager
recyclerView.adapter = adapter
adapter.addReactions(it.reactions.filterNot { it.shortname.startsWith(":") })
adapter.addReactions(it.reactions.filterNot { it.unicode.startsWith(":") })
}
}
}
......
......@@ -36,7 +36,7 @@ class MessageReactionsAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>()
}
else -> {
view = inflater.inflate(R.layout.item_reaction, parent, false)
SingleReactionViewHolder(view)
SingleReactionViewHolder(view, listener)
}
}
}
......@@ -71,8 +71,13 @@ class MessageReactionsAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>()
notifyItemRangeRemoved(0, oldSize)
}
class SingleReactionViewHolder(view: View) : RecyclerView.ViewHolder(view) {
class SingleReactionViewHolder(view: View,
private val listener: EmojiReactionListener?)
: RecyclerView.ViewHolder(view), View.OnClickListener {
@Inject lateinit var localRepository: LocalRepository
@Volatile lateinit var reaction: ReactionViewModel
@Volatile
var clickHandled = false
init {
DaggerLocalComponent.builder()
......@@ -82,10 +87,12 @@ class MessageReactionsAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>()
}
fun bind(reaction: ReactionViewModel) {
clickHandled = false
this.reaction = reaction
with(itemView) {
val emojiTextView = findViewById<TextView>(R.id.text_emoji)
val countTextView = findViewById<TextView>(R.id.text_count)
emojiTextView.text = reaction.shortname
emojiTextView.text = reaction.unicode
countTextView.text = reaction.count.toString()
val myself = localRepository.get(LocalRepository.USERNAME_KEY)
if (reaction.usernames.contains(myself)) {
......@@ -93,18 +100,31 @@ class MessageReactionsAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>()
val resources = context.resources
countTextView.setTextColor(resources.getColor(R.color.colorAccent))
}
emojiTextView.setOnClickListener(this@SingleReactionViewHolder)
countTextView.setOnClickListener(this@SingleReactionViewHolder)
}
}
override fun onClick(v: View?) {
synchronized(this) {
if (!clickHandled) {
clickHandled = true
listener?.onReactionTouched(reaction.messageId, reaction.shortname)
}
}
}
}
class AddReactionViewHolder(view: View, val listener: EmojiReactionListener?) : RecyclerView.ViewHolder(view) {
class AddReactionViewHolder(view: View,
private val listener: EmojiReactionListener?) : RecyclerView.ViewHolder(view) {
fun bind(messageId: String) {
itemView as ImageView
itemView.setOnClickListener {
val emojiPickerPopup = EmojiPickerPopup(itemView.context)
emojiPickerPopup.listener = object : EmojiKeyboardListenerAdapter() {
override fun onEmojiAdded(emoji: Emoji) {
listener?.onEmojiReactionAdded(messageId, emoji)
listener?.onReactionAdded(messageId, emoji)
}
}
emojiPickerPopup.show()
......
......@@ -279,7 +279,11 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
}
}
override fun onEmojiReactionAdded(messageId: String, emoji: Emoji) {
override fun onReactionTouched(messageId: String, emojiShortname: String) {
presenter.react(messageId, emojiShortname)
}
override fun onReactionAdded(messageId: String, emoji: Emoji) {
presenter.react(messageId, emoji.shortname)
}
......
......@@ -2,7 +2,8 @@ package chat.rocket.android.chatroom.viewmodel
data class ReactionViewModel(
val messageId: String,
val shortname: CharSequence,
val shortname: String,
val unicode: CharSequence,
val count: Int,
val usernames: List<String> = emptyList()
)
\ No newline at end of file
......@@ -160,7 +160,8 @@ class ViewModelMapper @Inject constructor(private val context: Context,
val count = usernames.size
list.add(
ReactionViewModel(messageId = message.id,
shortname = EmojiParser.parse(shortname),
shortname = shortname,
unicode = EmojiParser.parse(shortname),
count = count,
usernames = usernames)
)
......
......@@ -7,5 +7,13 @@ interface EmojiReactionListener {
* @param messageId The id of the message being reacted.
* @param emoji The emoji used to react.
*/
fun onEmojiReactionAdded(messageId: String, emoji: Emoji)
fun onReactionAdded(messageId: String, emoji: Emoji)
/**
* Callback when an added reaction is touched.
*
* @param messageId The id of the message with the reaction.
* @param emojiShortname The shortname of the emoji (:grin:, :smiley:, etc).
*/
fun onReactionTouched(messageId: String, emojiShortname: String)
}
\ No newline at end of file
......@@ -7,6 +7,7 @@
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:descendantFocusability="beforeDescendants"
android:background="@drawable/rounded_background"
android:orientation="horizontal">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment