From 8520c0c8adbde978cc2be93388ebbc253c0f4eea Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Mon, 14 Nov 2022 15:07:15 +1100 Subject: Swipe to see images in gallery in gallery layout. --- .../PostGalleryTypeImageRecyclerViewAdapter.java | 12 +- .../adapters/PostRecyclerViewAdapter.java | 497 +++++++++++++++------ .../res/layout/item_post_card_2_gallery_type.xml | 2 +- .../res/layout/item_post_gallery_gallery_type.xml | 47 ++ app/src/main/res/layout/item_post_gallery_type.xml | 2 +- 5 files changed, 422 insertions(+), 138 deletions(-) create mode 100644 app/src/main/res/layout/item_post_gallery_gallery_type.xml (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java index c9d61bf8..6d98405a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java @@ -67,6 +67,17 @@ public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapte } ((ImageViewHolder) holder).binding.errorTextViewItemGalleryImageInPostFeed.setVisibility(View.GONE); ((ImageViewHolder) holder).binding.progressBarItemGalleryImageInPostFeed.setVisibility(View.VISIBLE); + + ((ImageViewHolder) holder).binding.imageViewItemGalleryImageInPostFeed.setRatio(ratio); + + ((ImageViewHolder) holder).binding.imageViewItemGalleryImageInPostFeed.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { + @Override + public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { + ((ImageViewHolder) holder).binding.imageViewItemGalleryImageInPostFeed.removeOnLayoutChangeListener(this); + loadImage(((ImageViewHolder) holder)); + } + }); + loadImage((ImageViewHolder) holder); } } @@ -82,7 +93,6 @@ public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapte } private void loadImage(ImageViewHolder holder) { - holder.binding.imageViewItemGalleryImageInPostFeed.setRatio(ratio); RequestBuilder imageRequestBuilder = glide.load(galleryImages.get(holder.getBindingAdapterPosition()).url).listener(new RequestListener<>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index c6baefb1..182204e5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -11,7 +11,6 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Handler; -import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; @@ -88,6 +87,7 @@ import ml.docilealligator.infinityforreddit.customviews.AspectRatioGifImageView; import ml.docilealligator.infinityforreddit.customviews.SwipeLockInterface; import ml.docilealligator.infinityforreddit.customviews.SwipeLockLinearLayoutManager; import ml.docilealligator.infinityforreddit.databinding.ItemPostCard2GalleryTypeBinding; +import ml.docilealligator.infinityforreddit.databinding.ItemPostGalleryGalleryTypeBinding; import ml.docilealligator.infinityforreddit.databinding.ItemPostGalleryTypeBinding; import ml.docilealligator.infinityforreddit.events.PostUpdateEventToPostDetailFragment; import ml.docilealligator.infinityforreddit.fragments.PostFragment; @@ -119,10 +119,11 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter DIFF_CALLBACK = new DiffUtil.ItemCallback() { @Override @@ -416,7 +417,16 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter= 0) { + Post post = getItem(position); + ((PostGalleryBaseGalleryTypeViewHolder) holder).markPostRead(post, false); + } + } + holder.itemView.setBackgroundTintList(ColorStateList.valueOf(mCardViewBackgroundColor)); + ((PostGalleryBaseGalleryTypeViewHolder) holder).frameLayout.setVisibility(View.GONE); + ((PostGalleryBaseGalleryTypeViewHolder) holder).noPreviewImageView.setVisibility(View.GONE); } } @@ -3179,6 +3210,9 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter minTouchSlop || Math.abs(e.getRawY() - downY) > minTouchSlop) { + dragged = true; + } + if (!dragged) { + if (System.currentTimeMillis() - downTime >= longClickThreshold) { + onLongClick(); + } + } + break; + case MotionEvent.ACTION_UP: + if (!dragged) { + if (System.currentTimeMillis() - downTime < longClickThreshold) { + onClick(); + } + } + case MotionEvent.ACTION_CANCEL: + downX = 0; + downY = 0; + dragged = false; + + } + return false; + } + + @Override + public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) { + + } + + @Override + public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { + + } + }); + + noPreviewImageView.setOnClickListener(view -> { + onClick(); + }); + + noPreviewImageView.setOnLongClickListener(view -> onLongClick()); + } + + void onClick() { + int position = getBindingAdapterPosition(); + if (position >= 0 && canStartActivity) { + Post post = getItem(position); + if (post != null) { + markPostRead(post, true); + + if (post.getPostType() == Post.TEXT_TYPE || !mSharedPreferences.getBoolean(SharedPreferencesUtils.CLICK_TO_SHOW_MEDIA_IN_GALLERY_LAYOUT, false)) { + openViewPostDetailActivity(post, getBindingAdapterPosition()); + } else { + openMedia(post); + } + } + } + } + + boolean onLongClick() { + int position = getBindingAdapterPosition(); + if (position >= 0 && canStartActivity) { + Post post = getItem(position); + if (post != null) { + markPostRead(post, true); + + if (post.getPostType() == Post.TEXT_TYPE || mSharedPreferences.getBoolean(SharedPreferencesUtils.CLICK_TO_SHOW_MEDIA_IN_GALLERY_LAYOUT, false)) { + openViewPostDetailActivity(post, getBindingAdapterPosition()); + } else { + openMedia(post); + } + } + } + + return true; + } + + void markPostRead(Post post, boolean changePostItemColor) { + if (mAccessToken != null && !post.isRead() && mMarkPostsAsRead) { + post.markAsRead(true); + if (changePostItemColor) { + itemView.setBackgroundTintList(ColorStateList.valueOf(mReadPostCardViewBackgroundColor)); + } + if (mActivity != null && mActivity instanceof MarkPostAsReadInterface) { + ((MarkPostAsReadInterface) mActivity).markPostAsRead(post); + mFragment.markPostAsRead(post); + } + } + } + } + + class PostGalleryGalleryTypeViewHolder extends PostGalleryBaseGalleryTypeViewHolder { + + public PostGalleryGalleryTypeViewHolder(@NonNull ItemPostGalleryGalleryTypeBinding binding) { + super(binding.getRoot(), binding.galleryFrameLayoutItemPostGalleryGalleryType, + binding.galleryRecyclerViewItemPostGalleryGalleryType, binding.imageIndexTextViewItemPostGalleryGalleryType, + binding.imageViewNoPreviewItemPostGalleryGalleryType); + } + } + class PostCard2VideoAutoplayViewHolder extends PostBaseViewHolder implements ToroPlayer { @BindView(R.id.icon_gif_image_view_item_post_card_2_video_autoplay) AspectRatioGifImageView iconGifImageView; diff --git a/app/src/main/res/layout/item_post_card_2_gallery_type.xml b/app/src/main/res/layout/item_post_card_2_gallery_type.xml index 5ad3f24b..1455d1fd 100644 --- a/app/src/main/res/layout/item_post_card_2_gallery_type.xml +++ b/app/src/main/res/layout/item_post_card_2_gallery_type.xml @@ -21,7 +21,7 @@ diff --git a/app/src/main/res/layout/item_post_gallery_gallery_type.xml b/app/src/main/res/layout/item_post_gallery_gallery_type.xml new file mode 100644 index 00000000..d1ee61b6 --- /dev/null +++ b/app/src/main/res/layout/item_post_gallery_gallery_type.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_post_gallery_type.xml b/app/src/main/res/layout/item_post_gallery_type.xml index a817cd40..13bd9517 100644 --- a/app/src/main/res/layout/item_post_gallery_type.xml +++ b/app/src/main/res/layout/item_post_gallery_type.xml @@ -198,7 +198,7 @@ -- cgit v1.2.3