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