1package eu.siacs.conversations.ui;
2
3import android.view.View;
4import android.view.ViewGroup;
5import android.util.TypedValue;
6
7import com.cheogram.android.EmojiSearch;
8
9import com.google.android.material.chip.Chip;
10import com.google.android.material.chip.ChipGroup;
11import com.google.android.material.color.MaterialColors;
12import com.google.common.collect.Collections2;
13import com.google.common.collect.ImmutableSet;
14
15import eu.siacs.conversations.R;
16import eu.siacs.conversations.entities.Reaction;
17
18import java.util.Collection;
19import java.util.List;
20import java.util.Map;
21import java.util.function.Consumer;
22
23public class BindingAdapters {
24
25 public static void setReactionsOnReceived(
26 final ChipGroup chipGroup,
27 final Reaction.Aggregated reactions,
28 final Consumer<Collection<String>> onModifiedReactions,
29 final Consumer<EmojiSearch.CustomEmoji> onCustomReaction,
30 final Runnable addReaction) {
31 setReactions(chipGroup, reactions, true, onModifiedReactions, onCustomReaction, addReaction);
32 }
33
34 public static void setReactionsOnSent(
35 final ChipGroup chipGroup,
36 final Reaction.Aggregated reactions,
37 final Consumer<Collection<String>> onModifiedReactions) {
38 setReactions(chipGroup, reactions, false, onModifiedReactions, null, null);
39 }
40
41 private static void setReactions(
42 final ChipGroup chipGroup,
43 final Reaction.Aggregated aggregated,
44 final boolean onReceived,
45 final Consumer<Collection<String>> onModifiedReactions,
46 final Consumer<EmojiSearch.CustomEmoji> onCustomReaction,
47 final Runnable addReaction) {
48 final var context = chipGroup.getContext();
49 final var size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35, context.getResources().getDisplayMetrics());
50 final var corner = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35, context.getResources().getDisplayMetrics());
51 final var layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, size);
52 final List<Map.Entry<EmojiSearch.Emoji, Integer>> reactions = aggregated.reactions;
53 if (reactions == null || reactions.isEmpty()) {
54 chipGroup.setVisibility(View.GONE);
55 } else {
56 chipGroup.removeAllViews();
57 chipGroup.setVisibility(View.VISIBLE);
58 for (final var reaction : reactions) {
59 final var emoji = reaction.getKey();
60 final var count = reaction.getValue();
61 final Chip chip = new Chip(chipGroup.getContext());
62 //chip.setEnsureMinTouchTargetSize(false);
63 chip.setChipMinHeight(size-32.0f);
64 chip.ensureAccessibleTouchTarget(size);
65 chip.setLayoutParams(layoutParams);
66 chip.setChipCornerRadius(corner);
67 emoji.setupChip(chip, count);
68 final boolean oneOfOurs = aggregated.ourReactions.contains(emoji.toString());
69 // received = surface; sent = surface high matches bubbles
70 if (oneOfOurs) {
71 chip.setChipBackgroundColor(
72 MaterialColors.getColorStateListOrNull(
73 context,
74 com.google.android.material.R.attr
75 .colorSurfaceContainerHighest));
76 } else {
77 chip.setChipBackgroundColor(
78 MaterialColors.getColorStateListOrNull(
79 context,
80 com.google.android.material.R.attr.colorSurfaceContainerLow));
81 }
82 chip.setTextEndPadding(0.0f);
83 chip.setTextStartPadding(0.0f);
84 chip.setOnClickListener(
85 v -> {
86 if (oneOfOurs) {
87 onModifiedReactions.accept(
88 ImmutableSet.copyOf(
89 Collections2.filter(
90 aggregated.ourReactions,
91 r -> !r.equals(emoji.toString()))));
92 } else {
93 if (emoji instanceof EmojiSearch.CustomEmoji) {
94 onCustomReaction.accept((EmojiSearch.CustomEmoji) emoji);
95 } else {
96 onModifiedReactions.accept(
97 new ImmutableSet.Builder<String>()
98 .addAll(aggregated.ourReactions)
99 .add(emoji.toString())
100 .build());
101 }
102 }
103 });
104 chipGroup.addView(chip);
105 }
106 if (addReaction != null) {
107 final Chip chip = new Chip(chipGroup.getContext());
108 chip.setChipMinHeight(size-32.0f);
109 chip.ensureAccessibleTouchTarget(size);
110 chip.setLayoutParams(layoutParams);
111 chip.setChipCornerRadius(corner);
112 chip.setChipIconResource(R.drawable.ic_add_reaction_24dp);
113 //chip.setChipStrokeColor(
114 // MaterialColors.getColorStateListOrNull(
115 // chipGroup.getContext(),
116 // com.google.android.material.R.attr.colorTertiary));
117 chip.setChipBackgroundColor(
118 MaterialColors.getColorStateListOrNull(
119 context,
120 com.google.android.material.R.attr.colorSurfaceContainerLow));
121 chip.setChipIconTint(
122 MaterialColors.getColorStateListOrNull(
123 context,
124 com.google.android.material.R.attr.colorOnSurface));
125 //chip.setEnsureMinTouchTargetSize(false);
126 chip.setTextEndPadding(0.0f);
127 chip.setTextStartPadding(0.0f);
128 chip.setOnClickListener(v -> addReaction.run());
129 chipGroup.addView(chip);
130 }
131 }
132 }
133}