From 9a1046eda20151d6b4df519a57cdcb7055d8e1ca Mon Sep 17 00:00:00 2001 From: cmp Date: Fri, 10 Mar 2023 22:38:43 -0600 Subject: Support long press for posts and comments to copy the post or comment link. (#1313) --- .../infinityforreddit/activities/BaseActivity.java | 19 +++++++++++++++- .../adapters/HistoryPostRecyclerViewAdapter.java | 26 ++++++++++++++++++++++ .../adapters/PostDetailRecyclerViewAdapter.java | 5 +++++ .../adapters/PostRecyclerViewAdapter.java | 26 ++++++++++++++++++++++ .../CommentMoreBottomSheetFragment.java | 6 +++++ .../ShareLinkBottomSheetFragment.java | 13 +---------- 6 files changed, 82 insertions(+), 13 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java index bd94a89e..6c734576 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java @@ -6,6 +6,9 @@ import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; import android.annotation.SuppressLint; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; import android.content.SharedPreferences; import android.content.res.ColorStateList; import android.content.res.Configuration; @@ -21,6 +24,7 @@ import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; @@ -400,4 +404,17 @@ public abstract class BaseActivity extends AppCompatActivity implements CustomFo public void unlockSwipeRightToGoBack() { } -} \ No newline at end of file + + public void copyLink(String link) { + ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + if (clipboard != null) { + ClipData clip = ClipData.newPlainText("simple text", link); + clipboard.setPrimaryClip(clip); + if (android.os.Build.VERSION.SDK_INT < 33) { + Toast.makeText(this, R.string.copy_success, Toast.LENGTH_SHORT).show(); + } + } else { + Toast.makeText(this, R.string.copy_link_failed, Toast.LENGTH_SHORT).show(); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java index 6115195f..18e4f61b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java @@ -2578,6 +2578,19 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter { + int position = getBindingAdapterPosition(); + if (position < 0) { + return false; + } + Post post = getItem(position); + if (post != null) { + mActivity.copyLink(post.getPermalink()); + return true; + } + return false; + }); } void setBaseView(AspectRatioGifImageView iconGifImageView, @@ -3886,6 +3899,19 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter { + int position = getBindingAdapterPosition(); + if (position < 0) { + return false; + } + Post post = getItem(position); + if (post != null) { + mActivity.copyLink(post.getPermalink()); + return true; + } + return false; + }); + requestListener = 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/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index 799c13a7..7c1a8b03 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -1449,6 +1449,11 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { + mActivity.copyLink(mPost.getPermalink()); + return true; + }); + if (mVoteButtonsOnTheRight) { ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(mBottomConstraintLayout); 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 97a0f777..08c91772 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -2691,6 +2691,19 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + int position = getBindingAdapterPosition(); + if (position < 0) { + return false; + } + Post post = getItem(position); + if (post != null) { + mActivity.copyLink(post.getPermalink()); + return true; + } + return false; + }); } void setBaseView(AspectRatioGifImageView iconGifImageView, @@ -4036,6 +4049,19 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + int position = getBindingAdapterPosition(); + if (position < 0) { + return false; + } + Post post = getItem(position); + if (post != null) { + mActivity.copyLink(post.getPermalink()); + return true; + } + return false; + }); + requestListener = 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/bottomsheetfragments/CommentMoreBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CommentMoreBottomSheetFragment.java index 4ff48bec..3ec419db 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CommentMoreBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/CommentMoreBottomSheetFragment.java @@ -179,6 +179,12 @@ public class CommentMoreBottomSheetFragment extends LandscapeExpandedRoundedBott } }); + shareTextView.setOnLongClickListener(view -> { + dismiss(); + activity.copyLink(comment.getPermalink()); + return true; + }); + copyTextView.setOnClickListener(view -> { dismiss(); CopyTextBottomSheetFragment.show(activity.getSupportFragmentManager(), diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/ShareLinkBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/ShareLinkBottomSheetFragment.java index f50e17d7..db0cb4e4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/ShareLinkBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/ShareLinkBottomSheetFragment.java @@ -2,8 +2,6 @@ package ml.docilealligator.infinityforreddit.bottomsheetfragments; import android.content.ActivityNotFoundException; -import android.content.ClipData; -import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; @@ -136,16 +134,7 @@ public class ShareLinkBottomSheetFragment extends LandscapeExpandedRoundedBottom } private void copyLink(String link) { - ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE); - if (clipboard != null) { - ClipData clip = ClipData.newPlainText("simple text", link); - clipboard.setPrimaryClip(clip); - if (android.os.Build.VERSION.SDK_INT < 33) { - Toast.makeText(activity, R.string.copy_success, Toast.LENGTH_SHORT).show(); - } - } else { - Toast.makeText(activity, R.string.copy_link_failed, Toast.LENGTH_SHORT).show(); - } + activity.copyLink(link); } @Override -- cgit v1.2.3