Skip to content

Commit

Permalink
Improve MovableFloatingActionButton gesture.
Browse files Browse the repository at this point in the history
  • Loading branch information
Docile-Alligator committed Oct 28, 2024
1 parent 065af14 commit ec3a093
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import androidx.annotation.Nullable;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import ml.docilealligator.infinityforreddit.customviews.slidr.widget.SliderPanel;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;

public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener {
Expand All @@ -28,6 +32,8 @@ public class MovableFloatingActionButton extends FloatingActionButton implements
@Nullable
private SharedPreferences postDetailsSharedPreferences;
private boolean portrait;
@Nullable
private SliderPanel sliderPanel;

public MovableFloatingActionButton(Context context) {
super(context);
Expand All @@ -46,6 +52,16 @@ public MovableFloatingActionButton(Context context, AttributeSet attrs, int defS

private void init() {
setOnTouchListener(this);
new Handler(Looper.getMainLooper()).post(() -> {
ViewParent parent = getParent();
while (parent != null) {
if (parent instanceof SliderPanel) {
sliderPanel = (SliderPanel) parent;
break;
}
parent = parent.getParent();
}
});
}

@Override
Expand All @@ -62,6 +78,10 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
dX = view.getX() - downRawX;
dY = view.getY() - downRawY;

if (sliderPanel != null) {
sliderPanel.lock();
}

return true;

} else if (action == MotionEvent.ACTION_MOVE) {
Expand Down Expand Up @@ -122,13 +142,19 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
float upDX = upRawX - downRawX;
float upDY = upRawY - downRawY;

if (sliderPanel != null) {
sliderPanel.unlock();
}

if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) {
return System.currentTimeMillis() - downTime >= 300 ? performLongClick() : performClick();
} else {

return true;
}
} else {
if (sliderPanel != null) {
sliderPanel.unlock();
}
return super.onTouchEvent(motionEvent);
}
}
Expand Down

0 comments on commit ec3a093

Please sign in to comment.