From ee1a0cc666476b659780c8b22ae0c0cdadaddcf9 Mon Sep 17 00:00:00 2001 From: Grishka Date: Fri, 13 Sep 2024 01:21:23 +0300 Subject: [PATCH] Allow "new posts" button to be swiped away --- .../fragments/HomeTimelineFragment.java | 8 +- .../ui/views/NewPostsButtonContainer.java | 84 +++++++++++++++++++ .../src/main/res/layout/fragment_timeline.xml | 4 +- 3 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 mastodon/src/main/java/org/joinmastodon/android/ui/views/NewPostsButtonContainer.java diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java index 66dbd5c7..8aef7868 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java @@ -4,6 +4,7 @@ import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -34,7 +35,6 @@ import android.widget.Toolbar; import com.squareup.otto.Subscribe; -import org.joinmastodon.android.BuildConfig; import org.joinmastodon.android.E; import org.joinmastodon.android.R; import org.joinmastodon.android.api.MastodonAPIRequest; @@ -61,6 +61,7 @@ import org.joinmastodon.android.ui.utils.DiscoverInfoBannerHelper; import org.joinmastodon.android.ui.viewcontrollers.HomeTimelineMenuController; import org.joinmastodon.android.ui.viewcontrollers.ToolbarDropdownMenuController; import org.joinmastodon.android.ui.views.FixedAspectRatioImageView; +import org.joinmastodon.android.ui.views.NewPostsButtonContainer; import org.joinmastodon.android.updater.GithubSelfUpdater; import org.parceler.Parcels; @@ -89,7 +90,7 @@ public class HomeTimelineFragment extends StatusListFragment implements ToolbarD private FixedAspectRatioImageView listsDropdownArrow; private TextView listsDropdownText; private Button newPostsBtn; - private View newPostsBtnWrap; + private NewPostsButtonContainer newPostsBtnWrap; private boolean newPostsBtnShown; private AnimatorSet currentNewPostsAnim; private ToolbarDropdownMenuController dropdownController; @@ -249,6 +250,7 @@ public class HomeTimelineFragment extends StatusListFragment implements ToolbarD } } + @SuppressLint("ClickableViewAccessibility") @Override public void onViewCreated(View view, Bundle savedInstanceState){ super.onViewCreated(view, savedInstanceState); @@ -267,6 +269,7 @@ public class HomeTimelineFragment extends StatusListFragment implements ToolbarD newPostsBtnWrap.setAlpha(0f); newPostsBtnWrap.setTranslationY(V.dp(-56)); } + newPostsBtnWrap.setOnHideButtonListener(this::hideNewPostsButton); updateToolbarLogo(); list.addOnScrollListener(new RecyclerView.OnScrollListener(){ @Override @@ -602,6 +605,7 @@ public class HomeTimelineFragment extends StatusListFragment implements ToolbarD @Override public void onAnimationEnd(Animator animation){ newPostsBtnWrap.setVisibility(View.GONE); + newPostsBtn.setTranslationY(0); currentNewPostsAnim=null; } }); diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/views/NewPostsButtonContainer.java b/mastodon/src/main/java/org/joinmastodon/android/ui/views/NewPostsButtonContainer.java new file mode 100644 index 00000000..c6f0f8ad --- /dev/null +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/views/NewPostsButtonContainer.java @@ -0,0 +1,84 @@ +package org.joinmastodon.android.ui.views; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.util.AttributeSet; +import android.util.Log; +import android.view.GestureDetector; +import android.view.MotionEvent; +import android.view.ViewConfiguration; +import android.widget.FrameLayout; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import me.grishka.appkit.utils.CubicBezierInterpolator; + +public class NewPostsButtonContainer extends FrameLayout{ + private GestureDetector gestureDetector; + private Runnable onHideButtonListener; + private float touchslop; + + public NewPostsButtonContainer(Context context){ + this(context, null); + } + + public NewPostsButtonContainer(Context context, AttributeSet attrs){ + this(context, attrs, 0); + } + + public NewPostsButtonContainer(Context context, AttributeSet attrs, int defStyle){ + super(context, attrs, defStyle); + gestureDetector=new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){ + private float totalYOffset; + + @Override + public boolean onDown(@NonNull MotionEvent e){ + totalYOffset=0; + getChildAt(0).animate().cancel(); + return false; + } + + @Override + public boolean onScroll(@Nullable MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY){ + totalYOffset+=distanceY; + getChildAt(0).setTranslationY(-Math.max(0, totalYOffset)); + return totalYOffset>0; + } + }); + gestureDetector.setIsLongpressEnabled(false); + touchslop=ViewConfiguration.get(context).getScaledTouchSlop(); + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev){ + if(gestureDetector.onTouchEvent(ev)) + return true; + return super.onInterceptTouchEvent(ev); + } + + @SuppressLint("ClickableViewAccessibility") + @Override + public boolean onTouchEvent(MotionEvent ev){ + boolean r=gestureDetector.onTouchEvent(ev); + if(ev.getAction()==MotionEvent.ACTION_UP){ + if(!r){ + if(getChildAt(0).getTranslationY()<-touchslop){ + onHideButtonListener.run(); + }else{ + animateBack(); + } + } + }else if(ev.getAction()==MotionEvent.ACTION_CANCEL){ + animateBack(); + } + return r; + } + + private void animateBack(){ + getChildAt(0).animate().translationY(0).setDuration(150).setInterpolator(CubicBezierInterpolator.DEFAULT).start(); + } + + public void setOnHideButtonListener(Runnable onHideButtonListener){ + this.onHideButtonListener=onHideButtonListener; + } +} diff --git a/mastodon/src/main/res/layout/fragment_timeline.xml b/mastodon/src/main/res/layout/fragment_timeline.xml index e39c2057..8f44ecec 100644 --- a/mastodon/src/main/res/layout/fragment_timeline.xml +++ b/mastodon/src/main/res/layout/fragment_timeline.xml @@ -34,7 +34,7 @@ android:contentDescription="@string/new_post" android:src="@drawable/ic_edit_24px"/> - - +