From 2d01fd9b6f21b8299b2d08d21b9fcd27c0ee67ac Mon Sep 17 00:00:00 2001 From: bjoern Date: Fri, 20 Oct 2023 15:17:24 +0200 Subject: [PATCH] sending reactions (#2680) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add basic reaction view * reuse pill background * make pill border a little more visible; this affects sended reactions as well as received ones * use 🤣 instead of 😂 to differ more from 😢 also, 😂 is a bit outdated for younger generations (so is probably 🤣 but not that much) (alternative would be 💀 for laughing, but that is probably not compatible between generations) * use 🙁 instead of 😢 for 'sad' to distict from lol lol gets revered to 😂 as 🤣 seems to be even a more non-go (thanks for expertise, young folks!) * wording: we add one reaction, not many * show/hide add-reaction-view together with action mode * pass message&view to showAddReactionView() * add sendReactions() api * create AddReactionView class * send reactions on click * hide AddReactiobView on sending emoji * detect self-reaction * feat: remove selections * position AddReactionView atop of parent view * move AddReactionView up a bit * padding top/bottom * center for now, this also postpones rtl/ltr * force initial layout * move slightly more off the bubble, keep away a bit from screen margin * move AddReactionsView left/right depending on incoming/outgoing mails * do not allow reactions in read-only chats * do not allow to react to videochat-invitations and info; the latter may makes sense at some point, however, to lower burder on other implementations, we do not send them for now * hide reaction-picker when leaving action mode with 'back' * hide reaction-picker when leaving action mode with 'back' * scroll reaction-panel together with recyclerview; this does not happen if new messages come in, however, this is a minor (and a stable panel position can be regarded even as a feature) * add sending reactions as an experimental feature as long not at least all system display reactions * clarify that reactions are currently not seen by all members * prefer View.INVISIBLE over offscreen hack --- res/layout/conversation_fragment.xml | 45 ++++++ res/values/colors.xml | 2 +- res/values/text_styles.xml | 9 +- res/xml/preferences_advanced.xml | 5 + src/com/b44t/messenger/rpc/Rpc.java | 3 + .../securesms/ConversationAdapter.java | 4 +- .../securesms/ConversationFragment.java | 15 +- .../AdvancedPreferenceFragment.java | 16 ++ .../securesms/reactions/AddReactionView.java | 143 ++++++++++++++++++ 9 files changed, 237 insertions(+), 5 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/reactions/AddReactionView.java diff --git a/res/layout/conversation_fragment.xml b/res/layout/conversation_fragment.xml index cbc6d173d..0c3d7c828 100644 --- a/res/layout/conversation_fragment.xml +++ b/res/layout/conversation_fragment.xml @@ -70,4 +70,49 @@ android:contentDescription="@string/menu_scroll_to_bottom" android:src="@drawable/ic_search_down"/> + + + + + + + + + diff --git a/res/values/colors.xml b/res/values/colors.xml index 9c2fb900a..c12434f9a 100644 --- a/res/values/colors.xml +++ b/res/values/colors.xml @@ -77,7 +77,7 @@ #ff999999 #ffffff - #80f1f1f1 + #88dddddd #545863 @color/gray5 @color/reaction_pill_border diff --git a/res/values/text_styles.xml b/res/values/text_styles.xml index 78f104885..22830b8ea 100644 --- a/res/values/text_styles.xml +++ b/res/values/text_styles.xml @@ -30,4 +30,11 @@ ?conversation_item_outgoing_text_secondary_color 0 - \ No newline at end of file + + + diff --git a/res/xml/preferences_advanced.xml b/res/xml/preferences_advanced.xml index 50067caa0..0e65641a7 100644 --- a/res/xml/preferences_advanced.xml +++ b/res/xml/preferences_advanced.xml @@ -73,6 +73,11 @@ android:key="pref_new_broadcast_list" android:title="@string/broadcast_lists"/> + + interface ItemClickListener extends BindableConversationItem.EventListener { void onItemClick(DcMsg item); - void onItemLongClick(DcMsg item); + void onItemLongClick(DcMsg item, View view); } public ConversationAdapter(@NonNull Context context, @@ -263,7 +263,7 @@ public class ConversationAdapter }); itemView.setOnLongClickListener(view -> { if (clickListener != null) { - clickListener.onItemLongClick(itemView.getMessageRecord()); + clickListener.onItemLongClick(itemView.getMessageRecord(), view); } return true; }); diff --git a/src/org/thoughtcrime/securesms/ConversationFragment.java b/src/org/thoughtcrime/securesms/ConversationFragment.java index 96db5476d..9eca02f0b 100644 --- a/src/org/thoughtcrime/securesms/ConversationFragment.java +++ b/src/org/thoughtcrime/securesms/ConversationFragment.java @@ -63,6 +63,7 @@ import org.thoughtcrime.securesms.connect.DcEventCenter; import org.thoughtcrime.securesms.connect.DcHelper; import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.mms.GlideApp; +import org.thoughtcrime.securesms.reactions.AddReactionView; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.reactions.ReactionsDetailsFragment; import org.thoughtcrime.securesms.util.AccessibilityUtil; @@ -105,6 +106,7 @@ public class ConversationFragment extends MessageSelectorFragment private StickyHeaderDecoration dateDecoration; private View scrollToBottomButton; private View floatingLocationButton; + private AddReactionView addReactionView; private TextView noMessageTextView; private Timer reloadTimer; @@ -143,6 +145,7 @@ public class ConversationFragment extends MessageSelectorFragment list = ViewUtil.findById(view, android.R.id.list); scrollToBottomButton = ViewUtil.findById(view, R.id.scroll_to_bottom_button); floatingLocationButton = ViewUtil.findById(view, R.id.floating_location_button); + addReactionView = ViewUtil.findById(view, R.id.add_reaction_view); noMessageTextView = ViewUtil.findById(view, R.id.no_messages_text_view); scrollToBottomButton.setOnClickListener(v -> scrollToBottom()); @@ -631,6 +634,8 @@ public class ConversationFragment extends MessageSelectorFragment // lastPositionId = positionId; markseenDebouncer.publish(() -> manageMessageSeenState()); + + ConversationFragment.this.addReactionView.move(dy); } @Override @@ -774,6 +779,7 @@ public class ConversationFragment extends MessageSelectorFragment if (getListAdapter().getSelectedItems().size() == 0) { actionMode.finish(); } else { + addReactionView.hide(); Menu menu = actionMode.getMenu(); setCorrectMenuVisibility(menu); ConversationAdaptiveActionsToolbar.adjustMenuActions(menu, 10, requireActivity().getWindow().getDecorView().getMeasuredWidth()); @@ -805,12 +811,17 @@ public class ConversationFragment extends MessageSelectorFragment } @Override - public void onItemLongClick(DcMsg messageRecord) { + public void onItemLongClick(DcMsg messageRecord, View view) { if (actionMode == null) { ((ConversationAdapter) list.getAdapter()).toggleSelection(messageRecord); list.getAdapter().notifyDataSetChanged(); actionMode = ((AppCompatActivity)getActivity()).startSupportActionMode(actionModeCallback); + addReactionView.show(messageRecord, view, () -> { + if (actionMode != null) { + actionMode.finish(); + } + }); } } @@ -910,10 +921,12 @@ public class ConversationFragment extends MessageSelectorFragment } actionMode = null; + addReactionView.hide(); } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { + addReactionView.hide(); switch(item.getItemId()) { case R.id.menu_context_copy: handleCopyMessage(getListAdapter().getSelectedItems()); diff --git a/src/org/thoughtcrime/securesms/preferences/AdvancedPreferenceFragment.java b/src/org/thoughtcrime/securesms/preferences/AdvancedPreferenceFragment.java index 048f462f9..7975dc098 100644 --- a/src/org/thoughtcrime/securesms/preferences/AdvancedPreferenceFragment.java +++ b/src/org/thoughtcrime/securesms/preferences/AdvancedPreferenceFragment.java @@ -146,6 +146,22 @@ public class AdvancedPreferenceFragment extends ListSummaryPreferenceFragment return true; }); + Preference sendReactions = this.findPreference("pref_send_reactions"); + sendReactions.setOnPreferenceChangeListener((preference, newValue) -> { + if ((Boolean)newValue) { + new AlertDialog.Builder(getActivity()) + .setTitle("Thanks for trying out \"Reactions\"!") + .setMessage("• By long tapping a message, you can now add a reaction\n\n" + + "• Do not rely on reactions being seen by all recipients currently: On some platforms they're still in development, also rolling out updates take time\n\n" + + "• If you want to quit the experimental feature, disable it again at \"Settings / Advanced\";" + + " received or already sent reactions are displayed independently of this switch") + .setCancelable(false) + .setPositiveButton(R.string.ok, null) + .show(); + } + return true; + }); + Preference locationStreamingEnabled = this.findPreference("pref_location_streaming_enabled"); locationStreamingEnabled.setOnPreferenceChangeListener((preference, newValue) -> { if ((Boolean)newValue) { diff --git a/src/org/thoughtcrime/securesms/reactions/AddReactionView.java b/src/org/thoughtcrime/securesms/reactions/AddReactionView.java new file mode 100644 index 000000000..123acf076 --- /dev/null +++ b/src/org/thoughtcrime/securesms/reactions/AddReactionView.java @@ -0,0 +1,143 @@ +package org.thoughtcrime.securesms.reactions; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; +import android.widget.LinearLayout; + +import androidx.core.content.ContextCompat; + +import com.b44t.messenger.DcContact; +import com.b44t.messenger.DcContext; +import com.b44t.messenger.DcMsg; +import com.b44t.messenger.rpc.Reactions; +import com.b44t.messenger.rpc.Rpc; + +import org.thoughtcrime.securesms.R; +import org.thoughtcrime.securesms.components.emoji.EmojiTextView; +import org.thoughtcrime.securesms.connect.DcHelper; +import org.thoughtcrime.securesms.util.Prefs; +import org.thoughtcrime.securesms.util.ViewUtil; + +import java.util.Map; + +public class AddReactionView extends LinearLayout { + private EmojiTextView [] defaultReactionViews; + private Context context; + private DcContext dcContext; + private Rpc rpc; + private DcMsg msgToReactTo; + private AddReactionListener listener; + + public AddReactionView(Context context) { + super(context); + } + + public AddReactionView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + private void init() { + if (context == null) { + context = getContext(); + dcContext = DcHelper.getContext(context); + rpc = DcHelper.getRpc(getContext()); + defaultReactionViews = new EmojiTextView[]{ + findViewById(R.id.reaction_0), + findViewById(R.id.reaction_1), + findViewById(R.id.reaction_2), + findViewById(R.id.reaction_3), + findViewById(R.id.reaction_4), + }; + for (int i = 0; i < defaultReactionViews.length; i++) { + final int ii = i; + defaultReactionViews[i].setOnClickListener(v -> reactionClicked(ii)); + } + } + } + + public void show(DcMsg msgToReactTo, View parentView, AddReactionListener listener) { + init(); // init delayed as needed + + if (!Prefs.getBooleanPreference(context, "pref_send_reactions", false)) { + return; + } + + if ( msgToReactTo.isInfo() + || msgToReactTo.getType() == DcMsg.DC_MSG_VIDEOCHAT_INVITATION + || !dcContext.getChat(msgToReactTo.getChatId()).canSend()) { + return; + } + + this.msgToReactTo = msgToReactTo; + this.listener = listener; + + final String existingReaction = getSelfReaction(); + for (EmojiTextView defaultReactionView : defaultReactionViews) { + if (defaultReactionView.getText().toString().equals(existingReaction)) { + defaultReactionView.setBackground(ContextCompat.getDrawable(context, R.drawable.reaction_pill_background_selected)); + } else { + defaultReactionView.setBackground(null); + } + } + + final int offset = (int)(this.getHeight() * 0.666); + int x = (int)parentView.getX(); + if (msgToReactTo.isOutgoing()) { + x += parentView.getWidth() - offset - this.getWidth(); + } else { + x += offset; + } + ViewUtil.setLeftMargin(this, x); + + int y = Math.max((int)parentView.getY() - offset, offset/2); + ViewUtil.setTopMargin(this, y); + + setVisibility(View.VISIBLE); + } + + public void hide() { + setVisibility(View.GONE); + } + + public void move(int dy) { + if (msgToReactTo != null && getVisibility() == View.VISIBLE) { + ViewUtil.setTopMargin(this, (int) this.getY() - dy); + } + } + + private String getSelfReaction() { + String result = null; + try { + final Reactions reactions = rpc.getMsgReactions(dcContext.getAccountId(), msgToReactTo.getId()); + final Map reactionsByContact = reactions.getReactionsByContact(); + final String [] selfReactions = reactionsByContact.get(DcContact.DC_CONTACT_ID_SELF); + if (selfReactions != null && selfReactions.length > 0) { + result = selfReactions[0]; + } + } catch(Exception e) { + e.printStackTrace(); + } + return result; + } + + private void reactionClicked(int i) { + try { + final String reaction = defaultReactionViews[i].getText().toString(); + if (reaction.equals(getSelfReaction())) { + rpc.sendReaction(dcContext.getAccountId(), msgToReactTo.getId(), ""); + } else { + rpc.sendReaction(dcContext.getAccountId(), msgToReactTo.getId(), reaction); + } + if (listener != null) { + listener.onShallHide(); + } + } catch(Exception e) { + e.printStackTrace(); + } + } + + public interface AddReactionListener { + void onShallHide(); + } +}