diff options
author | Alex Ning <chineseperson5@gmail.com> | 2018-08-10 02:24:36 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2018-08-10 02:24:36 +0000 |
commit | 362634d1c7b81b13c9814c7dfdd6ed35612149d4 (patch) | |
tree | 637a0e5c07a8f2387078c954b98e09307a895ce1 /app/src/main/java | |
parent | e44a172db0c2fb35f4d5657226c86ad4af0034dc (diff) | |
download | infinity-for-reddit-362634d1c7b81b13c9814c7dfdd6ed35612149d4.tar infinity-for-reddit-362634d1c7b81b13c9814c7dfdd6ed35612149d4.tar.gz infinity-for-reddit-362634d1c7b81b13c9814c7dfdd6ed35612149d4.tar.bz2 infinity-for-reddit-362634d1c7b81b13c9814c7dfdd6ed35612149d4.tar.lz infinity-for-reddit-362634d1c7b81b13c9814c7dfdd6ed35612149d4.tar.xz infinity-for-reddit-362634d1c7b81b13c9814c7dfdd6ed35612149d4.tar.zst infinity-for-reddit-362634d1c7b81b13c9814c7dfdd6ed35612149d4.zip |
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.
Diffstat (limited to 'app/src/main/java')
4 files changed, 105 insertions, 16 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostData.java index 8c08680c..ce998cff 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostData.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java index 7b70ca08..ed3fd0dd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java @@ -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); + } } } - } + }*/ } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java new file mode 100644 index 00000000..e75343d3 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java @@ -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); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index f0f3dcf5..c65d5ab8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -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)); |