From d1c4283f42f8f7b5a1fb12649335974360ea0c77 Mon Sep 17 00:00:00 2001 From: ThomasSession Date: Wed, 28 Aug 2024 09:17:15 +1000 Subject: [PATCH] Fix/emoji reactions crash (#1646) * Fixing crash on sdk =28 for emoji reactions * Proper styling of reactions Making sure we display the public key when there is no name * Making sure we display the emoji that was long pressed --- .../conversation/v2/ConversationActivityV2.kt | 4 +-- .../v2/messages/EmojiReactionsView.kt | 29 ++++++++------- .../v2/messages/VisibleMessageViewDelegate.kt | 2 +- .../reactions/ReactionRecipientsAdapter.java | 8 +++-- .../reactions/ReactionViewPagerAdapter.java | 2 +- .../reactions/ReactionsDialogFragment.java | 20 +++++++++-- ...cal_divider.xml => horizontal_divider.xml} | 2 +- .../reaction_pill_background_bordered.xml | 6 ++++ ...m_sheet_dialog_fragment_recipient_item.xml | 5 +-- ..._sheet_dialog_fragment_recycler_footer.xml | 11 +++--- ..._sheet_dialog_fragment_recycler_header.xml | 15 ++++---- app/src/main/res/layout/reactions_pill.xml | 18 +++++----- .../main/res/layout/reactions_pill_large.xml | 35 +++++++++++++++++++ 13 files changed, 112 insertions(+), 45 deletions(-) rename app/src/main/res/drawable/{vertical_divider.xml => horizontal_divider.xml} (72%) create mode 100644 app/src/main/res/drawable/reaction_pill_background_bordered.xml create mode 100644 app/src/main/res/layout/reactions_pill_large.xml diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 69844e33b6..306b1699ae 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -1529,13 +1529,13 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe } } - override fun onReactionLongClicked(messageId: MessageId) { + override fun onReactionLongClicked(messageId: MessageId, emoji: String?) { if (viewModel.recipient?.isGroupRecipient == true) { val isUserModerator = viewModel.openGroup?.let { openGroup -> val userPublicKey = textSecurePreferences.getLocalNumber() ?: return@let false OpenGroupManager.isUserModerator(this, openGroup.id, userPublicKey, viewModel.blindedPublicKey) } ?: false - val fragment = ReactionsDialogFragment.create(messageId, isUserModerator) + val fragment = ReactionsDialogFragment.create(messageId, isUserModerator, emoji) fragment.show(supportFragmentManager, null) } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/EmojiReactionsView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/EmojiReactionsView.kt index 49e4b1044f..9f4ce88879 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/EmojiReactionsView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/EmojiReactionsView.kt @@ -10,6 +10,7 @@ import android.widget.LinearLayout import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat +import androidx.core.view.setPadding import com.google.android.flexbox.JustifyContent import network.loki.messenger.R import network.loki.messenger.databinding.ViewEmojiReactionsBinding @@ -43,6 +44,8 @@ class EmojiReactionsView : ConstraintLayout, OnTouchListener { private var onDownTimestamp: Long = 0 private var extended = false + private val overflowItemSize = ViewUtil.dpToPx(24) + constructor(context: Context) : super(context) { init(null) } constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { init(attrs) } constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { init(attrs) } @@ -81,7 +84,9 @@ class EmojiReactionsView : ConstraintLayout, OnTouchListener { if (v.tag == null) return false val reaction = v.tag as Reaction val action = event.action - if (action == MotionEvent.ACTION_DOWN) onDown(MessageId(reaction.messageId, reaction.isMms)) else if (action == MotionEvent.ACTION_CANCEL) removeLongPressCallback() else if (action == MotionEvent.ACTION_UP) onUp(reaction) + if (action == MotionEvent.ACTION_DOWN) onDown(MessageId(reaction.messageId, reaction.isMms), reaction.emoji) + else if (action == MotionEvent.ACTION_CANCEL) removeLongPressCallback() + else if (action == MotionEvent.ACTION_UP) onUp(reaction) return true } @@ -91,18 +96,15 @@ class EmojiReactionsView : ConstraintLayout, OnTouchListener { binding.layoutEmojiContainer.removeAllViews() val overflowContainer = LinearLayout(context) overflowContainer.orientation = LinearLayout.HORIZONTAL - val innerPadding = ViewUtil.dpToPx(4) - overflowContainer.setPaddingRelative(innerPadding, innerPadding, innerPadding, innerPadding) val pixelSize = ViewUtil.dpToPx(1) - for (reaction in reactions) { + reactions.forEachIndexed { index, reaction -> if (binding.layoutEmojiContainer.childCount + 1 >= DEFAULT_THRESHOLD && threshold != Int.MAX_VALUE && reactions.size > threshold) { if (overflowContainer.parent == null) { binding.layoutEmojiContainer.addView(overflowContainer) val overflowParams = overflowContainer.layoutParams as MarginLayoutParams - overflowParams.height = ViewUtil.dpToPx(26) + overflowParams.height = MarginLayoutParams.WRAP_CONTENT overflowParams.setMargins(pixelSize, pixelSize, pixelSize, pixelSize) overflowContainer.layoutParams = overflowParams - overflowContainer.background = ContextCompat.getDrawable(context, R.drawable.reaction_pill_background) } val pill = buildPill(context, this, reaction, true) pill.setOnClickListener { v: View? -> @@ -111,6 +113,7 @@ class EmojiReactionsView : ConstraintLayout, OnTouchListener { } pill.findViewById(R.id.reactions_pill_count).visibility = GONE pill.findViewById(R.id.reactions_pill_spacer).visibility = GONE + pill.z = reaction.count - index.toFloat() // make sure the overflow is stacked properly overflowContainer.addView(pill) } else { val pill = buildPill(context, this, reaction, false) @@ -179,9 +182,10 @@ class EmojiReactionsView : ConstraintLayout, OnTouchListener { val countView = root.findViewById(R.id.reactions_pill_count) val spacer = root.findViewById(R.id.reactions_pill_spacer) if (isCompact) { - root.setPaddingRelative(1, 1, 1, 1) + root.setPadding(0) val layoutParams = root.layoutParams - layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT + layoutParams.height = overflowItemSize + layoutParams.width = overflowItemSize root.layoutParams = layoutParams } if (reaction.emoji != null) { @@ -201,9 +205,8 @@ class EmojiReactionsView : ConstraintLayout, OnTouchListener { root.background = ContextCompat.getDrawable(context, R.drawable.reaction_pill_background_selected) countView.setTextColor(ThemeUtil.getThemedColor(context, R.attr.reactionsPillSelectedTextColor)) } else { - if (!isCompact) { - root.background = ContextCompat.getDrawable(context, R.drawable.reaction_pill_background) - } + root.background = if(isCompact) ContextCompat.getDrawable(context, R.drawable.reaction_pill_background_bordered) + else ContextCompat.getDrawable(context, R.drawable.reaction_pill_background) } return root } @@ -215,12 +218,12 @@ class EmojiReactionsView : ConstraintLayout, OnTouchListener { } } - private fun onDown(messageId: MessageId) { + private fun onDown(messageId: MessageId, emoji: String?) { removeLongPressCallback() val newLongPressCallback = Runnable { performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) if (delegate != null) { - delegate!!.onReactionLongClicked(messageId) + delegate!!.onReactionLongClicked(messageId, emoji) } } longPressCallback = newLongPressCallback diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageViewDelegate.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageViewDelegate.kt index 6788dd3f38..69797b8848 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageViewDelegate.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageViewDelegate.kt @@ -10,6 +10,6 @@ interface VisibleMessageViewDelegate { fun onReactionClicked(emoji: String, messageId: MessageId, userWasSender: Boolean) - fun onReactionLongClicked(messageId: MessageId) + fun onReactionLongClicked(messageId: MessageId, emoji: String?) } \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/reactions/ReactionRecipientsAdapter.java b/app/src/main/java/org/thoughtcrime/securesms/reactions/ReactionRecipientsAdapter.java index 79717eabb1..05817f4923 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/reactions/ReactionRecipientsAdapter.java +++ b/app/src/main/java/org/thoughtcrime/securesms/reactions/ReactionRecipientsAdapter.java @@ -1,5 +1,7 @@ package org.thoughtcrime.securesms.reactions; +import static org.session.libsession.utilities.IdUtilKt.truncateIdForDisplay; + import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -129,7 +131,7 @@ final class ReactionRecipientsAdapter extends RecyclerView.Adapter insets.consumeSystemWindowInsets()); TabLayoutMediator mediator = new TabLayoutMediator(emojiTabs, recipientPagerView, (tab, position) -> { - tab.setCustomView(R.layout.reactions_pill); + tab.setCustomView(R.layout.reactions_pill_large); View customView = Objects.requireNonNull(tab.getCustomView()); EmojiImageView emoji = customView.findViewById(R.id.reactions_pill_emoji); @@ -169,6 +171,18 @@ public final class ReactionsDialogFragment extends BottomSheetDialogFragment imp } recipientsAdapter.submitList(emojiCounts); + + // select the tab based on which emoji the user long pressed on + TabLayout emojiTabs = requireDialog().findViewById(R.id.emoji_tabs); + String emoji = requireArguments().getString(ARGS_EMOJI); + int tabIndex = 0; + for (int i = 0; i < emojiCounts.size(); i++) { + if(emojiCounts.get(i).getBaseEmoji().equals(emoji)){ + tabIndex = i; + break; + } + } + emojiTabs.selectTab(emojiTabs.getTabAt(tabIndex)); })); } diff --git a/app/src/main/res/drawable/vertical_divider.xml b/app/src/main/res/drawable/horizontal_divider.xml similarity index 72% rename from app/src/main/res/drawable/vertical_divider.xml rename to app/src/main/res/drawable/horizontal_divider.xml index 88e5898e99..2658006cb5 100644 --- a/app/src/main/res/drawable/vertical_divider.xml +++ b/app/src/main/res/drawable/horizontal_divider.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/app/src/main/res/drawable/reaction_pill_background_bordered.xml b/app/src/main/res/drawable/reaction_pill_background_bordered.xml new file mode 100644 index 0000000000..0adaee8025 --- /dev/null +++ b/app/src/main/res/drawable/reaction_pill_background_bordered.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/app/src/main/res/layout/reactions_bottom_sheet_dialog_fragment_recipient_item.xml b/app/src/main/res/layout/reactions_bottom_sheet_dialog_fragment_recipient_item.xml index ef94652a91..e3d037809d 100644 --- a/app/src/main/res/layout/reactions_bottom_sheet_dialog_fragment_recipient_item.xml +++ b/app/src/main/res/layout/reactions_bottom_sheet_dialog_fragment_recipient_item.xml @@ -3,7 +3,8 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" - android:layout_height="52dp"> + android:layout_height="50dp" + android:background="?backgroundSecondary"> \ No newline at end of file + android:layout_weight="1" + style="@style/Signal.Text.Preview" + android:textColor="?android:textColorTertiary" + tools:text="And 1244 other have reacted to this message" /> \ No newline at end of file diff --git a/app/src/main/res/layout/reactions_bottom_sheet_dialog_fragment_recycler_header.xml b/app/src/main/res/layout/reactions_bottom_sheet_dialog_fragment_recycler_header.xml index 8776d3cd47..2c20312645 100644 --- a/app/src/main/res/layout/reactions_bottom_sheet_dialog_fragment_recycler_header.xml +++ b/app/src/main/res/layout/reactions_bottom_sheet_dialog_fragment_recycler_header.xml @@ -6,21 +6,21 @@ android:layout_height="wrap_content"> - + android:gravity="center_vertical"> @@ -28,13 +28,16 @@ android:id="@+id/header_view_emoji_count" android:layout_weight="1" android:layout_width="0dp" - android:layout_height="wrap_content"/> + android:layout_height="wrap_content" + android:layout_gravity="center_vertical" + android:textColor="?android:textColorTertiary"/> + android:layout_height="wrap_content" + android:layout_gravity="center_vertical"/> \ No newline at end of file diff --git a/app/src/main/res/layout/reactions_pill.xml b/app/src/main/res/layout/reactions_pill.xml index a88673cba8..69546d092b 100644 --- a/app/src/main/res/layout/reactions_pill.xml +++ b/app/src/main/res/layout/reactions_pill.xml @@ -4,31 +4,31 @@ xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="wrap_content" - android:layout_height="26dp" - android:paddingStart="7dp" - android:paddingEnd="7dp" + android:layout_height="22dp" + android:paddingStart="8dp" + android:paddingEnd="8dp" android:gravity="center"> + android:layout_width="13dp" + android:layout_height="13dp" + android:layout_gravity="center_vertical"/> diff --git a/app/src/main/res/layout/reactions_pill_large.xml b/app/src/main/res/layout/reactions_pill_large.xml new file mode 100644 index 0000000000..44fbd5aa02 --- /dev/null +++ b/app/src/main/res/layout/reactions_pill_large.xml @@ -0,0 +1,35 @@ + + + + + + + + + + \ No newline at end of file