Copy post title and content in ViewPostDetailActivity.

This commit is contained in:
Alex Ning 2020-01-29 12:43:57 +08:00
parent cb91543690
commit 5e401a6a96
10 changed files with 248 additions and 2 deletions

View File

@ -74,6 +74,7 @@ import ml.docilealligator.infinityforreddit.CommentData;
import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView;
import ml.docilealligator.infinityforreddit.CustomView.MarkwonLinearLayoutManager; import ml.docilealligator.infinityforreddit.CustomView.MarkwonLinearLayoutManager;
import ml.docilealligator.infinityforreddit.FetchComment; import ml.docilealligator.infinityforreddit.FetchComment;
import ml.docilealligator.infinityforreddit.Fragment.CopyTextBottomSheetFragment;
import ml.docilealligator.infinityforreddit.Fragment.ModifyCommentBottomSheetFragment; import ml.docilealligator.infinityforreddit.Fragment.ModifyCommentBottomSheetFragment;
import ml.docilealligator.infinityforreddit.Fragment.ShareLinkBottomSheetFragment; import ml.docilealligator.infinityforreddit.Fragment.ShareLinkBottomSheetFragment;
import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Post.Post;
@ -126,6 +127,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
private boolean loadMoreCommentsFailed; private boolean loadMoreCommentsFailed;
private int mCommentBackgroundColor; private int mCommentBackgroundColor;
private ShareLinkBottomSheetFragment mShareLinkBottomSheetFragment; private ShareLinkBottomSheetFragment mShareLinkBottomSheetFragment;
private CopyTextBottomSheetFragment mCopyTextBottomSheetFragment;
public CommentAndPostRecyclerViewAdapter(AppCompatActivity activity, Retrofit retrofit, Retrofit oauthRetrofit, public CommentAndPostRecyclerViewAdapter(AppCompatActivity activity, Retrofit retrofit, Retrofit oauthRetrofit,
RedditDataRoomDatabase redditDataRoomDatabase, RequestManager glide, RedditDataRoomDatabase redditDataRoomDatabase, RequestManager glide,
@ -145,6 +147,14 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
@Override @Override
public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) { public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) {
textView.setTextColor(markdownColor); 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 @Override
@ -225,6 +235,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
mCommentBackgroundColor = typedValue.data; mCommentBackgroundColor = typedValue.data;
mShareLinkBottomSheetFragment = new ShareLinkBottomSheetFragment(); mShareLinkBottomSheetFragment = new ShareLinkBottomSheetFragment();
mCopyTextBottomSheetFragment = new CopyTextBottomSheetFragment();
} }
@Override @Override

View File

@ -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;
}
}

View File

@ -109,6 +109,6 @@ public class SortTypeBottomSheetFragment extends RoundedBottomSheetDialogFragmen
@Override @Override
public void onAttach(@NonNull Context context) { public void onAttach(@NonNull Context context) {
super.onAttach(context); super.onAttach(context);
this.activity = (Activity) context; activity = (Activity) context;
} }
} }

View File

@ -133,6 +133,7 @@ public class ParsePost {
} else { } else {
String selfTextPlain = Utils.trimTrailingWhitespace( String selfTextPlain = Utils.trimTrailingWhitespace(
Html.fromHtml(data.getString(JSONUtils.SELFTEXT_HTML_KEY))).toString(); Html.fromHtml(data.getString(JSONUtils.SELFTEXT_HTML_KEY))).toString();
post.setSelfTextPlain(selfTextPlain);
if (selfTextPlain.length() > 250) { if (selfTextPlain.length() > 250) {
selfTextPlain = selfTextPlain.substring(0, 250); selfTextPlain = selfTextPlain.substring(0, 250);
} }
@ -234,6 +235,7 @@ public class ParsePost {
} else { } else {
String selfTextPlain = Utils.trimTrailingWhitespace( String selfTextPlain = Utils.trimTrailingWhitespace(
Html.fromHtml(data.getString(JSONUtils.SELFTEXT_HTML_KEY))).toString(); Html.fromHtml(data.getString(JSONUtils.SELFTEXT_HTML_KEY))).toString();
post.setSelfTextPlain(selfTextPlain);
if (selfTextPlain.length() > 250) { if (selfTextPlain.length() > 250) {
selfTextPlain = selfTextPlain.substring(0, 250); selfTextPlain = selfTextPlain.substring(0, 250);
} }

View File

@ -39,6 +39,7 @@ public class Post implements Parcelable {
private String postTime; private String postTime;
private String title; private String title;
private String selfText; private String selfText;
private String selfTextPlain;
private String selfTextPlainTrimmed; private String selfTextPlainTrimmed;
private String previewUrl; private String previewUrl;
private String thumbnailPreviewUrl; private String thumbnailPreviewUrl;
@ -177,6 +178,7 @@ public class Post implements Parcelable {
postTimeMillis = in.readLong(); postTimeMillis = in.readLong();
title = in.readString(); title = in.readString();
selfText = in.readString(); selfText = in.readString();
selfTextPlain = in.readString();
selfTextPlainTrimmed = in.readString(); selfTextPlainTrimmed = in.readString();
previewUrl = in.readString(); previewUrl = in.readString();
thumbnailPreviewUrl = in.readString(); thumbnailPreviewUrl = in.readString();
@ -267,6 +269,14 @@ public class Post implements Parcelable {
this.selfText = selfText; this.selfText = selfText;
} }
public String getSelfTextPlain() {
return selfTextPlain;
}
public void setSelfTextPlain(String selfTextPlain) {
this.selfTextPlain = selfTextPlain;
}
public String getSelfTextPlainTrimmed() { public String getSelfTextPlainTrimmed() {
return selfTextPlainTrimmed; return selfTextPlainTrimmed;
} }
@ -438,6 +448,7 @@ public class Post implements Parcelable {
parcel.writeLong(postTimeMillis); parcel.writeLong(postTimeMillis);
parcel.writeString(title); parcel.writeString(title);
parcel.writeString(selfText); parcel.writeString(selfText);
parcel.writeString(selfTextPlain);
parcel.writeString(selfTextPlainTrimmed); parcel.writeString(selfTextPlainTrimmed);
parcel.writeString(previewUrl); parcel.writeString(previewUrl);
parcel.writeString(thumbnailPreviewUrl); parcel.writeString(thumbnailPreviewUrl);

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_view_copy_text_material_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="24dp"
android:paddingEnd="24dp"
android:textIsSelectable="true"
android:enabled="true"
android:focusable="true"
android:longClickable="true" />
</ScrollView>

View File

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:overScrollMode="never"
tools:context=".Fragment.SortTimeBottomSheetFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/copy_raw_text_text_view_copy_text_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/copy_raw_text"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_default"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground" />
<TextView
android:id="@+id/copy_markdown_text_view_copy_text_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/copy_markdown"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_default"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground" />
<TextView
android:id="@+id/copy_all_raw_text_text_view_copy_text_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/copy_all_raw_text"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_default"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground" />
<TextView
android:id="@+id/copy_all_markdown_text_view_copy_text_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/copy_all_markdown"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_default"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>

View File

@ -79,7 +79,11 @@
android:paddingStart="16dp" android:paddingStart="16dp"
android:paddingEnd="16dp" android:paddingEnd="16dp"
android:textColor="@color/primaryTextColor" android:textColor="@color/primaryTextColor"
android:textSize="?attr/title_font_18" /> android:textSize="?attr/title_font_18"
android:textIsSelectable="true"
android:enabled="true"
android:focusable="true"
android:longClickable="true" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/content_markdown_view_item_post_detail" android:id="@+id/content_markdown_view_item_post_detail"

View File

@ -405,4 +405,10 @@
<string name="copy_success">Copied</string> <string name="copy_success">Copied</string>
<string name="copy_failed">Cannot copy the link</string> <string name="copy_failed">Cannot copy the link</string>
<string name="copy_text">Copy</string>
<string name="copy_all">Copy All</string>
<string name="copy_markdown">Copy Markdown</string>
<string name="copy_raw_text">Copy Raw Text</string>
<string name="copy_all_markdown">Copy All Markdown</string>
<string name="copy_all_raw_text">Copy All Raw Text</string>
</resources> </resources>

View File

@ -58,6 +58,12 @@
<item name="buttonBarNegativeButtonStyle">@style/MaterialAlertDialogNegativeButtonStyle</item> <item name="buttonBarNegativeButtonStyle">@style/MaterialAlertDialogNegativeButtonStyle</item>
</style> </style>
<style name="CopyTextMaterialAlertDialogTheme">
<item name="android:textSize">?attr/font_default</item>
<item name="materialAlertDialogTitleTextStyle">@style/MaterialAlertDialogTitleTextStyle</item>
<item name="buttonBarNegativeButtonStyle">@style/MaterialAlertDialogNegativeButtonStyle</item>
</style>
<style name="MaterialAlertDialogPositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog"> <style name="MaterialAlertDialogPositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/colorAccent</item> <item name="android:textColor">@color/colorAccent</item>
</style> </style>