Blur the preview image if the post is NSFW. Tweak some layouts. Optimize some methods.

This commit is contained in:
Alex Ning 2018-08-30 21:18:21 +08:00
parent 4364e20e36
commit 6637cefc05
6 changed files with 71 additions and 136 deletions

View File

@ -43,9 +43,9 @@ dependencies {
implementation 'com.android.support:customtabs:28.0.0-rc01'
implementation 'com.alexvasilkov:gesture-views:2.5.2'
implementation 'com.android.support:cardview-v7:28.0.0-rc01'
implementation 'com.github.bumptech.glide:glide:4.6.1'
implementation 'com.github.bumptech.glide:glide:4.7.1'
implementation 'com.github.pwittchen:swipe-rx2:0.3.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
// Room components
implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
@ -58,4 +58,5 @@ dependencies {
implementation 'org.sufficientlysecure:html-textview:3.6'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.4.0'
implementation 'jp.wasabeef:glide-transformations:3.3.0'
}

View File

@ -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<RecyclerView.ViewHold
((DataViewHolder) holder).relativeLayout.setVisibility(View.VISIBLE);
((DataViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
((DataViewHolder) holder).imageView.setVisibility(View.VISIBLE);
String previewUrl = mPostData.get(position).getPreviewUrl();
RequestBuilder imageRequestBuilder = glide.load(previewUrl).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> 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<RecyclerView.ViewHold
switch (mPostData.get(position).getPostType()) {
case PostData.IMAGE_TYPE:
((DataViewHolder) holder).typeTextView.setText("IMAGE");
final String previewImageUrl = mPostData.get(position).getPreviewUrl();
final String imageUrl = mPostData.get(position).getUrl();
glide.load(previewImageUrl).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> 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<RecyclerView.ViewHold
break;
case PostData.LINK_TYPE:
((DataViewHolder) holder).typeTextView.setText("LINK");
String linkPreviewUrl = mPostData.get(position).getPreviewUrl();
glide.load(linkPreviewUrl).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> 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<RecyclerView.ViewHold
builder.addDefaultShareMenuItem();
builder.setToolbarColor(mContext.getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(mContext, Uri.parse(linkUrl));
customTabsIntent.launchUrl(mContext, Uri.parse(mPostData.get(position).getUrl()));
}
});
break;
case PostData.GIF_VIDEO_TYPE:
((DataViewHolder) holder).typeTextView.setText("GIF");
String gifVideoPreviewUrl = mPostData.get(position).getPreviewUrl();
glide.load(gifVideoPreviewUrl).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> 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<RecyclerView.ViewHold
break;
case PostData.VIDEO_TYPE:
((DataViewHolder) holder).typeTextView.setText("VIDEO");
String videoPreviewUrl = mPostData.get(position).getPreviewUrl();
glide.load(videoPreviewUrl).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> 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) {

View File

@ -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<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//Need to be implemented
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> 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<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//Need to be implemented
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> 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<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> 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<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> 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<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> 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) {

View File

@ -27,7 +27,7 @@
android:orientation="vertical">
<RelativeLayout
android:id="@+id/relative_view_view_post_detail"
android:id="@+id/relative_layout_view_post_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
@ -89,17 +89,17 @@
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp">
<TextView
android:id="@+id/type_text_view_view_post_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_centerVertical="true"
android:background="@drawable/rounded_corner"
android:textColor="@android:color/white" />
@ -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" />

View File

@ -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"/>