|
| 1 | +package org.iiab.controller.feedback.presentation; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.SharedPreferences; |
| 6 | +import android.view.MotionEvent; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewConfiguration; |
| 9 | +import android.view.ViewGroup; |
| 10 | + |
| 11 | +import com.google.android.material.floatingactionbutton.FloatingActionButton; |
| 12 | + |
| 13 | +/** |
| 14 | + * Draggable feedback FAB with position persistence, recycled from Code On the Go's |
| 15 | + * FeedbackButtonManager. Distinguishes a tap (fires {@code onTap}) from a drag (moves |
| 16 | + * the button and stores its position as normalized ratios). ADFA-4538. |
| 17 | + */ |
| 18 | +public final class FeedbackFab { |
| 19 | + |
| 20 | + private static final String PREFS = "iiab_delivery"; |
| 21 | + private static final String KEY_X = "fab_x_ratio"; |
| 22 | + private static final String KEY_Y = "fab_y_ratio"; |
| 23 | + |
| 24 | + private FeedbackFab() { |
| 25 | + } |
| 26 | + |
| 27 | + @SuppressLint("ClickableViewAccessibility") |
| 28 | + public static void attach(FloatingActionButton fab, Runnable onTap) { |
| 29 | + if (fab == null) { |
| 30 | + return; |
| 31 | + } |
| 32 | + final Context ctx = fab.getContext(); |
| 33 | + final int slop = ViewConfiguration.get(ctx).getScaledTouchSlop(); |
| 34 | + |
| 35 | + fab.post(() -> applySaved(fab)); |
| 36 | + fab.addOnLayoutChangeListener((v, l, t, r, b, ol, ot, orr, ob) -> applySaved(fab)); |
| 37 | + |
| 38 | + fab.setOnTouchListener(new View.OnTouchListener() { |
| 39 | + float downX, downY, dX, dY; |
| 40 | + boolean dragging; |
| 41 | + |
| 42 | + @Override |
| 43 | + public boolean onTouch(View v, MotionEvent e) { |
| 44 | + switch (e.getActionMasked()) { |
| 45 | + case MotionEvent.ACTION_DOWN: |
| 46 | + downX = e.getRawX(); |
| 47 | + downY = e.getRawY(); |
| 48 | + dX = v.getX() - e.getRawX(); |
| 49 | + dY = v.getY() - e.getRawY(); |
| 50 | + dragging = false; |
| 51 | + return true; |
| 52 | + case MotionEvent.ACTION_MOVE: |
| 53 | + if (!dragging && Math.hypot(e.getRawX() - downX, e.getRawY() - downY) > slop) { |
| 54 | + dragging = true; |
| 55 | + } |
| 56 | + if (dragging) { |
| 57 | + ViewGroup parent = (ViewGroup) v.getParent(); |
| 58 | + if (parent == null) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + float nx = clamp(e.getRawX() + dX, 0, parent.getWidth() - v.getWidth()); |
| 62 | + float ny = clamp(e.getRawY() + dY, 0, parent.getHeight() - v.getHeight()); |
| 63 | + v.setX(nx); |
| 64 | + v.setY(ny); |
| 65 | + } |
| 66 | + return true; |
| 67 | + case MotionEvent.ACTION_UP: |
| 68 | + if (dragging) { |
| 69 | + savePosition(fab); |
| 70 | + } else { |
| 71 | + v.performClick(); |
| 72 | + if (onTap != null) { |
| 73 | + onTap.run(); |
| 74 | + } |
| 75 | + } |
| 76 | + return true; |
| 77 | + default: |
| 78 | + return false; |
| 79 | + } |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + private static float clamp(float v, float lo, float hi) { |
| 85 | + if (hi < lo) { |
| 86 | + return lo; |
| 87 | + } |
| 88 | + return Math.max(lo, Math.min(v, hi)); |
| 89 | + } |
| 90 | + |
| 91 | + private static void applySaved(FloatingActionButton fab) { |
| 92 | + ViewGroup parent = (ViewGroup) fab.getParent(); |
| 93 | + if (parent == null || parent.getWidth() == 0 || parent.getHeight() == 0) { |
| 94 | + return; |
| 95 | + } |
| 96 | + SharedPreferences p = fab.getContext().getSharedPreferences(PREFS, Context.MODE_PRIVATE); |
| 97 | + float xr = p.getFloat(KEY_X, -1f); |
| 98 | + float yr = p.getFloat(KEY_Y, -1f); |
| 99 | + if (xr < 0 || yr < 0) { |
| 100 | + return; |
| 101 | + } |
| 102 | + float availX = parent.getWidth() - fab.getWidth(); |
| 103 | + float availY = parent.getHeight() - fab.getHeight(); |
| 104 | + fab.setX(clamp(xr * availX, 0, availX)); |
| 105 | + fab.setY(clamp(yr * availY, 0, availY)); |
| 106 | + } |
| 107 | + |
| 108 | + private static void savePosition(FloatingActionButton fab) { |
| 109 | + ViewGroup parent = (ViewGroup) fab.getParent(); |
| 110 | + if (parent == null) { |
| 111 | + return; |
| 112 | + } |
| 113 | + float availX = parent.getWidth() - fab.getWidth(); |
| 114 | + float availY = parent.getHeight() - fab.getHeight(); |
| 115 | + if (availX <= 0 || availY <= 0) { |
| 116 | + return; |
| 117 | + } |
| 118 | + fab.getContext().getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit() |
| 119 | + .putFloat(KEY_X, clamp(fab.getX() / availX, 0, 1)) |
| 120 | + .putFloat(KEY_Y, clamp(fab.getY() / availY, 0, 1)) |
| 121 | + .apply(); |
| 122 | + } |
| 123 | +} |
0 commit comments