From 1bd9ba61c6cccd1fc318f1698fddc0d13e4723cf Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 28 Sep 2018 14:49:49 +0800 Subject: Change the layout of post. Add an error view when loading the preview image fails. --- .../infinityforreddit/PostRecyclerViewAdapter.java | 70 +++++++++++++--------- .../infinityforreddit/ViewPostDetailActivity.java | 64 ++++++++++++++++---- 2 files changed, 93 insertions(+), 41 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index aeaa3ae0..d9b6c146 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -9,7 +9,6 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.customtabs.CustomTabsIntent; import android.support.v4.content.ContextCompat; -import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; @@ -74,8 +73,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; - } - }); - - if(mPostData.get(position).isNSFW()) { - imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) - .into(((DataViewHolder) holder).imageView); - } else { - imageRequestBuilder.into(((DataViewHolder) holder).imageView); - } + loadImage(holder, mPostData.get(holder.getAdapterPosition())); } if(mPostData.get(position).isStickied()) { @@ -482,6 +461,38 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + ((DataViewHolder) holder).progressBar.setVisibility(View.GONE); + ((DataViewHolder) holder).errorLinearLayout.setVisibility(View.VISIBLE); + ((DataViewHolder)holder).errorLinearLayout.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + ((DataViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((DataViewHolder) holder).errorLinearLayout.setVisibility(View.GONE); + loadImage(holder, postData); + } + }); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + ((DataViewHolder) holder).errorLinearLayout.setVisibility(View.GONE); + ((DataViewHolder) holder).progressBar.setVisibility(View.GONE); + return false; + } + }); + + if(postData.isNSFW()) { + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) + .into(((DataViewHolder) holder).imageView); + } else { + imageRequestBuilder.into(((DataViewHolder) holder).imageView); + } + } @Override public int getItemCount() { if(mPostData == null || mPostData.isEmpty()) { @@ -496,7 +507,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { + mImageView.setVisibility(View.VISIBLE); + loadImage(); + /*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 + progressBar.setVisibility(View.GONE); return false; } @@ -169,10 +175,10 @@ public class ViewPostDetailActivity extends AppCompatActivity { if(mPostData.isNSFW()) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 3))) - .into(imageView); + .into(mImageView); } else { - imageRequestBuilder.into(imageView); - } + imageRequestBuilder.into(mImageView); + }*/ } mRecyclerView.setNestedScrollingEnabled(false); @@ -222,7 +228,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { switch (mPostData.getPostType()) { case PostData.IMAGE_TYPE: typeTextView.setText("IMAGE"); - imageView.setOnClickListener(new View.OnClickListener() { + mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(ViewPostDetailActivity.this, ViewImageActivity.class); @@ -241,7 +247,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { contentTextView.setHtml(mPostData.getSelfText()); } - imageView.setOnClickListener(new View.OnClickListener() { + mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); @@ -257,7 +263,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { typeTextView.setText("VIDEO"); final Uri gifVideoUri = Uri.parse(mPostData.getVideoUrl()); - imageView.setOnClickListener(new View.OnClickListener() { + mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(ViewPostDetailActivity.this, ViewVideoActivity.class); @@ -278,7 +284,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { typeTextView.setText("VIDEO"); final Uri videoUri = Uri.parse(mPostData.getVideoUrl()); - imageView.setOnClickListener(new View.OnClickListener() { + mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(ViewPostDetailActivity.this, ViewVideoActivity.class); @@ -524,6 +530,38 @@ public class ViewPostDetailActivity extends AppCompatActivity { }); } + private void loadImage() { + RequestBuilder imageRequestBuilder = Glide.with(this).load(mPostData.getPreviewUrl()).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + mLoadImageProgressBar.setVisibility(View.GONE); + mLoadImageErrorTextView.setVisibility(View.VISIBLE); + mLoadImageErrorTextView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + mLoadImageProgressBar.setVisibility(View.VISIBLE); + mLoadImageErrorTextView.setVisibility(View.GONE); + loadImage(); + } + }); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + mLoadWrapper.setVisibility(View.GONE); + return false; + } + }); + + if(mPostData.isNSFW()) { + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 3))) + .into(mImageView); + } else { + imageRequestBuilder.into(mImageView); + } + } + private void showRetrySnackbar() { Snackbar snackbar = Snackbar.make(mCoordinatorLayout, R.string.load_comment_failed, Snackbar.LENGTH_INDEFINITE); snackbar.setAction(R.string.retry, new View.OnClickListener() { -- cgit v1.2.3