From b04ad8956dac3d5d07a389b56e1e22e70bf9ff51 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 6 Jan 2020 17:19:41 +0800 Subject: Sharing and copying post links and media links are available. --- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 35 ++- .../Adapter/PostRecyclerViewAdapter.java | 300 +++++++++++---------- .../infinityforreddit/Fragment/PostFragment.java | 5 +- .../Fragment/PostTypeBottomSheetFragment.java | 18 +- .../Fragment/ShareLinkBottomSheetFragment.java | 142 ++++++++++ 5 files changed, 330 insertions(+), 170 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ShareLinkBottomSheetFragment.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 edaefc75..14dab410 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -1,6 +1,5 @@ package ml.docilealligator.infinityforreddit.Adapter; -import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.graphics.ColorFilter; @@ -76,6 +75,7 @@ import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; import ml.docilealligator.infinityforreddit.CustomView.MarkwonLinearLayoutManager; import ml.docilealligator.infinityforreddit.FetchComment; import ml.docilealligator.infinityforreddit.Fragment.ModifyCommentBottomSheetFragment; +import ml.docilealligator.infinityforreddit.Fragment.ShareLinkBottomSheetFragment; import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.R; @@ -99,7 +99,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { - try { - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("text/plain"); - intent.putExtra(Intent.EXTRA_TEXT, mPost.getPermalink()); - mActivity.startActivity(Intent.createChooser(intent, mActivity.getString(R.string.share))); - } catch (ActivityNotFoundException e) { - Toast.makeText(mActivity, R.string.no_activity_found_for_share, Toast.LENGTH_SHORT).show(); + Bundle bundle = new Bundle(); + bundle.putString(ShareLinkBottomSheetFragment.EXTRA_POST_LINK, mPost.getPermalink()); + if (mPost.getPostType() != Post.TEXT_TYPE) { + bundle.putInt(ShareLinkBottomSheetFragment.EXTRA_MEDIA_TYPE, mPost.getPostType()); + switch (mPost.getPostType()) { + case Post.IMAGE_TYPE: + case Post.GIF_TYPE: + case Post.LINK_TYPE: + case Post.NO_PREVIEW_LINK_TYPE: + bundle.putString(ShareLinkBottomSheetFragment.EXTRA_MEDIA_LINK, mPost.getUrl()); + break; + case Post.VIDEO_TYPE: + bundle.putString(ShareLinkBottomSheetFragment.EXTRA_MEDIA_LINK, mPost.getVideoDownloadUrl()); + break; + } } + mShareLinkBottomSheetFragment.setArguments(bundle); + mShareLinkBottomSheetFragment.show(mActivity.getSupportFragmentManager(), mShareLinkBottomSheetFragment.getTag()); }); mUpvoteButton.setOnClickListener(view -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index ca3d7111..2a4e6fe1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -1,11 +1,10 @@ package ml.docilealligator.infinityforreddit.Adapter; -import android.content.ActivityNotFoundException; -import android.content.Context; import android.content.Intent; import android.graphics.ColorFilter; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -18,6 +17,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintSet; import androidx.core.content.ContextCompat; @@ -54,6 +54,7 @@ import ml.docilealligator.infinityforreddit.AsyncTask.LoadSubredditIconAsyncTask import ml.docilealligator.infinityforreddit.AsyncTask.LoadUserDataAsyncTask; import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToDetailActivity; +import ml.docilealligator.infinityforreddit.Fragment.ShareLinkBottomSheetFragment; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Post.PostDataSource; @@ -86,7 +87,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { if (canStartActivity) { canStartActivity = false; - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); + Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getAuthor()); - mContext.startActivity(intent); + mActivity.startActivity(intent); } }); @@ -199,7 +202,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - if (mContext != null && getItemCount() > 0) { + if (mActivity != null && getItemCount() > 0) { if (iconImageUrl == null || iconImageUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) @@ -232,7 +235,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - if (mContext != null && getItemCount() > 0) { + if (mActivity != null && getItemCount() > 0) { if (iconImageUrl == null || iconImageUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) @@ -267,15 +270,15 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - if (mContext != null && getItemCount() > 0) { + if (mActivity != null && getItemCount() > 0) { if (iconImageUrl == null || iconImageUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) @@ -320,14 +323,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 0) { ((PostViewHolder) holder).gildedNumberTextView.setVisibility(View.VISIBLE); - String gildedNumber = mContext.getResources().getString(R.string.gilded_count, gilded); + String gildedNumber = mActivity.getResources().getString(R.string.gilded_count, gilded); ((PostViewHolder) holder).gildedNumberTextView.setText(gildedNumber); } @@ -357,13 +360,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, FilteredThingActivity.class); + Intent intent = new Intent(mActivity, FilteredThingActivity.class); intent.putExtra(FilteredThingActivity.EXTRA_NAME, post.getSubredditNamePrefixed().substring(2)); intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); intent.putExtra(FilteredThingActivity.EXTRA_FILTER, Post.NSFW_TYPE); - mContext.startActivity(intent); + mActivity.startActivity(intent); }); } ((PostViewHolder) holder).nsfwTextView.setVisibility(View.VISIBLE); @@ -381,13 +384,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter mCallback.typeChipClicked(post.getPostType())); } @@ -428,11 +431,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, ViewImageActivity.class); + Intent intent = new Intent(mActivity, ViewImageActivity.class); intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, imageUrl); intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + "-" + id + ".jpg"); - mContext.startActivity(intent); + mActivity.startActivity(intent); }); break; case Post.LINK_TYPE: @@ -443,14 +446,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, LinkResolverActivity.class); + Intent intent = new Intent(mActivity, LinkResolverActivity.class); Uri uri = Uri.parse(post.getUrl()); if (uri.getScheme() == null && uri.getHost() == null) { intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl())); } else { intent.setData(uri); } - mContext.startActivity(intent); + mActivity.startActivity(intent); }); break; case Post.GIF_TYPE: @@ -458,12 +461,12 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, ViewGIFActivity.class); + Intent intent = new Intent(mActivity, ViewGIFActivity.class); intent.setData(gifVideoUri); intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, subredditName + "-" + id + ".gif"); intent.putExtra(ViewGIFActivity.IMAGE_URL_KEY, post.getVideoUrl()); - mContext.startActivity(intent); + mActivity.startActivity(intent); }); ((PostViewHolder) holder).playButtonImageView.setVisibility(View.VISIBLE); @@ -473,12 +476,12 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, ViewVideoActivity.class); + Intent intent = new Intent(mActivity, ViewVideoActivity.class); intent.setData(videoUri); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, subredditName); intent.putExtra(ViewVideoActivity.EXTRA_ID, fullName); - mContext.startActivity(intent); + mActivity.startActivity(intent); }); ((PostViewHolder) holder).playButtonImageView.setVisibility(View.VISIBLE); @@ -492,14 +495,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, LinkResolverActivity.class); + Intent intent = new Intent(mActivity, LinkResolverActivity.class); Uri uri = Uri.parse(post.getUrl()); if (uri.getScheme() == null && uri.getHost() == null) { intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl())); } else { intent.setData(uri); } - mContext.startActivity(intent); + mActivity.startActivity(intent); }); break; case Post.TEXT_TYPE: @@ -513,12 +516,12 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { if (mAccessToken == null) { - Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); return; } if (isArchived) { - Toast.makeText(mContext, R.string.archived_post_vote_unavailable, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.archived_post_vote_unavailable, Toast.LENGTH_SHORT).show(); return; } @@ -536,14 +539,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { if (mAccessToken == null) { - Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); return; } if (isArchived) { - Toast.makeText(mContext, R.string.archived_post_vote_unavailable, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.archived_post_vote_unavailable, Toast.LENGTH_SHORT).show(); return; } @@ -607,14 +610,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { if (mAccessToken == null) { - Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); return; } @@ -675,7 +678,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - try { - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("text/plain"); - intent.putExtra(Intent.EXTRA_TEXT, permalink); - mContext.startActivity(Intent.createChooser(intent, mContext.getString(R.string.share))); - } catch (ActivityNotFoundException e) { - Toast.makeText(mContext, R.string.no_activity_found_for_share, Toast.LENGTH_SHORT).show(); - } - }); + ((PostViewHolder) holder).shareButton.setOnClickListener(view -> shareLink(post)); } } else if (holder instanceof PostCompactViewHolder) { Post post = getItem(position); @@ -743,10 +737,10 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - if (mContext != null && getItemCount() > 0) { + if (mActivity != null && getItemCount() > 0) { if (iconImageUrl == null || iconImageUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) @@ -787,7 +781,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - if (mContext != null && getItemCount() > 0) { + if (mActivity != null && getItemCount() > 0) { if (iconImageUrl == null || iconImageUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) @@ -818,22 +812,22 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { if (canStartActivity) { canStartActivity = false; if (post.getSubredditNamePrefixed().startsWith("u/")) { - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); + Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getSubredditNamePrefixed().substring(2)); - mContext.startActivity(intent); + mActivity.startActivity(intent); } else { - Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); + Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, post.getSubredditNamePrefixed().substring(2)); - mContext.startActivity(intent); + mActivity.startActivity(intent); } } }); @@ -844,7 +838,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - if (mContext != null && getItemCount() > 0) { + if (mActivity != null && getItemCount() > 0) { if (iconImageUrl == null || iconImageUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) @@ -874,15 +868,15 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { if (canStartActivity) { canStartActivity = false; - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); + Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getAuthor()); - mContext.startActivity(intent); + mActivity.startActivity(intent); } }); @@ -892,7 +886,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 0) { ((PostCompactViewHolder) holder).gildedNumberTextView.setVisibility(View.VISIBLE); - String gildedNumber = mContext.getResources().getString(R.string.gilded_count, gilded); + String gildedNumber = mActivity.getResources().getString(R.string.gilded_count, gilded); ((PostCompactViewHolder) holder).gildedNumberTextView.setText(gildedNumber); } @@ -911,13 +905,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, FilteredThingActivity.class); + Intent intent = new Intent(mActivity, FilteredThingActivity.class); intent.putExtra(FilteredThingActivity.EXTRA_NAME, post.getSubredditNamePrefixed().substring(2)); intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); intent.putExtra(FilteredThingActivity.EXTRA_FILTER, Post.NSFW_TYPE); - mContext.startActivity(intent); + mActivity.startActivity(intent); }); } ((PostCompactViewHolder) holder).nsfwTextView.setVisibility(View.VISIBLE); @@ -935,13 +929,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter mCallback.typeChipClicked(post.getPostType())); } @@ -982,11 +976,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, ViewImageActivity.class); + Intent intent = new Intent(mActivity, ViewImageActivity.class); intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, imageUrl); intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + "-" + id + ".jpg"); - mContext.startActivity(intent); + mActivity.startActivity(intent); }); break; case Post.LINK_TYPE: @@ -997,14 +991,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, LinkResolverActivity.class); + Intent intent = new Intent(mActivity, LinkResolverActivity.class); Uri uri = Uri.parse(post.getUrl()); if (uri.getScheme() == null && uri.getHost() == null) { intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl())); } else { intent.setData(uri); } - mContext.startActivity(intent); + mActivity.startActivity(intent); }); break; case Post.GIF_TYPE: @@ -1012,12 +1006,12 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, ViewGIFActivity.class); + Intent intent = new Intent(mActivity, ViewGIFActivity.class); intent.setData(gifVideoUri); intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, subredditName + "-" + id + ".gif"); intent.putExtra(ViewGIFActivity.IMAGE_URL_KEY, post.getVideoUrl()); - mContext.startActivity(intent); + mActivity.startActivity(intent); }); ((PostCompactViewHolder) holder).playButtonImageView.setVisibility(View.VISIBLE); @@ -1027,12 +1021,12 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, ViewVideoActivity.class); + Intent intent = new Intent(mActivity, ViewVideoActivity.class); intent.setData(videoUri); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, subredditName); intent.putExtra(ViewVideoActivity.EXTRA_ID, fullName); - mContext.startActivity(intent); + mActivity.startActivity(intent); }); ((PostCompactViewHolder) holder).playButtonImageView.setVisibility(View.VISIBLE); @@ -1046,14 +1040,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, LinkResolverActivity.class); + Intent intent = new Intent(mActivity, LinkResolverActivity.class); Uri uri = Uri.parse(post.getUrl()); if (uri.getScheme() == null && uri.getHost() == null) { intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl())); } else { intent.setData(uri); } - mContext.startActivity(intent); + mActivity.startActivity(intent); }); break; case Post.TEXT_TYPE: @@ -1063,12 +1057,12 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { if (mAccessToken == null) { - Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); return; } if (isArchived) { - Toast.makeText(mContext, R.string.archived_post_vote_unavailable, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.archived_post_vote_unavailable, Toast.LENGTH_SHORT).show(); return; } @@ -1086,14 +1080,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { if (mAccessToken == null) { - Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); return; } if (isArchived) { - Toast.makeText(mContext, R.string.archived_post_vote_unavailable, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.archived_post_vote_unavailable, Toast.LENGTH_SHORT).show(); return; } @@ -1157,14 +1151,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { if (mAccessToken == null) { - Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); + Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); return; } @@ -1225,7 +1219,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - try { - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("text/plain"); - intent.putExtra(Intent.EXTRA_TEXT, permalink); - mContext.startActivity(Intent.createChooser(intent, mContext.getString(R.string.share))); - } catch (ActivityNotFoundException e) { - Toast.makeText(mContext, R.string.no_activity_found_for_share, Toast.LENGTH_SHORT).show(); - } - }); + ((PostCompactViewHolder) holder).shareButton.setOnClickListener(view -> shareLink(post)); } } } @@ -1329,6 +1314,27 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + textTypeTextView.setOnClickListener(view -> { ((PostTypeSelectionCallback) activity).postTypeSelected(TYPE_TEXT); dismiss(); }); - linkTypeLinearLayout.setOnClickListener(view -> { + linkTypeTextView.setOnClickListener(view -> { ((PostTypeSelectionCallback) activity).postTypeSelected(TYPE_LINK); dismiss(); }); - imageTypeLinearLayout.setOnClickListener(view -> { + imageTypeTextView.setOnClickListener(view -> { ((PostTypeSelectionCallback) activity).postTypeSelected(TYPE_IMAGE); dismiss(); }); - videoTypeLinearLayout.setOnClickListener(view -> { + videoTypeTextView.setOnClickListener(view -> { ((PostTypeSelectionCallback) activity).postTypeSelected(TYPE_VIDEO); dismiss(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ShareLinkBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ShareLinkBottomSheetFragment.java new file mode 100644 index 00000000..0447c7ba --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ShareLinkBottomSheetFragment.java @@ -0,0 +1,142 @@ +package ml.docilealligator.infinityforreddit.Fragment; + + +import android.app.Activity; +import android.content.ActivityNotFoundException; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; +import android.content.Intent; +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.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; + +import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.Post.Post; +import ml.docilealligator.infinityforreddit.R; + +/** + * A simple {@link Fragment} subclass. + */ +public class ShareLinkBottomSheetFragment extends RoundedBottomSheetDialogFragment { + public static final String EXTRA_POST_LINK = "EPL"; + public static final String EXTRA_MEDIA_LINK = "EML"; + public static final String EXTRA_MEDIA_TYPE = "EMT"; + + @BindView(R.id.share_post_link_text_view_share_link_bottom_sheet_fragment) + TextView sharePostLinkTextView; + @BindView(R.id.share_media_link_text_view_share_link_bottom_sheet_fragment) + TextView shareMediaLinkTextView; + @BindView(R.id.copy_post_link_text_view_share_link_bottom_sheet_fragment) + TextView copyPostLinkTextView; + @BindView(R.id.copy_media_link_text_view_share_link_bottom_sheet_fragment) + TextView copyMediaLinkTextView; + + private Activity activity; + + public ShareLinkBottomSheetFragment() { + // 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_share_link_bottom_sheet, container, false); + + ButterKnife.bind(this, rootView); + + String postLink = getArguments().getString(EXTRA_POST_LINK); + String mediaLink = getArguments().containsKey(EXTRA_MEDIA_LINK) ? getArguments().getString(EXTRA_MEDIA_LINK) : null; + + if (mediaLink != null) { + shareMediaLinkTextView.setVisibility(View.VISIBLE); + copyMediaLinkTextView.setVisibility(View.VISIBLE); + + int mediaType = getArguments().getInt(EXTRA_MEDIA_TYPE); + switch (mediaType) { + case Post.IMAGE_TYPE: + shareMediaLinkTextView.setText(R.string.share_image_link); + copyMediaLinkTextView.setText(R.string.copy_image_link); + shareMediaLinkTextView.setCompoundDrawablesWithIntrinsicBounds( + activity.getDrawable(R.drawable.ic_image_24dp), null, null, null); + break; + case Post.GIF_TYPE: + shareMediaLinkTextView.setText(R.string.share_gif_link); + copyMediaLinkTextView.setText(R.string.copy_gif_link); + shareMediaLinkTextView.setCompoundDrawablesWithIntrinsicBounds( + activity.getDrawable(R.drawable.ic_image_24dp), null, null, null); + break; + case Post.VIDEO_TYPE: + shareMediaLinkTextView.setText(R.string.share_video_link); + copyMediaLinkTextView.setText(R.string.copy_video_link); + shareMediaLinkTextView.setCompoundDrawablesWithIntrinsicBounds( + activity.getDrawable(R.drawable.ic_outline_video_24dp), null, null, null); + break; + case Post.LINK_TYPE: + case Post.NO_PREVIEW_LINK_TYPE: + shareMediaLinkTextView.setText(R.string.share_link); + copyMediaLinkTextView.setText(R.string.copy_link); + break; + } + + shareMediaLinkTextView.setOnClickListener(view -> { + shareLink(mediaLink); + dismiss(); + }); + copyMediaLinkTextView.setOnClickListener(view -> { + copyLink(mediaLink); + dismiss(); + }); + } + + sharePostLinkTextView.setOnClickListener(view -> { + shareLink(postLink); + dismiss(); + }); + copyPostLinkTextView.setOnClickListener(view -> { + copyLink(postLink); + dismiss(); + }); + return rootView; + } + + private void shareLink(String link) { + try { + Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("text/plain"); + intent.putExtra(Intent.EXTRA_TEXT, link); + activity.startActivity(Intent.createChooser(intent, getString(R.string.share))); + } catch (ActivityNotFoundException e) { + Toast.makeText(activity, R.string.no_activity_found_for_share, Toast.LENGTH_SHORT).show(); + } + } + + 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); + 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); + this.activity = (AppCompatActivity) context; + } +} -- cgit v1.2.3