basic display of reactions

This commit is contained in:
adbenitez
2023-05-11 10:50:26 -04:00
committed by B. Petersen
parent de1f1d4fe2
commit a38ad255da
16 changed files with 288 additions and 40 deletions
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp" />
<solid android:color="@color/reaction_pill_background" />
<stroke android:color="@color/reaction_pill_border" android:width="1dp" />
</shape>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp" />
<solid android:color="@color/reaction_pill_background_selected" />
<stroke android:color="@color/reaction_pill_border_selected" android:width="1dp" />
</shape>
+10
View File
@@ -220,5 +220,15 @@
</LinearLayout>
<org.thoughtcrime.securesms.reactions.ReactionsConversationView
android:id="@+id/reactions_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/body_bubble"
android:layout_alignStart="@id/body_bubble"
android:layout_marginTop="-4dp"
android:orientation="horizontal"
app:message_type="incoming" />
</RelativeLayout>
</org.thoughtcrime.securesms.ConversationItem>
+10
View File
@@ -197,6 +197,16 @@
</LinearLayout>
<org.thoughtcrime.securesms.reactions.ReactionsConversationView
android:id="@+id/reactions_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/body_bubble"
android:layout_alignEnd="@id/body_bubble"
android:layout_marginTop="-4dp"
android:orientation="horizontal"
app:message_type="outgoing" />
<!-- the following view is only left because it is used as a reference for positioning above.
removing would require some re-layouting, it's just not done yet.-->
<View
+37
View File
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
tools:viewBindingIgnore="true"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="26dp"
android:layout_marginStart="1dp"
android:layout_marginEnd="1dp"
android:paddingStart="7dp"
android:paddingEnd="7dp"
android:gravity="center">
<org.thoughtcrime.securesms.components.emoji.EmojiImageView
android:id="@+id/reactions_pill_emoji"
android:layout_width="17dp"
android:layout_height="17dp" />
<Space
android:id="@+id/reactions_pill_spacer"
android:layout_width="4dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/reactions_pill_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="12dp"
android:fontFamily="sans-serif-medium"
android:textColor="@color/reaction_pill_text_color"
tools:text="23"
tools:ignore="SpUsage" />
</LinearLayout>
+9
View File
@@ -0,0 +1,9 @@
<?xml version='1.0' encoding='UTF-8'?>
<resources>
<color name="reaction_pill_background">@color/black</color>
<color name="reaction_pill_border">#80414347</color>
<color name="reaction_pill_text_color">@color/core_dark_05</color>
<color name="reaction_pill_background_selected">#ff282828</color>
<color name="reaction_pill_border_selected">@color/reaction_pill_border</color>
<color name="reaction_pill_text_color_selected">@color/reaction_pill_text_color</color>
</resources>
+11 -5
View File
@@ -240,16 +240,22 @@
<attr name="timeFrame" format="integer"/>
</declare-styleable>
<attr name="message_type" format="enum">
<enum name="preview" value="0" />
<enum name="outgoing" value="1" />
<enum name="incoming" value="2" />
</attr>
<declare-styleable name="QuoteView">
<attr name="message_type" format="enum">
<enum name="preview" value="0" />
<enum name="outgoing" value="1" />
<enum name="incoming" value="2" />
</attr>
<attr name="message_type" />
<attr name="quote_colorPrimary" format="color" />
<attr name="quote_colorSecondary" format="color" />
</declare-styleable>
<declare-styleable name="ReactionsConversationView">
<attr name="message_type" />
</declare-styleable>
<declare-styleable name="ConversationAdaptiveActionsToolbar">
<attr name="aat_max_shown" format="integer" />
</declare-styleable>
+7
View File
@@ -75,4 +75,11 @@
<!-- unknown_sender fits in dark mode and lite mode (also other chat/contact colors fit in both modes);
unknown_sender is used for the sender-name/quote-bar if the sender is unknown -->
<color name="unknown_sender">#ff999999</color>
<color name="reaction_pill_background">#ffffff</color>
<color name="reaction_pill_border">#80f1f1f1</color>
<color name="reaction_pill_text_color">#545863</color>
<color name="reaction_pill_background_selected">@color/gray5</color>
<color name="reaction_pill_border_selected">@color/reaction_pill_border</color>
<color name="reaction_pill_text_color_selected">@color/reaction_pill_text_color</color>
</resources>
+2
View File
@@ -722,6 +722,8 @@
<string name="emoji_symbols">Symbols</string>
<string name="emoji_flags">Flags</string>
<!-- Reactions -->
<string name="ReactionsConversationView_plus">+%1$d</string>
<!-- automatically delete message -->
<string name="delete_old_messages">Delete Old Messages</string>
+21 -19
View File
@@ -1,37 +1,39 @@
package com.b44t.messenger.rpc;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import androidx.annotation.Nullable;
public class Reaction {
// The reaction text string.
private final String text;
// The reaction emoji string.
private final String emoji;
// The count of users that have reacted with this reaction.
private final int count;
// true if self-account reacted with this reaction, false otherwise.
private final boolean isFromSelf;
public Reaction(String text, int count) {
this.text = text;
public Reaction(String emoji, int count, boolean isFromSelf) {
this.emoji = emoji;
this.count = count;
this.isFromSelf = isFromSelf;
}
public String getText() {
return text;
public String getEmoji() {
return emoji;
}
public int getCount() {
return count;
}
public static class ReactionDeserializer implements JsonDeserializer<Reaction> {
@Override
public Reaction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonArray tuple = json.getAsJsonArray();
return new Reaction(tuple.get(0).getAsString(), tuple.get(1).getAsInt());
}
public boolean isFromSelf() {
return isFromSelf;
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof Reaction) {
Reaction reaction = (Reaction) obj;
return emoji.equals(reaction.getEmoji()) && count == reaction.getCount() && isFromSelf == reaction.isFromSelf();
}
return false;
}
}
+8 -5
View File
@@ -1,23 +1,26 @@
package com.b44t.messenger.rpc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Reactions {
// Map from a contact to it's reaction to message.
private HashMap<Integer, String[]> reactionsByContact;
private final HashMap<Integer, String[]> reactionsByContact;
// Unique reactions, sorted in descending order.
private Reaction[] reactions;
private final ArrayList<Reaction> reactions;
public Reactions(HashMap<Integer, String[]> reactionsByContact, Reaction[] reactions) {
public Reactions(HashMap<Integer, String[]> reactionsByContact, ArrayList<Reaction> reactions) {
this.reactionsByContact = reactionsByContact;
this.reactions = reactions;
}
public HashMap<Integer, String[]> getReactionsByContact() {
public Map<Integer, String[]> getReactionsByContact() {
return reactionsByContact;
}
public Reaction[] getReactions() {
public List<Reaction> getReactions() {
return reactions;
}
}
+4 -10
View File
@@ -1,7 +1,5 @@
package com.b44t.messenger.rpc;
import static com.b44t.messenger.rpc.Reaction.ReactionDeserializer;
import com.b44t.messenger.DcJsonrpcInstance;
import com.b44t.messenger.util.concurrent.SettableFuture;
import com.google.gson.Gson;
@@ -18,14 +16,10 @@ public class Rpc {
private final Map<Integer, SettableFuture<JsonElement>> requestFutures = new ConcurrentHashMap<>();
private final DcJsonrpcInstance dcJsonrpcInstance;
private int requestId = 0;
private final Gson gson;
private final Gson gson = new GsonBuilder().serializeNulls().create();
public Rpc(DcJsonrpcInstance dcJsonrpcInstance) {
this.dcJsonrpcInstance = dcJsonrpcInstance;
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Reaction.class, new ReactionDeserializer());
this.gson = gsonBuilder.serializeNulls().create();
}
private void processResponse() throws JsonSyntaxException {
@@ -46,7 +40,7 @@ public class Rpc {
} else if (response.result != null) {
future.set(response.result);
} else {
future.setException(new RpcException("Got JSON-RPC response witout result or error: " + jsonResponse));
future.setException(new RpcException("Got JSON-RPC response without result or error: " + jsonResponse));
}
}
@@ -111,7 +105,7 @@ public class Rpc {
private static class Request {
public String jsonrpc = "2.0";
private final String jsonrpc = "2.0";
public String method;
public Object[] params;
public int id;
@@ -124,7 +118,7 @@ public class Rpc {
}
private static class Response {
public int id = 0;
public int id;
public JsonElement result;
public JsonElement error;
@@ -13,6 +13,7 @@ import androidx.appcompat.app.AlertDialog;
import com.b44t.messenger.DcChat;
import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcMsg;
import com.b44t.messenger.rpc.Rpc;
import org.thoughtcrime.securesms.connect.DcHelper;
@@ -30,6 +31,7 @@ public abstract class BaseConversationItem extends LinearLayout
protected final Context context;
protected final DcContext dcContext;
protected final Rpc rpc;
protected @NonNull Set<DcMsg> batchSelected = new HashSet<>();
@@ -39,6 +41,7 @@ public abstract class BaseConversationItem extends LinearLayout
super(context, attrs);
this.context = context;
this.dcContext = DcHelper.getContext(context);
this.rpc = DcHelper.getRpc(context);
}
protected void bind(@NonNull DcMsg messageRecord,
@@ -92,7 +95,7 @@ public abstract class BaseConversationItem extends LinearLayout
}
protected class ClickListener implements View.OnClickListener {
private OnClickListener parent;
private final OnClickListener parent;
ClickListener(@Nullable OnClickListener parent) {
this.parent = parent;
@@ -39,6 +39,8 @@ import androidx.annotation.Nullable;
import com.b44t.messenger.DcChat;
import com.b44t.messenger.DcContact;
import com.b44t.messenger.DcMsg;
import com.b44t.messenger.rpc.Reactions;
import com.b44t.messenger.rpc.RpcException;
import org.thoughtcrime.securesms.audio.AudioSlidePlayer;
import org.thoughtcrime.securesms.components.AudioView;
@@ -59,6 +61,7 @@ import org.thoughtcrime.securesms.mms.Slide;
import org.thoughtcrime.securesms.mms.SlideClickListener;
import org.thoughtcrime.securesms.mms.SlideDeck;
import org.thoughtcrime.securesms.mms.StickerSlide;
import org.thoughtcrime.securesms.reactions.ReactionsConversationView;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.LongClickMovementMethod;
import org.thoughtcrime.securesms.util.MediaUtil;
@@ -97,6 +100,7 @@ public class ConversationItem extends BaseConversationItem
@Nullable private QuoteView quoteView;
private ConversationItemFooter footer;
private ConversationItemFooter stickerFooter;
private ReactionsConversationView reactionsView;
private TextView groupSender;
private View groupSenderHolder;
private AvatarImageView contactPhoto;
@@ -134,6 +138,7 @@ public class ConversationItem extends BaseConversationItem
this.bodyText = findViewById(R.id.conversation_item_body);
this.footer = findViewById(R.id.conversation_item_footer);
this.stickerFooter = findViewById(R.id.conversation_item_sticker_footer);
this.reactionsView = findViewById(R.id.reactions_view);
this.groupSender = findViewById(R.id.group_message_sender);
this.contactPhoto = findViewById(R.id.contact_photo);
this.contactPhotoHolder = findViewById(R.id.contact_photo_container);
@@ -185,6 +190,7 @@ public class ConversationItem extends BaseConversationItem
setGroupMessageStatus();
setAuthor(messageRecord, showSender);
setMessageSpacing(context);
setReactions(messageRecord);
setFooter(messageRecord, locale);
setQuote(messageRecord);
}
@@ -667,6 +673,19 @@ public class ConversationItem extends BaseConversationItem
activeFooter.setMessageRecord(current, locale);
}
private void setReactions(@NonNull DcMsg current) {
try {
Reactions reactions = rpc.getMsgReactions(dcContext.getAccountId(), current.getId());
if (reactions.getReactions() == null) {
reactionsView.clear();
} else {
reactionsView.setReactions(reactions.getReactions());
}
} catch (RpcException e) {
reactionsView.clear();
}
}
private ConversationItemFooter getActiveFooter(@NonNull DcMsg messageRecord) {
if (hasSticker(messageRecord)) {
return stickerFooter;
@@ -0,0 +1,124 @@
package org.thoughtcrime.securesms.reactions;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import com.b44t.messenger.rpc.Reaction;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.components.emoji.EmojiImageView;
import org.thoughtcrime.securesms.util.ViewUtil;
import java.util.ArrayList;
import java.util.List;
public class ReactionsConversationView extends LinearLayout {
// Normally 6dp, but we have 1dp left+right margin on the pills themselves
private static final int OUTER_MARGIN = ViewUtil.dpToPx(5);
private final List<Reaction> reactions = new ArrayList<>();
private boolean isIncoming;
public ReactionsConversationView(Context context) {
super(context);
init(null);
}
public ReactionsConversationView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
private void init(@Nullable AttributeSet attrs) {
if (attrs != null) {
TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ReactionsConversationView, 0, 0);
isIncoming = typedArray.getInt(R.styleable.ReactionsConversationView_message_type, 0) == 2;
}
}
public void clear() {
this.reactions.clear();
removeAllViews();
}
public void setReactions(List<Reaction> reactions) {
if (reactions.equals(this.reactions)) {
return;
}
clear();
this.reactions.addAll(reactions);
for (Reaction reaction : buildShortenedReactionsList(this.reactions)) {
View pill = buildPill(getContext(), this, reaction);
addView(pill);
}
if (isIncoming) {
ViewUtil.setLeftMargin(this, OUTER_MARGIN);
} else {
ViewUtil.setRightMargin(this, OUTER_MARGIN);
}
}
private static @NonNull List<Reaction> buildShortenedReactionsList(@NonNull List<Reaction> reactions) {
if (reactions.size() > 3) {
List<Reaction> shortened = new ArrayList<>(3);
shortened.add(reactions.get(0));
shortened.add(reactions.get(1));
int count = 0;
boolean isFromSelf = false;
for (int index = 2; index < reactions.size(); index++) {
count += reactions.get(index).getCount();
isFromSelf = isFromSelf || reactions.get(index).isFromSelf();
}
shortened.add(new Reaction(null, count, isFromSelf));
return shortened;
} else {
return reactions;
}
}
private static View buildPill(@NonNull Context context, @NonNull ViewGroup parent, @NonNull Reaction reaction) {
View root = LayoutInflater.from(context).inflate(R.layout.reactions_pill, parent, false);
EmojiImageView emojiView = root.findViewById(R.id.reactions_pill_emoji);
TextView countView = root.findViewById(R.id.reactions_pill_count);
View spacer = root.findViewById(R.id.reactions_pill_spacer);
if (reaction.getEmoji() != null) {
emojiView.setImageEmoji(reaction.getEmoji());
if (reaction.getCount() > 1) {
countView.setText(String.valueOf(reaction.getCount()));
} else {
countView.setVisibility(GONE);
spacer.setVisibility(GONE);
}
} else {
emojiView.setVisibility(GONE);
spacer.setVisibility(GONE);
countView.setText(context.getString(R.string.ReactionsConversationView_plus, reaction.getCount()));
}
if (reaction.isFromSelf()) {
root.setBackground(ContextCompat.getDrawable(context, R.drawable.reaction_pill_background_selected));
countView.setTextColor(ContextCompat.getColor(context, R.color.reaction_pill_text_color_selected));
} else {
root.setBackground(ContextCompat.getDrawable(context, R.drawable.reaction_pill_background));
}
return root;
}
}
@@ -254,6 +254,16 @@ public class ViewUtil {
view.requestLayout();
}
public static void setRightMargin(@NonNull View view, int margin) {
if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_LTR) {
((ViewGroup.MarginLayoutParams) view.getLayoutParams()).rightMargin = margin;
} else {
((ViewGroup.MarginLayoutParams) view.getLayoutParams()).leftMargin = margin;
}
view.forceLayout();
view.requestLayout();
}
public static void setTopMargin(@NonNull View view, int margin) {
((ViewGroup.MarginLayoutParams) view.getLayoutParams()).topMargin = margin;
view.requestLayout();