From 56d82d133f7d9ecbe98c543d18a2a071ffe27c3e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 2 Sep 2021 20:58:27 +0800 Subject: Use PostFragment to handle loading subreddit or user icons for PostRecyclerViewAdapter. --- .../adapters/PostRecyclerViewAdapter.java | 151 +++++++++------------ .../infinityforreddit/fragments/PostFragment.java | 54 ++++++-- 2 files changed, 109 insertions(+), 96 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 12d6c0d1..350fb7d5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -75,7 +75,6 @@ import ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks; import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.VoteThing; import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity; @@ -86,8 +85,6 @@ import ml.docilealligator.infinityforreddit.activities.ViewRedditGalleryActivity import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.activities.ViewUserDetailActivity; import ml.docilealligator.infinityforreddit.activities.ViewVideoActivity; -import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon; -import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData; import ml.docilealligator.infinityforreddit.bottomsheetfragments.ShareLinkBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.AspectRatioGifImageView; @@ -134,13 +131,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - if (mActivity != null && getItemCount() > 0) { - if (iconImageUrl == null || iconImageUrl.equals("")) { + mFragment.loadIcon(post.getAuthor(), false, (subredditOrUserName, iconUrl) -> { + if (mActivity != null && getItemCount() > 0 && post.getAuthor().equals(subredditOrUserName)) { + if (iconUrl == null || iconUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .into(((PostBaseViewHolder) holder).iconGifImageView); } else { - mGlide.load(iconImageUrl) + mGlide.load(iconUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .error(mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) @@ -533,7 +520,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { - post.setAuthorIconUrl(iconImageUrl); + post.setAuthorIconUrl(iconUrl); } } }); @@ -550,27 +537,25 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - if (mActivity != null && getItemCount() > 0) { - if (iconImageUrl == null || iconImageUrl.equals("")) { - mGlide.load(R.drawable.subreddit_default_icon) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) - .into(((PostBaseViewHolder) holder).iconGifImageView); - } else { - mGlide.load(iconImageUrl) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) - .error(mGlide.load(R.drawable.subreddit_default_icon) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) - .into(((PostBaseViewHolder) holder).iconGifImageView); - } - - if (holder.getBindingAdapterPosition() >= 0) { - post.setSubredditIconUrl(iconImageUrl); - } - } - }); + mFragment.loadIcon(post.getSubredditName(), true, (subredditOrUserName, iconUrl) -> { + if (mActivity != null && getItemCount() > 0 && post.getSubredditName().equals(subredditOrUserName)) { + if (iconUrl == null || iconUrl.equals("")) { + mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(((PostBaseViewHolder) holder).iconGifImageView); + } else { + mGlide.load(iconUrl) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(((PostBaseViewHolder) holder).iconGifImageView); + } + + if (holder.getBindingAdapterPosition() >= 0) { + post.setSubredditIconUrl(iconUrl); + } + } + }); } else if (!post.getSubredditIconUrl().equals("")) { mGlide.load(post.getSubredditIconUrl()) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) @@ -586,14 +571,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + mFragment.loadIcon(authorName, post.getAuthor().equals("[deleted]"), (subredditOrUserName, iconUrl) -> { if (mActivity != null && getItemCount() > 0) { - if (iconImageUrl == null || iconImageUrl.equals("")) { + if (iconUrl == null || iconUrl.equals("") && authorName.equals(subredditOrUserName)) { mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .into(((PostBaseViewHolder) holder).iconGifImageView); } else { - mGlide.load(iconImageUrl) + mGlide.load(iconUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .error(mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) @@ -601,7 +586,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { - post.setAuthorIconUrl(iconImageUrl); + post.setAuthorIconUrl(iconUrl); } } }); @@ -644,21 +629,21 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 0 && !mHideTheNumberOfAwards) { + if (post.getNAwards() > 0 && !mHideTheNumberOfAwards) { ((PostBaseViewHolder) holder).awardsTextView.setVisibility(View.VISIBLE); - if (nAwards == 1) { + if (post.getNAwards() == 1) { ((PostBaseViewHolder) holder).awardsTextView.setText(mActivity.getString(R.string.one_award)); } else { - ((PostBaseViewHolder) holder).awardsTextView.setText(mActivity.getString(R.string.n_awards, nAwards)); + ((PostBaseViewHolder) holder).awardsTextView.setText(mActivity.getString(R.string.n_awards, post.getNAwards())); } } @@ -1011,14 +996,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - if (mActivity != null && getItemCount() > 0) { - if (iconImageUrl == null || iconImageUrl.equals("")) { + mFragment.loadIcon(post.getAuthor(), false, (subredditOrUserName, iconUrl) -> { + if (mActivity != null && getItemCount() > 0 && post.getAuthor().equals(subredditOrUserName)) { + if (iconUrl == null || iconUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .into(((PostCompactBaseViewHolder) holder).iconGifImageView); } else { - mGlide.load(iconImageUrl) + mGlide.load(iconUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .error(mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) @@ -1026,7 +1011,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { - post.setAuthorIconUrl(iconImageUrl); + post.setAuthorIconUrl(iconUrl); } } }); @@ -1043,27 +1028,25 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - if (mActivity != null && getItemCount() > 0) { - if (iconImageUrl == null || iconImageUrl.equals("")) { - mGlide.load(R.drawable.subreddit_default_icon) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) - .into(((PostCompactBaseViewHolder) holder).iconGifImageView); - } else { - mGlide.load(iconImageUrl) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) - .error(mGlide.load(R.drawable.subreddit_default_icon) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) - .into(((PostCompactBaseViewHolder) holder).iconGifImageView); - } - - if (holder.getBindingAdapterPosition() >= 0) { - post.setSubredditIconUrl(iconImageUrl); - } - } - }); + mFragment.loadIcon(subredditName, true, (subredditOrUserName, iconUrl) -> { + if (mActivity != null && getItemCount() > 0 && subredditName.equals(subredditOrUserName)) { + if (iconUrl == null || iconUrl.equals("")) { + mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(((PostCompactBaseViewHolder) holder).iconGifImageView); + } else { + mGlide.load(iconUrl) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(((PostCompactBaseViewHolder) holder).iconGifImageView); + } + + if (holder.getBindingAdapterPosition() >= 0) { + post.setSubredditIconUrl(iconUrl); + } + } + }); } else if (!post.getSubredditIconUrl().equals("")) { mGlide.load(post.getSubredditIconUrl()) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) @@ -1085,15 +1068,15 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - if (mActivity != null && getItemCount() > 0) { - if (iconImageUrl == null || iconImageUrl.equals("")) { + String authorName = post.getAuthor().equals("[deleted]") ? post.getSubredditName() : post.getAuthor(); + mFragment.loadIcon(authorName, post.getAuthor().equals("[deleted]"), (subredditOrUserName, iconUrl) -> { + if (mActivity != null && getItemCount() > 0 && authorName.equals(subredditOrUserName)) { + if (iconUrl == null || iconUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .into(((PostCompactBaseViewHolder) holder).iconGifImageView); } else { - mGlide.load(iconImageUrl) + mGlide.load(iconUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .error(mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) @@ -1101,7 +1084,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { - post.setAuthorIconUrl(iconImageUrl); + post.setAuthorIconUrl(iconUrl); } } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index c98f27e6..8db76f8f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -53,7 +53,9 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import java.util.ArrayList; +import java.util.HashMap; import java.util.Locale; +import java.util.Map; import java.util.Random; import java.util.concurrent.Executor; @@ -79,6 +81,8 @@ import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity; import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.adapters.PostRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon; +import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.CustomToroContainer; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; @@ -246,6 +250,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private ItemTouchHelper touchHelper; private ArrayList readPosts; private Unbinder unbinder; + private Map subredditOrUserIcons = new HashMap<>(); public PostFragment() { // Required empty public constructor @@ -444,8 +449,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { sortType = new SortType(SortType.Type.valueOf(sort), SortType.Time.valueOf(sortTime)); postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, defaultPostLayout); - mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, + mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -526,8 +531,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { sortType = new SortType(SortType.Type.valueOf(sort)); } - mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, + mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, displaySubredditName, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -602,8 +607,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { sortType = new SortType(SortType.Type.valueOf(sort)); } - mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, + mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -672,8 +677,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + username, defaultPostLayout); - mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, + mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -738,8 +743,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout); - mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, + mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -797,8 +802,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout); - mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, + mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, mExoCreator, new PostRecyclerViewAdapter.Callback() { @@ -1578,6 +1583,27 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + public void loadIcon(String subredditOrUserName, boolean isSubreddit, LoadIconListener loadIconListener) { + if (subredditOrUserIcons.containsKey(subredditOrUserName)) { + loadIconListener.loadIconSuccess(subredditOrUserName, subredditOrUserIcons.get(subredditOrUserName)); + } else { + if (isSubreddit) { + LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, + subredditOrUserName, mRetrofit, + iconImageUrl -> { + subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); + loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); + }); + } else { + LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditOrUserName, + mRetrofit, iconImageUrl -> { + subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); + loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); + }); + } + } + } + @Subscribe public void onPostUpdateEvent(PostUpdateEventToPostList event) { PagedList posts = mAdapter.getCurrentList(); @@ -2048,4 +2074,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } } + + public interface LoadIconListener { + void loadIconSuccess(String subredditOrUserName, String iconUrl); + } } \ No newline at end of file -- cgit v1.2.3