From 5e401a6a96f4b83a79685907a6bee4997ff7e183 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 29 Jan 2020 12:43:57 +0800 Subject: Copy post title and content in ViewPostDetailActivity. --- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 11 +++ .../Fragment/CopyTextBottomSheetFragment.java | 110 +++++++++++++++++++++ .../Fragment/SortTypeBottomSheetFragment.java | 2 +- .../infinityforreddit/ParsePost.java | 2 + .../infinityforreddit/Post/Post.java | 11 +++ 5 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CopyTextBottomSheetFragment.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index 14dab410..b15d9416 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -74,6 +74,7 @@ import ml.docilealligator.infinityforreddit.CommentData; import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; import ml.docilealligator.infinityforreddit.CustomView.MarkwonLinearLayoutManager; import ml.docilealligator.infinityforreddit.FetchComment; +import ml.docilealligator.infinityforreddit.Fragment.CopyTextBottomSheetFragment; import ml.docilealligator.infinityforreddit.Fragment.ModifyCommentBottomSheetFragment; import ml.docilealligator.infinityforreddit.Fragment.ShareLinkBottomSheetFragment; import ml.docilealligator.infinityforreddit.Post.Post; @@ -126,6 +127,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { + Bundle bundle = new Bundle(); + bundle.putString(CopyTextBottomSheetFragment.EXTRA_RAW_TEXT, mPost.getSelfTextPlain()); + bundle.putString(CopyTextBottomSheetFragment.EXTRA_MARKDOWN, mPost.getSelfText()); + mCopyTextBottomSheetFragment.setArguments(bundle); + mCopyTextBottomSheetFragment.show(mActivity.getSupportFragmentManager(), mCopyTextBottomSheetFragment.getTag()); + return true; + }); } @Override @@ -225,6 +235,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { + showCopyDialog(rawText); + dismiss(); + }); + + copyMarkdownTextView.setOnClickListener(view -> { + showCopyDialog(markdownText); + dismiss(); + }); + + copyAllRawTextTextView.setOnClickListener(view -> { + copyText(rawText); + dismiss(); + }); + + copyAllMarkdownTextView.setOnClickListener(view -> { + copyText(markdownText); + dismiss(); + }); + + return rootView; + } + + private void showCopyDialog(String text) { + LayoutInflater inflater = activity.getLayoutInflater(); + View layout = inflater.inflate(R.layout.copy_text_material_dialog, null); + TextView textView = layout.findViewById(R.id.text_view_copy_text_material_dialog); + textView.setText(text); + new MaterialAlertDialogBuilder(activity, R.style.CopyTextMaterialAlertDialogTheme) + .setTitle(R.string.copy_text) + .setView(layout) + .setPositiveButton(R.string.copy_all, (dialogInterface, i) -> copyText(text)) + .setNegativeButton(R.string.cancel, null) + .show(); + } + + private void copyText(String text) { + ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE); + if (clipboard != null) { + ClipData clip = ClipData.newPlainText("simple text", text); + clipboard.setPrimaryClip(clip); + Toast.makeText(activity, R.string.copy_success, Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(activity, R.string.copy_failed, Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + activity = (Activity) context; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SortTypeBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SortTypeBottomSheetFragment.java index f5ee3696..ba03d93c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SortTypeBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SortTypeBottomSheetFragment.java @@ -109,6 +109,6 @@ public class SortTypeBottomSheetFragment extends RoundedBottomSheetDialogFragmen @Override public void onAttach(@NonNull Context context) { super.onAttach(context); - this.activity = (Activity) context; + activity = (Activity) context; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index 25a0e1cd..435cce53 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -133,6 +133,7 @@ public class ParsePost { } else { String selfTextPlain = Utils.trimTrailingWhitespace( Html.fromHtml(data.getString(JSONUtils.SELFTEXT_HTML_KEY))).toString(); + post.setSelfTextPlain(selfTextPlain); if (selfTextPlain.length() > 250) { selfTextPlain = selfTextPlain.substring(0, 250); } @@ -234,6 +235,7 @@ public class ParsePost { } else { String selfTextPlain = Utils.trimTrailingWhitespace( Html.fromHtml(data.getString(JSONUtils.SELFTEXT_HTML_KEY))).toString(); + post.setSelfTextPlain(selfTextPlain); if (selfTextPlain.length() > 250) { selfTextPlain = selfTextPlain.substring(0, 250); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java index e16893c2..8a025c97 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java @@ -39,6 +39,7 @@ public class Post implements Parcelable { private String postTime; private String title; private String selfText; + private String selfTextPlain; private String selfTextPlainTrimmed; private String previewUrl; private String thumbnailPreviewUrl; @@ -177,6 +178,7 @@ public class Post implements Parcelable { postTimeMillis = in.readLong(); title = in.readString(); selfText = in.readString(); + selfTextPlain = in.readString(); selfTextPlainTrimmed = in.readString(); previewUrl = in.readString(); thumbnailPreviewUrl = in.readString(); @@ -267,6 +269,14 @@ public class Post implements Parcelable { this.selfText = selfText; } + public String getSelfTextPlain() { + return selfTextPlain; + } + + public void setSelfTextPlain(String selfTextPlain) { + this.selfTextPlain = selfTextPlain; + } + public String getSelfTextPlainTrimmed() { return selfTextPlainTrimmed; } @@ -438,6 +448,7 @@ public class Post implements Parcelable { parcel.writeLong(postTimeMillis); parcel.writeString(title); parcel.writeString(selfText); + parcel.writeString(selfTextPlain); parcel.writeString(selfTextPlainTrimmed); parcel.writeString(previewUrl); parcel.writeString(thumbnailPreviewUrl); -- cgit v1.2.3