Display the subreddit icon in ViewPostDetailActivity. Fixed a bug when the subreddit had no icon, the default icon was not shown. Save the subreddit icon url to BestPostData. Having done saving the subreddit icon url to the instance of BestPostData, skip loading the icon from the database using AsyncTask and load the icon from the instance of BestPostData instead.

This commit is contained in:
Alex Ning 2018-08-10 10:24:36 +08:00
parent e44a172db0
commit 362634d1c7
4 changed files with 105 additions and 16 deletions

View File

@ -18,6 +18,7 @@ class BestPostData implements Parcelable {
private String id;
private String fullName;
private String subredditName;
private String subredditIconUrl;
private String postTime;
private String title;
private String selfText;
@ -80,6 +81,7 @@ class BestPostData implements Parcelable {
id = in.readString();
fullName = in.readString();
subredditName = in.readString();
subredditIconUrl = in.readString();
postTime = in.readString();
title = in.readString();
selfText = in.readString();
@ -120,6 +122,14 @@ class BestPostData implements Parcelable {
return subredditName;
}
public String getSubredditIconUrl() {
return subredditIconUrl;
}
public void setSubredditIconUrl(String subredditIconUrl) {
this.subredditIconUrl = subredditIconUrl;
}
public String getPostTime() {
return postTime;
}
@ -214,6 +224,7 @@ class BestPostData implements Parcelable {
parcel.writeString(id);
parcel.writeString(fullName);
parcel.writeString(subredditName);
parcel.writeString(subredditIconUrl);
parcel.writeString(postTime);
parcel.writeString(title);
parcel.writeString(selfText);

View File

@ -4,7 +4,6 @@ import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.customtabs.CustomTabsIntent;
@ -99,8 +98,14 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
int voteType = mBestPostData.get(position).getVoteType();
boolean nsfw = mBestPostData.get(position).getNSFW();
new LoadSubredditIconAsyncTask(mContext, ((DataViewHolder) holder).subredditImageView,
subredditDao, subredditName).execute();
if(mBestPostData.get(position).getSubredditIconUrl() == null) {
new LoadSubredditIconAsyncTask(mContext, ((DataViewHolder) holder).subredditIconCircleImageView,
subredditDao, subredditName, mBestPostData.get(position)).execute();
} else if(!mBestPostData.get(position).getSubredditIconUrl().equals("")) {
glide.load(mBestPostData.get(position).getSubredditIconUrl()).into(((DataViewHolder) holder).subredditIconCircleImageView);
} else {
glide.load(R.drawable.subreddit_default_icon).into(((DataViewHolder) holder).subredditIconCircleImageView);
}
((DataViewHolder) holder).cardView.setOnClickListener(new View.OnClickListener() {
@Override
@ -460,7 +465,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
class DataViewHolder extends RecyclerView.ViewHolder {
private CardView cardView;
private CircleImageView subredditImageView;
private CircleImageView subredditIconCircleImageView;
private TextView subredditNameTextView;
private TextView postTimeTextView;
private TextView titleTextView;
@ -478,7 +483,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
DataViewHolder(CardView itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.card_view_view_post_detail);
subredditImageView = itemView.findViewById(R.id.subreddit_icon_circle_image_view_best_post_item);
subredditIconCircleImageView = itemView.findViewById(R.id.subreddit_icon_circle_image_view_best_post_item);
subredditNameTextView = itemView.findViewById(R.id.subreddit_text_view_best_post_item);
postTimeTextView = itemView.findViewById(R.id.post_time_text_view_best_post_item);
titleTextView = itemView.findViewById(R.id.title_text_view_best_post_item);
@ -531,16 +536,17 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
}
}
private static class LoadSubredditIconAsyncTask extends AsyncTask<Void, Void, Void> {
private Context context;
private CircleImageView iconImageView;
/*private static class LoadSubredditIconAsyncTask extends AsyncTask<Void, Void, Void> {
private final WeakReference<Context> contextWeakReference;
private final WeakReference<CircleImageView> circleImageViewWeakReference;
private SubredditDao subredditDao;
private String subredditName;
private String iconImageUrl;
LoadSubredditIconAsyncTask(Context context, CircleImageView iconImageView, SubredditDao subredditDao, String subredditName) {
this.context = context;
this.iconImageView = iconImageView;
contextWeakReference = new WeakReference<>(context);
circleImageViewWeakReference = new WeakReference<>(iconImageView);
this.subredditDao = subredditDao;
this.subredditName = subredditName;
}
@ -556,11 +562,16 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(iconImageUrl != null) {
Glide.with(context).load(iconImageUrl).into(iconImageView);
} else {
Glide.with(context).load(R.drawable.subreddit_default_icon).into(iconImageView);
Context context = contextWeakReference.get();
CircleImageView circleImageView = circleImageViewWeakReference.get();
if(context != null && circleImageView != null) {
if(iconImageUrl != null) {
Glide.with(context).load(iconImageUrl).into(circleImageView);
} else {
Glide.with(context).load(R.drawable.subreddit_default_icon).into(circleImageView);
}
}
}
}
}*/
}

View File

@ -0,0 +1,57 @@
package ml.docilealligator.infinityforreddit;
import android.content.Context;
import android.os.AsyncTask;
import com.bumptech.glide.Glide;
import java.lang.ref.WeakReference;
import de.hdodenhof.circleimageview.CircleImageView;
class LoadSubredditIconAsyncTask extends AsyncTask<Void, Void, Void> {
private final WeakReference<Context> contextWeakReference;
private final WeakReference<CircleImageView> circleImageViewWeakReference;
private SubredditDao subredditDao;
private String subredditName;
private String iconImageUrl;
private BestPostData bestPostData;
LoadSubredditIconAsyncTask(Context context, CircleImageView iconImageView,
SubredditDao subredditDao, String subredditName,
BestPostData bestPostData) {
contextWeakReference = new WeakReference<>(context);
circleImageViewWeakReference = new WeakReference<>(iconImageView);
this.subredditDao = subredditDao;
this.subredditName = subredditName;
this.bestPostData = bestPostData;
}
@Override
protected Void doInBackground(Void... voids) {
if(subredditDao.getSubredditData(subredditName) != null) {
iconImageUrl = subredditDao.getSubredditData(subredditName).getIconUrl();
} else {
iconImageUrl = "";
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Context context = contextWeakReference.get();
CircleImageView circleImageView = circleImageViewWeakReference.get();
if(context != null && circleImageView != null) {
if(!iconImageUrl.equals("")) {
Glide.with(context).load(iconImageUrl).into(circleImageView);
} else {
Glide.with(context).load(R.drawable.subreddit_default_icon).into(circleImageView);
}
}
bestPostData.setSubredditIconUrl(iconImageUrl);
}
}

View File

@ -66,7 +66,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mCoordinatorLayout = findViewById(R.id.coordinator_layout_view_post_detail);
CircleImageView subredditImageView = findViewById(R.id.subreddit_icon_circle_image_view_view_post_detail);
CircleImageView subredditIconCircleImageView = findViewById(R.id.subreddit_icon_circle_image_view_view_post_detail);
TextView postTimeTextView = findViewById(R.id.post_time_text_view_view_post_detail);
TextView subredditTextView = findViewById(R.id.subreddit_text_view_view_post_detail);
TextView contentTextView = findViewById(R.id.content_text_view_view_post_detail);
@ -86,6 +86,16 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mCommentCardView = findViewById(R.id.comment_card_view_view_post_detail);
mRecyclerView = findViewById(R.id.recycler_view_view_post_detail);
if(mPostData.getSubredditIconUrl() == null) {
new LoadSubredditIconAsyncTask(this, subredditIconCircleImageView,
SubredditRoomDatabase.getDatabase(this).subredditDao(), mPostData.getSubredditName(),
mPostData).execute();
} else if(!mPostData.getSubredditIconUrl().equals("")) {
Glide.with(this).load(mPostData.getSubredditIconUrl()).into(subredditIconCircleImageView);
} else {
Glide.with(this).load(R.drawable.subreddit_default_icon).into(subredditIconCircleImageView);
}
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));