diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-01-29 04:43:57 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-01-29 04:43:57 +0000 |
commit | 5e401a6a96f4b83a79685907a6bee4997ff7e183 (patch) | |
tree | 57d17c810914cd7afcdce33a10e70d8ce6314284 /app/src/main/java/ml | |
parent | cb9154369070fe3773c16da0d4e7b3c33b716a1f (diff) | |
download | infinity-for-reddit-5e401a6a96f4b83a79685907a6bee4997ff7e183.tar infinity-for-reddit-5e401a6a96f4b83a79685907a6bee4997ff7e183.tar.gz infinity-for-reddit-5e401a6a96f4b83a79685907a6bee4997ff7e183.tar.bz2 infinity-for-reddit-5e401a6a96f4b83a79685907a6bee4997ff7e183.tar.lz infinity-for-reddit-5e401a6a96f4b83a79685907a6bee4997ff7e183.tar.xz infinity-for-reddit-5e401a6a96f4b83a79685907a6bee4997ff7e183.tar.zst infinity-for-reddit-5e401a6a96f4b83a79685907a6bee4997ff7e183.zip |
Copy post title and content in ViewPostDetailActivity.
Diffstat (limited to 'app/src/main/java/ml')
5 files changed, 135 insertions, 1 deletions
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<Recy private boolean loadMoreCommentsFailed; private int mCommentBackgroundColor; private ShareLinkBottomSheetFragment mShareLinkBottomSheetFragment; + private CopyTextBottomSheetFragment mCopyTextBottomSheetFragment; public CommentAndPostRecyclerViewAdapter(AppCompatActivity activity, Retrofit retrofit, Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, RequestManager glide, @@ -145,6 +147,14 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy @Override public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) { textView.setTextColor(markdownColor); + textView.setOnLongClickListener(view -> { + 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<Recy mCommentBackgroundColor = typedValue.data; mShareLinkBottomSheetFragment = new ShareLinkBottomSheetFragment(); + mCopyTextBottomSheetFragment = new CopyTextBottomSheetFragment(); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CopyTextBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CopyTextBottomSheetFragment.java new file mode 100644 index 00000000..96bb671d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/CopyTextBottomSheetFragment.java @@ -0,0 +1,110 @@ +package ml.docilealligator.infinityforreddit.Fragment; + + +import android.app.Activity; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; + +import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.R; + +/** + * A simple {@link Fragment} subclass. + */ +public class CopyTextBottomSheetFragment extends RoundedBottomSheetDialogFragment { + public static final String EXTRA_RAW_TEXT = "ERT"; + public static final String EXTRA_MARKDOWN = "EM"; + + @BindView(R.id.copy_raw_text_text_view_copy_text_bottom_sheet_fragment) + TextView copyRawTextTextView; + @BindView(R.id.copy_markdown_text_view_copy_text_bottom_sheet_fragment) + TextView copyMarkdownTextView; + @BindView(R.id.copy_all_raw_text_text_view_copy_text_bottom_sheet_fragment) + TextView copyAllRawTextTextView; + @BindView(R.id.copy_all_markdown_text_view_copy_text_bottom_sheet_fragment) + TextView copyAllMarkdownTextView; + + private Activity activity; + + public CopyTextBottomSheetFragment() { + // Required empty public constructor + } + + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View rootView = inflater.inflate(R.layout.fragment_copy_text_bottom_sheet, container, false); + ButterKnife.bind(this, rootView); + + String rawText = getArguments().getString(EXTRA_RAW_TEXT); + String markdownText = getArguments().getString(EXTRA_MARKDOWN); + + copyRawTextTextView.setOnClickListener(view -> { + 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); |