aboutsummaryrefslogtreecommitdiff
path: root/app/src
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2020-09-03 01:34:32 +0000
committerAlex Ning <chineseperson5@gmail.com>2020-09-03 01:34:32 +0000
commit98b324f1902888f7382d4ed8d42eed4832ed4d5f (patch)
tree28c40426d572ff66322f6e263c94517cf83f4afe /app/src
parent7911f4f3813e789f6a7bf6ecc4b8739f1a86a0b6 (diff)
downloadinfinity-for-reddit-98b324f1902888f7382d4ed8d42eed4832ed4d5f.tar
infinity-for-reddit-98b324f1902888f7382d4ed8d42eed4832ed4d5f.tar.gz
infinity-for-reddit-98b324f1902888f7382d4ed8d42eed4832ed4d5f.tar.bz2
infinity-for-reddit-98b324f1902888f7382d4ed8d42eed4832ed4d5f.tar.lz
infinity-for-reddit-98b324f1902888f7382d4ed8d42eed4832ed4d5f.tar.xz
infinity-for-reddit-98b324f1902888f7382d4ed8d42eed4832ed4d5f.tar.zst
infinity-for-reddit-98b324f1902888f7382d4ed8d42eed4832ed4d5f.zip
Implement barebone swipe to upvote or downvote.
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java21
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java77
2 files changed, 98 insertions, 0 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java
index c9cf8e96..13fa5d90 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java
@@ -10,6 +10,7 @@ import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
+import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -30,6 +31,7 @@ import androidx.constraintlayout.widget.ConstraintSet;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
+import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
@@ -509,10 +511,12 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
switch (post.getVoteType()) {
case 1:
//Upvoted
+ Log.i("asdasdf", "upvoted");
((PostBaseViewHolder) holder).upvoteButton.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN);
((PostBaseViewHolder) holder).scoreTextView.setTextColor(mUpvotedColor);
break;
case -1:
+ Log.i("asdasdf", "downvoted");
//Downvoted
((PostBaseViewHolder) holder).downvoteButton.setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN);
((PostBaseViewHolder) holder).scoreTextView.setTextColor(mDownvotedColor);
@@ -1240,6 +1244,19 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
return null;
}
+ public void onItemSwipe(RecyclerView.ViewHolder viewHolder, int direction) {
+ if (viewHolder instanceof PostBaseViewHolder) {
+ Post post = getItem(viewHolder.getAdapterPosition());
+ if (post != null) {
+ if (direction == ItemTouchHelper.LEFT || direction == ItemTouchHelper.START) {
+ ((PostBaseViewHolder) viewHolder).upvoteButton.performClick();
+ } else {
+ ((PostBaseViewHolder) viewHolder).downvoteButton.performClick();
+ }
+ }
+ }
+ }
+
public interface Callback {
void retryLoadingMore();
@@ -1510,12 +1527,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if (previousVoteType != 1) {
//Not upvoted before
+ Log.i("asdasdf", "not 1 before");
post.setVoteType(1);
newVoteType = APIUtils.DIR_UPVOTE;
upvoteButton
.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN);
scoreTextView.setTextColor(mUpvotedColor);
} else {
+ Log.i("asdasdf", "1 before");
//Upvoted before
post.setVoteType(0);
newVoteType = APIUtils.DIR_UNVOTE;
@@ -1529,11 +1548,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
@Override
public void onVoteThingSuccess(int position1) {
if (newVoteType.equals(APIUtils.DIR_UPVOTE)) {
+ Log.i("asdasdf", "1");
post.setVoteType(1);
upvoteButton
.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN);
scoreTextView.setTextColor(mUpvotedColor);
} else {
+ Log.i("asdasdf", "0");
post.setVoteType(0);
upvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN);
scoreTextView.setTextColor(mPostIconAndInfoColor);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java
index a2097c3b..fe33e43a 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java
@@ -6,7 +6,10 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
+import android.graphics.Canvas;
import android.graphics.Rect;
+import android.graphics.drawable.ColorDrawable;
+import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
@@ -27,9 +30,11 @@ import androidx.annotation.DimenRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
+import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.paging.PagedList;
+import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;
@@ -172,6 +177,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private int maxPosition = -1;
private int postLayout;
private SortType sortType;
+ private ColorDrawable backgroundLeft;
+ private ColorDrawable backgroundRight;
+ private Drawable drawableLeft;
+ private Drawable drawableRight;
+ private float swipeActionThreshold = 0.3f;
+ private ItemTouchHelper touchHelper;
public PostFragment() {
// Required empty public constructor
@@ -610,6 +621,72 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
((ActivityToolbarInterface) activity).displaySortType();
}
+ backgroundLeft = new ColorDrawable(customThemeWrapper.getDownvoted());
+ backgroundRight = new ColorDrawable(customThemeWrapper.getUpvoted());
+ drawableLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_black_24dp, null);
+ drawableRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_black_24dp, null);
+ touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
+ @Override
+ public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
+ int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
+ return makeMovementFlags(0, swipeFlags);
+ }
+
+ @Override
+ public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
+ return false;
+ }
+
+ @Override
+ public boolean isItemViewSwipeEnabled() {
+ return true;
+ }
+
+ @Override
+ public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
+ if (touchHelper != null) {
+ touchHelper.attachToRecyclerView(null);
+ touchHelper.attachToRecyclerView(mPostRecyclerView);
+ if (mAdapter != null) {
+ mAdapter.onItemSwipe(viewHolder, direction);
+ }
+ }
+ }
+
+ @Override
+ public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
+ super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
+
+ View itemView = viewHolder.itemView;
+ int horizontalOffset = (int) Utils.convertDpToPixel(16, activity);
+ if (dX > 0) {
+ if (dX > (itemView.getRight() - itemView.getLeft()) * 0.3) {
+ backgroundLeft.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
+ } else {
+ backgroundLeft.setBounds(0, 0, 0, 0);
+ }
+ drawableLeft.setBounds(itemView.getLeft() + ((int) dX) - horizontalOffset - drawableLeft.getIntrinsicWidth(), (itemView.getBottom() - itemView.getTop() - drawableLeft.getIntrinsicHeight()) / 2, itemView.getLeft() + ((int) dX) - horizontalOffset, (itemView.getBottom() - itemView.getTop() + drawableLeft.getIntrinsicHeight()) / 2);
+ backgroundLeft.draw(c);
+ drawableLeft.draw(c);
+ } else if (dX < 0) {
+ if (-dX > (itemView.getRight() - itemView.getLeft()) * 0.3) {
+ backgroundRight.setBounds(0, itemView.getTop(), itemView.getRight(), itemView.getBottom());
+ } else {
+ backgroundRight.setBounds(0, 0, 0, 0);
+ }
+ drawableRight.setBounds(itemView.getRight() + ((int) dX) + horizontalOffset, (itemView.getBottom() - itemView.getTop() - drawableRight.getIntrinsicHeight()) / 2, itemView.getRight() + ((int) dX) + horizontalOffset + drawableRight.getIntrinsicWidth(), (itemView.getBottom() - itemView.getTop() + drawableRight.getIntrinsicHeight()) / 2);
+ backgroundRight.draw(c);
+ drawableRight.draw(c);
+ }
+ }
+
+ @Override
+ public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
+ return swipeActionThreshold;
+ }
+ });
+
+ touchHelper.attachToRecyclerView(mPostRecyclerView);
mPostRecyclerView.setAdapter(mAdapter);
mPostRecyclerView.setCacheManager(mAdapter);
mPostRecyclerView.setPlayerInitializer(order -> {