From 6637cefc051d44802f54e741ca2492cbd36ac405 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 30 Aug 2018 21:18:21 +0800 Subject: Blur the preview image if the post is NSFW. Tweak some layouts. Optimize some methods. --- .../infinityforreddit/PostRecyclerViewAdapter.java | 87 +++++++------------- .../infinityforreddit/ViewPostDetailActivity.java | 95 ++++++++-------------- .../main/res/layout/activity_view_post_detail.xml | 18 ++-- app/src/main/res/layout/item_post.xml | 2 +- 4 files changed, 68 insertions(+), 134 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index ac7a09b4..f2e5d200 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -24,15 +24,18 @@ import android.widget.TextView; import android.widget.Toast; import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.RequestManager; import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; +import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; import java.util.ArrayList; import de.hdodenhof.circleimageview.CircleImageView; +import jp.wasabeef.glide.transformations.BlurTransformation; /** * Created by alex on 2/25/18. @@ -171,6 +174,27 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + ((DataViewHolder) holder).progressBar.setVisibility(View.GONE); + return false; + } + }); + + if(mPostData.get(position).isNSFW()) { + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) + .into(((DataViewHolder) holder).imageView); + } else { + imageRequestBuilder.into(((DataViewHolder) holder).imageView); + } } if(mPostData.get(position).isStickied()) { @@ -181,20 +205,8 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - return false; - } - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - ((DataViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - }).into(((DataViewHolder) holder).imageView); + final String imageUrl = mPostData.get(position).getUrl(); ((DataViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -209,20 +221,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - return false; - } - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - ((DataViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - }).into(((DataViewHolder) holder).imageView); - final String linkUrl = mPostData.get(position).getUrl(); ((DataViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -231,29 +230,14 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - ((DataViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - }).into(((DataViewHolder) holder).imageView); - - String gifVideoUrl = mPostData.get(position).getVideoUrl(); - final Uri gifVideoUri = Uri.parse(gifVideoUrl); + final Uri gifVideoUri = Uri.parse(mPostData.get(position).getVideoUrl()); ((DataViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -273,23 +257,8 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - ((DataViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - }).into(((DataViewHolder) holder).imageView); - - String videoUrl = mPostData.get(position).getVideoUrl(); - final Uri videoUri = Uri.parse(videoUrl); + final Uri videoUri = Uri.parse(mPostData.get(position).getVideoUrl()); ((DataViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 10591902..f922a0d6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -25,9 +25,11 @@ import android.widget.TextView; import android.widget.Toast; import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; +import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; import org.sufficientlysecure.htmltextview.HtmlTextView; @@ -35,6 +37,7 @@ import org.sufficientlysecure.htmltextview.HtmlTextView; import java.util.ArrayList; import de.hdodenhof.circleimageview.CircleImageView; +import jp.wasabeef.glide.transformations.BlurTransformation; public class ViewPostDetailActivity extends AppCompatActivity { @@ -132,6 +135,32 @@ public class ViewPostDetailActivity extends AppCompatActivity { break; } + if(mPostData.getPostType() != PostData.TEXT_TYPE && mPostData.getPostType() != PostData.NO_PREVIEW_LINK_TYPE) { + relativeLayout.setVisibility(View.VISIBLE); + imageView.setVisibility(View.VISIBLE); + + RequestBuilder imageRequestBuilder = Glide.with(this).load(mPostData.getPreviewUrl()).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + //Need to be implemented + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + return false; + } + }); + + if(mPostData.isNSFW()) { + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 3))) + .into(imageView); + } else { + imageRequestBuilder.into(imageView); + } + } + mRecyclerView.setNestedScrollingEnabled(false); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); @@ -166,20 +195,6 @@ public class ViewPostDetailActivity extends AppCompatActivity { switch (mPostData.getPostType()) { case PostData.IMAGE_TYPE: typeTextView.setText("IMAGE"); - relativeLayout.setVisibility(View.VISIBLE); - Glide.with(this).load(mPostData.getPreviewUrl()).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - //Need to be implemented - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - progressBar.setVisibility(View.GONE); - return false; - } - }).into(imageView); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -193,26 +208,12 @@ public class ViewPostDetailActivity extends AppCompatActivity { }); break; case PostData.LINK_TYPE: - relativeLayout.setVisibility(View.VISIBLE); typeTextView.setText("LINK"); if(!mPostData.getSelfText().equals("")) { contentTextView.setVisibility(View.VISIBLE); contentTextView.setHtml(mPostData.getSelfText()); } - String linkPreviewUrl = mPostData.getPreviewUrl(); - Glide.with(this).load(linkPreviewUrl).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - return false; - } - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - progressBar.setVisibility(View.GONE); - return false; - } - }).into(imageView); - final String linkUrl = mPostData.getUrl(); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -221,30 +222,14 @@ public class ViewPostDetailActivity extends AppCompatActivity { builder.addDefaultShareMenuItem(); builder.setToolbarColor(getResources().getColor(R.color.colorPrimary)); CustomTabsIntent customTabsIntent = builder.build(); - customTabsIntent.launchUrl(ViewPostDetailActivity.this, Uri.parse(linkUrl)); + customTabsIntent.launchUrl(ViewPostDetailActivity.this, Uri.parse(mPostData.getUrl())); } }); break; case PostData.GIF_VIDEO_TYPE: - relativeLayout.setVisibility(View.VISIBLE); typeTextView.setText("VIDEO"); - String gifVideoPreviewUrl = mPostData.getPreviewUrl(); - Glide.with(this).load(gifVideoPreviewUrl).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - progressBar.setVisibility(View.GONE); - return false; - } - }).into(imageView); - - String gifVideoUrl = mPostData.getVideoUrl(); - final Uri gifVideoUri = Uri.parse(gifVideoUrl); + final Uri gifVideoUri = Uri.parse(mPostData.getVideoUrl()); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -263,25 +248,9 @@ public class ViewPostDetailActivity extends AppCompatActivity { }); break; case PostData.VIDEO_TYPE: - relativeLayout.setVisibility(View.VISIBLE); typeTextView.setText("VIDEO"); - String videoPreviewUrl = mPostData.getPreviewUrl(); - Glide.with(this).load(videoPreviewUrl).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - progressBar.setVisibility(View.GONE); - return false; - } - }).into(imageView); - - String videoUrl = mPostData.getVideoUrl(); - final Uri videoUri = Uri.parse(videoUrl); + final Uri videoUri = Uri.parse(mPostData.getVideoUrl()); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { diff --git a/app/src/main/res/layout/activity_view_post_detail.xml b/app/src/main/res/layout/activity_view_post_detail.xml index 64a648c2..4e9feffc 100644 --- a/app/src/main/res/layout/activity_view_post_detail.xml +++ b/app/src/main/res/layout/activity_view_post_detail.xml @@ -27,7 +27,7 @@ android:orientation="vertical"> + android:layout_height="wrap_content" + android:layout_marginTop="8dp" + android:layout_marginStart="16dp" + android:layout_marginEnd="16dp"> @@ -130,11 +130,7 @@ android:layout_height="wrap_content" android:text="@string/nsfw" android:layout_alignParentEnd="true" - android:layout_marginEnd="16dp" - android:layout_marginLeft="16dp" - android:layout_marginRight="16dp" - android:layout_marginStart="16dp" - android:layout_marginTop="8dp" + android:layout_marginStart="8dp" android:background="@drawable/nsfw_rounded_corner" android:textColor="@android:color/white" android:visibility="gone" /> diff --git a/app/src/main/res/layout/item_post.xml b/app/src/main/res/layout/item_post.xml index 871125d8..78a55cf2 100644 --- a/app/src/main/res/layout/item_post.xml +++ b/app/src/main/res/layout/item_post.xml @@ -112,7 +112,7 @@ android:layout_height="wrap_content" android:background="@drawable/nsfw_rounded_corner" android:layout_alignParentEnd="true" - android:layout_marginStart="16dp" + android:layout_marginStart="8dp" android:layout_centerVertical="true" android:textColor="@android:color/white" android:visibility="gone"/> -- cgit v1.2.3