|
11 | 11 | import android.util.DisplayMetrics;
|
12 | 12 | import android.view.Gravity;
|
13 | 13 | import android.view.LayoutInflater;
|
| 14 | +import android.view.MotionEvent; |
14 | 15 | import android.view.View;
|
| 16 | +import android.view.ViewGroup; |
15 | 17 | import android.view.Window;
|
16 | 18 | import android.view.WindowManager;
|
17 | 19 | import android.widget.AdapterView;
|
18 | 20 | import android.widget.Button;
|
19 | 21 | import android.widget.GridView;
|
| 22 | +import android.widget.PopupWindow; |
| 23 | +import android.widget.TextView; |
20 | 24 |
|
| 25 | +import androidx.annotation.ColorInt; |
21 | 26 | import androidx.annotation.DrawableRes;
|
22 | 27 |
|
23 | 28 | import java.util.ArrayList;
|
@@ -77,11 +82,42 @@ public class ReactButton
|
77 | 82 | */
|
78 | 83 | private int mReactDialogShape = R.drawable.react_dialog_shape;
|
79 | 84 |
|
| 85 | + /** |
| 86 | + * Enable/Disable the reactions tooltip feature |
| 87 | + */ |
| 88 | + private boolean enableReactionTooltip = false; |
| 89 | + |
| 90 | + /** |
| 91 | + * The offset between tooltip and the reaction icon |
| 92 | + */ |
| 93 | + private int mTooltipOffsetFromReaction = 100; |
| 94 | + |
| 95 | + /** |
| 96 | + * The default color for React tooltip text |
| 97 | + */ |
| 98 | + @ColorInt private int mReactTooltipTextColor = Color.WHITE; |
| 99 | + |
| 100 | + /** |
| 101 | + * The default drawable shape for tooltip text |
| 102 | + */ |
| 103 | + @DrawableRes private int mReactTooltipShape = R.drawable.react_tooltip_shape; |
| 104 | + |
| 105 | + /** |
| 106 | + * The min height of tooltip |
| 107 | + */ |
| 108 | + private static final int TOOLTIP_VIEW_MIN_HEIGHT = 50; |
| 109 | + |
| 110 | + /** |
| 111 | + * Reaction image view padding |
| 112 | + */ |
| 113 | + private static final int ICON_PADDING = 10; |
| 114 | + |
80 | 115 | /**
|
81 | 116 | * The size of reaction icon in dp
|
82 | 117 | * Icon size + icon padding * 2
|
83 | 118 | */
|
84 |
| - private static final int ICON_SIZE_WITH_PADDING = 55; |
| 119 | + private static final int ICON_SIZE_WITH_PADDING = 45 + ICON_PADDING; |
| 120 | + |
85 | 121 |
|
86 | 122 | /**
|
87 | 123 | * Full reaction icon size converted from dp
|
@@ -176,6 +212,49 @@ public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
|
176 | 212 | }
|
177 | 213 | });
|
178 | 214 |
|
| 215 | + if (enableReactionTooltip) { |
| 216 | + final PopupWindow[] popupWindow = new PopupWindow[1]; |
| 217 | + reactionsGrid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { |
| 218 | + @Override |
| 219 | + public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) { |
| 220 | + Reaction currentReaction = mReactions.get(position); |
| 221 | + |
| 222 | + View tooltipView = LayoutInflater.from(context).inflate(R.layout.react_tooltip_layout, null); |
| 223 | + tooltipView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| 224 | + tooltipView.setBackgroundResource(mReactTooltipShape); |
| 225 | + |
| 226 | + TextView tooltipTextView = tooltipView.findViewById(R.id.react_tooltip_text); |
| 227 | + tooltipTextView.setTextColor(mReactTooltipTextColor); |
| 228 | + tooltipTextView.setText(currentReaction.getReactText()); |
| 229 | + |
| 230 | + popupWindow[0] = new PopupWindow(tooltipView, tooltipView.getMeasuredWidth(), tooltipView.getMeasuredHeight(), true); |
| 231 | + popupWindow[0].setOutsideTouchable(true); |
| 232 | + |
| 233 | + final int[] viewLocation = new int[2]; |
| 234 | + view.getLocationOnScreen(viewLocation); |
| 235 | + float xOffset = viewLocation[0]; |
| 236 | + float yOffset = viewLocation[1] - mTooltipOffsetFromReaction; |
| 237 | + |
| 238 | + if (yOffset <= TOOLTIP_VIEW_MIN_HEIGHT) yOffset += mTooltipOffsetFromReaction * 2 + TOOLTIP_VIEW_MIN_HEIGHT; |
| 239 | + |
| 240 | + popupWindow[0].showAtLocation(tooltipView, Gravity.NO_GRAVITY, (int) xOffset, (int) yOffset); |
| 241 | + return false; |
| 242 | + } |
| 243 | + }); |
| 244 | + |
| 245 | + reactionsGrid.setOnTouchListener(new OnTouchListener() { |
| 246 | + |
| 247 | + @SuppressLint("ClickableViewAccessibility") |
| 248 | + @Override |
| 249 | + public boolean onTouch(View v, MotionEvent event) { |
| 250 | + if (event.getAction() == MotionEvent.ACTION_UP) { |
| 251 | + if (popupWindow[0] != null) popupWindow[0].dismiss(); |
| 252 | + } |
| 253 | + return false; |
| 254 | + } |
| 255 | + }); |
| 256 | + } |
| 257 | + |
179 | 258 | AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
|
180 | 259 | dialogBuilder.setView(dialogView);
|
181 | 260 | mReactAlertDialog = dialogBuilder.create();
|
@@ -227,6 +306,34 @@ public void setReactionDialogShape(@DrawableRes int drawableShape) {
|
227 | 306 | this.mReactDialogShape = drawableShape;
|
228 | 307 | }
|
229 | 308 |
|
| 309 | + /** |
| 310 | + * @param offset from the reaction icon |
| 311 | + */ |
| 312 | + public void setTooltipOffsetFromReaction(int offset) { |
| 313 | + mTooltipOffsetFromReaction = offset; |
| 314 | + } |
| 315 | + |
| 316 | + /** |
| 317 | + * @param color tooltip text color |
| 318 | + */ |
| 319 | + public void setReactionTooltipTextColor(@ColorInt int color) { |
| 320 | + mReactTooltipTextColor = color; |
| 321 | + } |
| 322 | + |
| 323 | + /** |
| 324 | + * @param drawableShape tooltip shape for the layout |
| 325 | + */ |
| 326 | + public void setReactionTooltipShape(@DrawableRes int drawableShape) { |
| 327 | + mReactTooltipShape = drawableShape; |
| 328 | + } |
| 329 | + |
| 330 | + /** |
| 331 | + * @param isEnable enable/disable the reactions tooltip feature |
| 332 | + */ |
| 333 | + public void setEnableReactionTooltip(boolean isEnable) { |
| 334 | + enableReactionTooltip = isEnable; |
| 335 | + } |
| 336 | + |
230 | 337 | /**
|
231 | 338 | * @param reactions Array of six Reactions to update default six Reactions
|
232 | 339 | */
|
|
0 commit comments