From 0a68bc3a0190a821612eb75879625ca9ec0a2b9f Mon Sep 17 00:00:00 2001 From: malinajirka Date: Tue, 3 Jul 2018 14:02:21 +0200 Subject: [PATCH 1/5] Replace Volley with Glide in the PostsListAdapter --- .../android/ui/posts/PostsListActivity.java | 2 +- .../ui/posts/adapters/PostsListAdapter.java | 79 +++++++++++-------- .../ui/reader/adapters/ReaderPostAdapter.java | 2 +- .../android/util/image/ImageManager.kt | 17 ++++ .../src/main/res/layout/post_cardview.xml | 3 +- 5 files changed, 68 insertions(+), 35 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListActivity.java index 9c98b6f728f9..c1c826cbf279 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListActivity.java @@ -44,7 +44,7 @@ public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.post_list_activity); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); if (savedInstanceState == null) { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/adapters/PostsListAdapter.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/adapters/PostsListAdapter.java index 2e998d6afaca..4c31e07de131 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/adapters/PostsListAdapter.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/adapters/PostsListAdapter.java @@ -23,11 +23,13 @@ import android.view.animation.DecelerateInterpolator; import android.widget.AdapterView; import android.widget.ImageView; +import android.widget.ImageView.ScaleType; import android.widget.ProgressBar; import android.widget.TextView; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; +import org.wordpress.android.BuildConfig; import org.wordpress.android.R; import org.wordpress.android.WordPress; import org.wordpress.android.fluxc.Dispatcher; @@ -53,8 +55,9 @@ import org.wordpress.android.util.DisplayUtils; import org.wordpress.android.util.ImageUtils; import org.wordpress.android.util.SiteUtils; +import org.wordpress.android.util.image.ImageManager; +import org.wordpress.android.util.image.ImageType; import org.wordpress.android.widgets.PostListButton; -import org.wordpress.android.widgets.WPNetworkImageView; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -110,6 +113,7 @@ public enum LoadMode { @Inject protected PostStore mPostStore; @Inject protected MediaStore mMediaStore; @Inject protected UploadStore mUploadStore; + @Inject protected ImageManager mImageManager; public PostsListAdapter(Context context, @NonNull SiteModel site, boolean isPage) { ((WordPress) context.getApplicationContext()).component().inject(this); @@ -160,7 +164,7 @@ private boolean isValidPostPosition(int position) { } @Override - public void onAttachedToRecyclerView(RecyclerView recyclerView) { + public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); mRecyclerView = recyclerView; } @@ -183,7 +187,7 @@ public int getItemCount() { } @Override - public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if (viewType == VIEW_TYPE_ENDLIST_INDICATOR) { View view = mLayoutInflater.inflate(R.layout.endlist_indicator, parent, false); view.getLayoutParams().height = mEndlistIndicatorHeight; @@ -210,7 +214,7 @@ private boolean canPublishPost(PostModel post) { } @Override - public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { // nothing to do if this is the static endlist indicator if (getItemViewType(position) == VIEW_TYPE_ENDLIST_INDICATOR) { return; @@ -333,24 +337,25 @@ public void onClick(View v) { }); } - private void showFeaturedImage(int postId, WPNetworkImageView imgFeatured) { + private void showFeaturedImage(int postId, ImageView imgFeatured) { String imageUrl = mFeaturedImageUrls.get(postId); if (imageUrl == null) { imgFeatured.setVisibility(View.GONE); + mImageManager.cancelRequestAndClearImageView(imgFeatured); } else if (imageUrl.startsWith("http")) { String photonUrl = ReaderUtils.getResizedImageUrl( imageUrl, mPhotonWidth, mPhotonHeight, !SiteUtils.isPhotonCapable(mSite)); imgFeatured.setVisibility(View.VISIBLE); - imgFeatured.setImageUrl(photonUrl, WPNetworkImageView.ImageType.PHOTO); + mImageManager.load(imgFeatured, ImageType.PHOTO, photonUrl, ScaleType.CENTER_CROP); } else { Bitmap bmp = ImageUtils.getWPImageSpanThumbnailFromFilePath( imgFeatured.getContext(), imageUrl, mPhotonWidth); if (bmp != null) { - imgFeatured.setImageUrl(null, WPNetworkImageView.ImageType.NONE); imgFeatured.setVisibility(View.VISIBLE); - imgFeatured.setImageBitmap(bmp); + mImageManager.load(imgFeatured, bmp); } else { imgFeatured.setVisibility(View.GONE); + mImageManager.cancelRequestAndClearImageView(imgFeatured); } } } @@ -431,6 +436,7 @@ private void updateStatusTextAndImage(TextView txtStatus, ImageView imgStatus, P if ((PostStatus.fromPost(post) == PostStatus.PUBLISHED) && !post.isLocalDraft() && !post.isLocallyChanged()) { txtStatus.setVisibility(View.GONE); imgStatus.setVisibility(View.GONE); + mImageManager.cancelRequestAndClearImageView(imgStatus); } else { int statusTextResId = 0; int statusIconResId = 0; @@ -490,6 +496,16 @@ private void updateStatusTextAndImage(TextView txtStatus, ImageView imgStatus, P statusIconResId = R.drawable.ic_gridicons_page; statusColorResId = R.color.alert_red; break; + case UNKNOWN: + // no-op + break; + case PUBLISHED: + // no-op + break; + default: + if (BuildConfig.DEBUG) { + throw new IllegalStateException("Missing switch case."); + } } } @@ -506,10 +522,11 @@ private void updateStatusTextAndImage(TextView txtStatus, ImageView imgStatus, P if (drawable != null) { drawable = DrawableCompat.wrap(drawable); DrawableCompat.setTint(drawable, resources.getColor(statusColorResId)); - imgStatus.setImageDrawable(drawable); imgStatus.setVisibility(View.VISIBLE); + mImageManager.load(imgStatus, drawable); } else { imgStatus.setVisibility(View.GONE); + mImageManager.cancelRequestAndClearImageView(imgStatus); } } } @@ -731,7 +748,7 @@ private class PostViewHolder extends RecyclerView.ViewHolder { private final PostListButton mBtnTrash; private final PostListButton mBtnBack; - private final WPNetworkImageView mImgFeatured; + private final ImageView mImgFeatured; private final ViewGroup mLayoutButtons; private final View mDisabledOverlay; @@ -741,27 +758,27 @@ private class PostViewHolder extends RecyclerView.ViewHolder { PostViewHolder(View view) { super(view); - mTxtTitle = (TextView) view.findViewById(R.id.text_title); - mTxtExcerpt = (TextView) view.findViewById(R.id.text_excerpt); - mTxtDate = (TextView) view.findViewById(R.id.text_date); - mTxtStatus = (TextView) view.findViewById(R.id.text_status); - mImgStatus = (ImageView) view.findViewById(R.id.image_status); + mTxtTitle = view.findViewById(R.id.text_title); + mTxtExcerpt = view.findViewById(R.id.text_excerpt); + mTxtDate = view.findViewById(R.id.text_date); + mTxtStatus = view.findViewById(R.id.text_status); + mImgStatus = view.findViewById(R.id.image_status); - mBtnEdit = (PostListButton) view.findViewById(R.id.btn_edit); - mBtnView = (PostListButton) view.findViewById(R.id.btn_view); - mBtnPublish = (PostListButton) view.findViewById(R.id.btn_publish); - mBtnMore = (PostListButton) view.findViewById(R.id.btn_more); + mBtnEdit = view.findViewById(R.id.btn_edit); + mBtnView = view.findViewById(R.id.btn_view); + mBtnPublish = view.findViewById(R.id.btn_publish); + mBtnMore = view.findViewById(R.id.btn_more); - mBtnStats = (PostListButton) view.findViewById(R.id.btn_stats); - mBtnTrash = (PostListButton) view.findViewById(R.id.btn_trash); - mBtnBack = (PostListButton) view.findViewById(R.id.btn_back); + mBtnStats = view.findViewById(R.id.btn_stats); + mBtnTrash = view.findViewById(R.id.btn_trash); + mBtnBack = view.findViewById(R.id.btn_back); - mImgFeatured = (WPNetworkImageView) view.findViewById(R.id.image_featured); - mLayoutButtons = (ViewGroup) view.findViewById(R.id.layout_buttons); + mImgFeatured = view.findViewById(R.id.image_featured); + mLayoutButtons = view.findViewById(R.id.layout_buttons); mDisabledOverlay = view.findViewById(R.id.disabled_overlay); - mProgressBar = (ProgressBar) view.findViewById(R.id.post_upload_progress); + mProgressBar = view.findViewById(R.id.post_upload_progress); } } @@ -778,15 +795,15 @@ private class PageViewHolder extends RecyclerView.ViewHolder { PageViewHolder(View view) { super(view); - mTxtTitle = (TextView) view.findViewById(R.id.text_title); - mTxtStatus = (TextView) view.findViewById(R.id.text_status); - mImgStatus = (ImageView) view.findViewById(R.id.image_status); + mTxtTitle = view.findViewById(R.id.text_title); + mTxtStatus = view.findViewById(R.id.text_status); + mImgStatus = view.findViewById(R.id.image_status); mBtnMore = view.findViewById(R.id.btn_more); mDividerTop = view.findViewById(R.id.divider_top); - mDateHeader = (ViewGroup) view.findViewById(R.id.header_date); - mTxtDate = (TextView) mDateHeader.findViewById(R.id.text_date); + mDateHeader = view.findViewById(R.id.header_date); + mTxtDate = mDateHeader.findViewById(R.id.text_date); mDisabledOverlay = view.findViewById(R.id.disabled_overlay); - mProgressBar = (ProgressBar) view.findViewById(R.id.post_upload_progress); + mProgressBar = view.findViewById(R.id.post_upload_progress); } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java index 653fec38a7b0..65f547cd1ea9 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java @@ -906,7 +906,7 @@ private void updateBookmarkView(final ReaderPostViewHolder holder, final ReaderP } else { bookmarkButton.setVisibility(View.GONE); } - bookmarkButton.setImageResource(post.isBookmarked + mImageManager.load(bookmarkButton, post.isBookmarked ? R.drawable.ic_bookmark_blue_medium_18dp : R.drawable.ic_bookmark_grey_min_18dp); if (post.isBookmarked) { diff --git a/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt b/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt index 7c40ae2d7ddf..4952bc43937b 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt @@ -47,6 +47,14 @@ class ImageManager @Inject constructor(val placeholderManager: ImagePlaceholderM .into(imageView) } + @JvmOverloads + fun load(imageView: ImageView, resourceId: Int, scaleType: ImageView.ScaleType = CENTER) { + GlideApp.with(imageView.context) + .load(resourceId) + .applyScaleType(scaleType) + .into(imageView) + } + fun loadIntoCircle(imageView: ImageView, imageType: ImageType, imgUrl: String) { GlideApp.with(imageView.context) .load(imgUrl) @@ -121,6 +129,15 @@ class ImageManager @Inject constructor(val placeholderManager: ImagePlaceholderM ImageManager(ImagePlaceholderManager()).load(imageView, drawable, scaleType) } + @JvmStatic + @Deprecated("Use injected ImageManager", + ReplaceWith("imageManager.load(imageView, resourceId, scaleType)", + "org.wordpress.android.util.image.ImageManager")) + @JvmOverloads + fun loadImage(imageView: ImageView, resourceId: Int, scaleType: ImageView.ScaleType = CENTER) { + ImageManager(ImagePlaceholderManager()).load(imageView, resourceId, scaleType) + } + @JvmStatic @Deprecated("Use injected ImageManager", ReplaceWith("imageManager.loadIntoCircle(imageView, imgType, imgUrl)", diff --git a/WordPress/src/main/res/layout/post_cardview.xml b/WordPress/src/main/res/layout/post_cardview.xml index 109bfcca3b5c..566c264cf506 100644 --- a/WordPress/src/main/res/layout/post_cardview.xml +++ b/WordPress/src/main/res/layout/post_cardview.xml @@ -17,11 +17,10 @@ android:background="?android:selectableItemBackground" android:orientation="vertical"> - From c627dc519b4277b956151357fb11d2869bfd9e7c Mon Sep 17 00:00:00 2001 From: malinajirka Date: Tue, 10 Jul 2018 11:50:51 +0200 Subject: [PATCH 2/5] Revert switch statement refactoring --- .../android/ui/posts/adapters/PostsListAdapter.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/adapters/PostsListAdapter.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/adapters/PostsListAdapter.java index 4c31e07de131..60add7fdae98 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/adapters/PostsListAdapter.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/adapters/PostsListAdapter.java @@ -29,7 +29,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; -import org.wordpress.android.BuildConfig; import org.wordpress.android.R; import org.wordpress.android.WordPress; import org.wordpress.android.fluxc.Dispatcher; @@ -503,9 +502,8 @@ private void updateStatusTextAndImage(TextView txtStatus, ImageView imgStatus, P // no-op break; default: - if (BuildConfig.DEBUG) { - throw new IllegalStateException("Missing switch case."); - } + // no-op + return; } } From b405674397901da80479254612caa3da7b7e55b1 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Wed, 11 Jul 2018 09:55:49 +0200 Subject: [PATCH 3/5] Clean up companion object methods in ImageManager --- .../android/util/image/ImageManager.kt | 43 ------------------- 1 file changed, 43 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt b/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt index 4952bc43937b..53c7e86c9491 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt @@ -110,48 +110,5 @@ class ImageManager @Inject constructor(val placeholderManager: ImagePlaceholderM ) { ImageManager(ImagePlaceholderManager()).load(imageView, imageType, imgUrl, scaleType) } - - @JvmStatic - @Deprecated("Use injected ImageManager", - ReplaceWith("imageManager.load(imageView, bitmap, scaleType)", - "org.wordpress.android.util.image.ImageManager")) - @JvmOverloads - fun loadImage(imageView: ImageView, bitmap: Bitmap, scaleType: ImageView.ScaleType = CENTER) { - ImageManager(ImagePlaceholderManager()).load(imageView, bitmap, scaleType) - } - - @JvmStatic - @Deprecated("Use injected ImageManager", - ReplaceWith("imageManager.load(imageView, drawable, scaleType)", - "org.wordpress.android.util.image.ImageManager")) - @JvmOverloads - fun loadImage(imageView: ImageView, drawable: Drawable, scaleType: ImageView.ScaleType = CENTER) { - ImageManager(ImagePlaceholderManager()).load(imageView, drawable, scaleType) - } - - @JvmStatic - @Deprecated("Use injected ImageManager", - ReplaceWith("imageManager.load(imageView, resourceId, scaleType)", - "org.wordpress.android.util.image.ImageManager")) - @JvmOverloads - fun loadImage(imageView: ImageView, resourceId: Int, scaleType: ImageView.ScaleType = CENTER) { - ImageManager(ImagePlaceholderManager()).load(imageView, resourceId, scaleType) - } - - @JvmStatic - @Deprecated("Use injected ImageManager", - ReplaceWith("imageManager.loadIntoCircle(imageView, imgType, imgUrl)", - "org.wordpress.android.util.image.ImageManager")) - fun loadImageIntoCircle(imageView: ImageView, imageType: ImageType, imgUrl: String) { - ImageManager(ImagePlaceholderManager()).loadIntoCircle(imageView, imageType, imgUrl) - } - - @JvmStatic - @Deprecated("Use injected ImageManager", - ReplaceWith("imageManager.clear(imageView)", - "org.wordpress.android.util.image.ImageManager")) - fun clear(imageView: ImageView) { - ImageManager(ImagePlaceholderManager()).cancelRequestAndClearImageView(imageView) - } } } From 4403677d5f2f27dfa68da2f33c2feacab2d27279 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Wed, 11 Jul 2018 10:31:04 +0200 Subject: [PATCH 4/5] Replace ImageManager's companion methods with singleton --- .../ui/reader/views/ReaderThumbnailStrip.java | 2 +- .../wordpress/android/util/image/ImageManager.kt | 14 ++------------ 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderThumbnailStrip.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderThumbnailStrip.java index c93ceb8fd99f..2651c8980776 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderThumbnailStrip.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderThumbnailStrip.java @@ -95,7 +95,7 @@ public void loadThumbnails(long blogId, long postId, boolean isPrivate) { mView.addView(view); String photonUrl = PhotonUtils.getPhotonImageUrl(imageUrl, mThumbnailWidth, mThumbnailHeight); - ImageManager.loadImage(imageView, ImageType.PHOTO, photonUrl, ScaleType.CENTER_CROP); + ImageManager.Companion.getInstance().load(imageView, ImageType.PHOTO, photonUrl, ScaleType.CENTER_CROP); // tapping a thumbnail opens the photo viewer imageView.setOnClickListener(new OnClickListener() { diff --git a/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt b/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt index 53c7e86c9491..72aab8a88f05 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt @@ -98,17 +98,7 @@ class ImageManager @Inject constructor(val placeholderManager: ImagePlaceholderM @Deprecated("Object for backward compatibility with code which doesn't support DI") companion object { - @JvmStatic - @Deprecated("Use injected ImageManager", - ReplaceWith("imageManager.load(imageView, imgUrl, placeholder, scaleType)", - "org.wordpress.android.util.image.ImageManager")) - fun loadImage( - imageView: ImageView, - imageType: ImageType, - imgUrl: String, - scaleType: ImageView.ScaleType - ) { - ImageManager(ImagePlaceholderManager()).load(imageView, imageType, imgUrl, scaleType) - } + @Deprecated("Use injected ImageManager") + val instance: ImageManager by lazy { ImageManager(ImagePlaceholderManager()) } } } From 212914236954817eae12c4f116439df232749e2c Mon Sep 17 00:00:00 2001 From: malinajirka Date: Thu, 12 Jul 2018 08:10:10 +0200 Subject: [PATCH 5/5] Add JvmStatic annotation to singleton instance in ImageManager --- .../wordpress/android/ui/reader/views/ReaderThumbnailStrip.java | 2 +- .../main/java/org/wordpress/android/util/image/ImageManager.kt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderThumbnailStrip.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderThumbnailStrip.java index 2651c8980776..524ab3817db6 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderThumbnailStrip.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderThumbnailStrip.java @@ -95,7 +95,7 @@ public void loadThumbnails(long blogId, long postId, boolean isPrivate) { mView.addView(view); String photonUrl = PhotonUtils.getPhotonImageUrl(imageUrl, mThumbnailWidth, mThumbnailHeight); - ImageManager.Companion.getInstance().load(imageView, ImageType.PHOTO, photonUrl, ScaleType.CENTER_CROP); + ImageManager.getInstance().load(imageView, ImageType.PHOTO, photonUrl, ScaleType.CENTER_CROP); // tapping a thumbnail opens the photo viewer imageView.setOnClickListener(new OnClickListener() { diff --git a/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt b/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt index 72aab8a88f05..592367b4702b 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/image/ImageManager.kt @@ -98,6 +98,7 @@ class ImageManager @Inject constructor(val placeholderManager: ImagePlaceholderM @Deprecated("Object for backward compatibility with code which doesn't support DI") companion object { + @JvmStatic @Deprecated("Use injected ImageManager") val instance: ImageManager by lazy { ImageManager(ImagePlaceholderManager()) } }