From 9127d68197b9086d5ec4d3ace2f135b461de4a6c Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 1 Apr 2023 11:01:10 +0200 Subject: [PATCH] render reactions --- .../android/ui/BindingAdapters.java | 62 +++++++++++++++++++ .../res/drawable/ic_add_reaction_24dp.xml | 10 +++ .../main/res/layout/item_message_received.xml | 25 +++++++- app/src/main/res/layout/item_message_sent.xml | 25 +++++++- 4 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 app/src/main/res/drawable/ic_add_reaction_24dp.xml diff --git a/app/src/main/java/im/conversations/android/ui/BindingAdapters.java b/app/src/main/java/im/conversations/android/ui/BindingAdapters.java index e529b6272..4c95b66f8 100644 --- a/app/src/main/java/im/conversations/android/ui/BindingAdapters.java +++ b/app/src/main/java/im/conversations/android/ui/BindingAdapters.java @@ -10,6 +10,8 @@ import androidx.annotation.DrawableRes; import androidx.annotation.NonNull; import androidx.databinding.BindingAdapter; import androidx.lifecycle.LiveData; +import com.google.android.material.chip.Chip; +import com.google.android.material.chip.ChipGroup; import com.google.android.material.color.MaterialColors; import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputLayout; @@ -24,6 +26,9 @@ import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.temporal.ChronoUnit; +import java.util.Locale; +import java.util.Map; +import java.util.Set; public class BindingAdapters { @@ -217,4 +222,61 @@ public class BindingAdapters { imageView.setVisibility(View.VISIBLE); } } + + @BindingAdapter("reactionsOnReceived") + public static void setReactionsOnReceived( + final ChipGroup chipGroup, final Set> reactions) { + setReactions(chipGroup, reactions, true); + } + + @BindingAdapter("reactionsOnSent") + public static void setReactionsOnSent( + final ChipGroup chipGroup, final Set> reactions) { + setReactions(chipGroup, reactions, false); + } + + private static void setReactions( + final ChipGroup chipGroup, + final Set> reactions, + final boolean onReceived) { + if (reactions == null || reactions.isEmpty()) { + chipGroup.setVisibility(View.GONE); + } else { + chipGroup.removeAllViews(); + chipGroup.setVisibility(View.VISIBLE); + for (final Map.Entry reaction : reactions) { + final var count = reaction.getValue(); + final Chip chip = new Chip(chipGroup.getContext()); + chip.setEnsureMinTouchTargetSize(false); + chip.setChipStartPadding(0.0f); + chip.setChipEndPadding(0.0f); + if (count == 1) { + chip.setText(reaction.getKey()); + } else { + chip.setText(String.format(Locale.ENGLISH, "%s %d", reaction.getKey(), count)); + } + chipGroup.addView(chip); + } + if (onReceived) { + final Chip chip = new Chip(chipGroup.getContext()); + chip.setChipIconResource(R.drawable.ic_add_reaction_24dp); + chip.setChipStrokeColor( + MaterialColors.getColorStateListOrNull( + chipGroup.getContext(), + com.google.android.material.R.attr.colorTertiary)); + chip.setChipBackgroundColor( + MaterialColors.getColorStateListOrNull( + chipGroup.getContext(), + com.google.android.material.R.attr.colorTertiaryContainer)); + chip.setChipIconTint( + MaterialColors.getColorStateListOrNull( + chipGroup.getContext(), + com.google.android.material.R.attr.colorOnTertiaryContainer)); + chip.setEnsureMinTouchTargetSize(false); + chip.setTextEndPadding(0.0f); + chip.setTextStartPadding(0.0f); + chipGroup.addView(chip); + } + } + } } diff --git a/app/src/main/res/drawable/ic_add_reaction_24dp.xml b/app/src/main/res/drawable/ic_add_reaction_24dp.xml new file mode 100644 index 000000000..9cd443662 --- /dev/null +++ b/app/src/main/res/drawable/ic_add_reaction_24dp.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/item_message_received.xml b/app/src/main/res/layout/item_message_received.xml index 50cf7f856..642080e19 100644 --- a/app/src/main/res/layout/item_message_received.xml +++ b/app/src/main/res/layout/item_message_received.xml @@ -134,17 +134,38 @@ + + + diff --git a/app/src/main/res/layout/item_message_sent.xml b/app/src/main/res/layout/item_message_sent.xml index 401ea0203..f8557e104 100644 --- a/app/src/main/res/layout/item_message_sent.xml +++ b/app/src/main/res/layout/item_message_sent.xml @@ -123,17 +123,38 @@ + + +