From dc12b60c8f0a21eb9b3ab2164471c93d2e20ae25 Mon Sep 17 00:00:00 2001 From: Alex Saveau Date: Tue, 5 Dec 2017 09:30:25 -0800 Subject: [PATCH] Improve Kotlin support for RecyclerView module (#2670) * Improve Kotlin support for RecyclerView module with nullability annotations Signed-off-by: Alex Saveau --- .../RecyclerToListViewScrollListener.java | 3 +- .../recyclerview/RecyclerViewPreloader.java | 29 +++++++------ .../main/java/com/bumptech/glide/Glide.java | 2 +- .../com/bumptech/glide/ListPreloader.java | 13 +++--- .../glide/util/FixedPreloadSizeProvider.java | 5 ++- .../glide/util/ViewPreloadSizeProvider.java | 5 ++- .../com/bumptech/glide/ListPreloaderTest.java | 42 ++++++++++--------- .../glide/samples/flickr/FlickrPhotoGrid.java | 7 ++-- .../glide/samples/flickr/FlickrPhotoList.java | 11 ++--- .../samples/gallery/RecyclerAdapter.java | 11 +++-- .../glide/samples/giphy/MainActivity.java | 11 ++--- 11 files changed, 80 insertions(+), 59 deletions(-) diff --git a/integration/recyclerview/src/main/java/com/bumptech/glide/integration/recyclerview/RecyclerToListViewScrollListener.java b/integration/recyclerview/src/main/java/com/bumptech/glide/integration/recyclerview/RecyclerToListViewScrollListener.java index 19f82293bb..30bf14dcb2 100644 --- a/integration/recyclerview/src/main/java/com/bumptech/glide/integration/recyclerview/RecyclerToListViewScrollListener.java +++ b/integration/recyclerview/src/main/java/com/bumptech/glide/integration/recyclerview/RecyclerToListViewScrollListener.java @@ -1,5 +1,6 @@ package com.bumptech.glide.integration.recyclerview; +import android.support.annotation.NonNull; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.AbsListView; @@ -20,7 +21,7 @@ public final class RecyclerToListViewScrollListener extends RecyclerView.OnScrol private int lastVisibleCount = -1; private int lastItemCount = -1; - public RecyclerToListViewScrollListener(AbsListView.OnScrollListener scrollListener) { + public RecyclerToListViewScrollListener(@NonNull AbsListView.OnScrollListener scrollListener) { this.scrollListener = scrollListener; } diff --git a/integration/recyclerview/src/main/java/com/bumptech/glide/integration/recyclerview/RecyclerViewPreloader.java b/integration/recyclerview/src/main/java/com/bumptech/glide/integration/recyclerview/RecyclerViewPreloader.java index 47be184a79..fb22947804 100644 --- a/integration/recyclerview/src/main/java/com/bumptech/glide/integration/recyclerview/RecyclerViewPreloader.java +++ b/integration/recyclerview/src/main/java/com/bumptech/glide/integration/recyclerview/RecyclerViewPreloader.java @@ -2,6 +2,7 @@ import android.app.Activity; import android.app.Fragment; +import android.support.annotation.NonNull; import android.support.v4.app.FragmentActivity; import android.support.v7.widget.RecyclerView; import com.bumptech.glide.Glide; @@ -34,17 +35,18 @@ public final class RecyclerViewPreloader extends RecyclerView.OnScrollListene /** * Helper constructor that accepts an {@link Activity}. */ - public RecyclerViewPreloader(Activity activity, - PreloadModelProvider preloadModelProvider, - PreloadSizeProvider preloadDimensionProvider, int maxPreload) { + public RecyclerViewPreloader(@NonNull Activity activity, + @NonNull PreloadModelProvider preloadModelProvider, + @NonNull PreloadSizeProvider preloadDimensionProvider, int maxPreload) { this(Glide.with(activity), preloadModelProvider, preloadDimensionProvider, maxPreload); } /** * Helper constructor that accepts an {@link FragmentActivity}. */ - public RecyclerViewPreloader(FragmentActivity fragmentActivity, - PreloadModelProvider preloadModelProvider, PreloadSizeProvider preloadDimensionProvider, + public RecyclerViewPreloader(@NonNull FragmentActivity fragmentActivity, + @NonNull PreloadModelProvider preloadModelProvider, + @NonNull PreloadSizeProvider preloadDimensionProvider, int maxPreload) { this(Glide.with(fragmentActivity), preloadModelProvider, preloadDimensionProvider, maxPreload); } @@ -52,8 +54,9 @@ public RecyclerViewPreloader(FragmentActivity fragmentActivity, /** * Helper constructor that accepts an {@link Fragment}. */ - public RecyclerViewPreloader(Fragment fragment, - PreloadModelProvider preloadModelProvider, PreloadSizeProvider preloadDimensionProvider, + public RecyclerViewPreloader(@NonNull Fragment fragment, + @NonNull PreloadModelProvider preloadModelProvider, + @NonNull PreloadSizeProvider preloadDimensionProvider, int maxPreload) { this(Glide.with(fragment), preloadModelProvider, preloadDimensionProvider, maxPreload); } @@ -61,11 +64,13 @@ public RecyclerViewPreloader(Fragment fragment, /** * Helper constructor that accepts an {@link android.support.v4.app.Fragment}. */ - public RecyclerViewPreloader(android.support.v4.app.Fragment fragment, - PreloadModelProvider preloadModelProvider, PreloadSizeProvider preloadDimensionProvider, + public RecyclerViewPreloader(@NonNull android.support.v4.app.Fragment fragment, + @NonNull PreloadModelProvider preloadModelProvider, + @NonNull PreloadSizeProvider preloadDimensionProvider, int maxPreload) { this(Glide.with(fragment), preloadModelProvider, preloadDimensionProvider, maxPreload); } + /** * Constructor that accepts interfaces for providing the dimensions of images to preload, the list * of models to preload for a given position, and the request to use to load images. @@ -74,9 +79,9 @@ public RecyclerViewPreloader(android.support.v4.app.Fragment fragment, * @param preloadDimensionProvider Provides the dimensions of images to load. * @param maxPreload Maximum number of items to preload. */ - public RecyclerViewPreloader(RequestManager requestManager, - PreloadModelProvider preloadModelProvider, - PreloadSizeProvider preloadDimensionProvider, int maxPreload) { + public RecyclerViewPreloader(@NonNull RequestManager requestManager, + @NonNull PreloadModelProvider preloadModelProvider, + @NonNull PreloadSizeProvider preloadDimensionProvider, int maxPreload) { ListPreloader listPreloader = new ListPreloader<>(requestManager, preloadModelProvider, preloadDimensionProvider, maxPreload); diff --git a/library/src/main/java/com/bumptech/glide/Glide.java b/library/src/main/java/com/bumptech/glide/Glide.java index 1e708345fc..42abbb23c6 100644 --- a/library/src/main/java/com/bumptech/glide/Glide.java +++ b/library/src/main/java/com/bumptech/glide/Glide.java @@ -602,7 +602,7 @@ private static RequestManagerRetriever getRetriever(@Nullable Context context) { // only occur due to errors with the Fragment lifecycle. Preconditions.checkNotNull( context, - "You cannot start a load on a not yet attached View or a Fragment where getActivity() " + "You cannot start a load on a not yet attached View or a Fragment where getActivity() " + "returns null (which usually occurs when getActivity() is called before the Fragment " + "is attached or after the Fragment is destroyed)."); return Glide.get(context).getRequestManagerRetriever(); diff --git a/library/src/main/java/com/bumptech/glide/ListPreloader.java b/library/src/main/java/com/bumptech/glide/ListPreloader.java index 06de9eaa22..806463d37c 100644 --- a/library/src/main/java/com/bumptech/glide/ListPreloader.java +++ b/library/src/main/java/com/bumptech/glide/ListPreloader.java @@ -85,7 +85,7 @@ public interface PreloadModelProvider { * @param item The model to load. */ @Nullable - RequestBuilder getPreloadRequestBuilder(U item); + RequestBuilder getPreloadRequestBuilder(@NonNull U item); } /** @@ -107,7 +107,7 @@ public interface PreloadSizeProvider { * @param item A model */ @Nullable - int[] getPreloadSize(T item, int adapterPosition, int perItemPosition); + int[] getPreloadSize(@NonNull T item, int adapterPosition, int perItemPosition); } /** @@ -119,8 +119,9 @@ public interface PreloadSizeProvider { * @param preloadDimensionProvider Provides the dimensions of images to load. * @param maxPreload Maximum number of items to preload. */ - public ListPreloader(RequestManager requestManager, PreloadModelProvider preloadModelProvider, - PreloadSizeProvider preloadDimensionProvider, int maxPreload) { + public ListPreloader(@NonNull RequestManager requestManager, + @NonNull PreloadModelProvider preloadModelProvider, + @NonNull PreloadSizeProvider preloadDimensionProvider, int maxPreload) { this.requestManager = requestManager; this.preloadModelProvider = preloadModelProvider; this.preloadDimensionProvider = preloadDimensionProvider; @@ -169,12 +170,12 @@ private void preload(int from, int to) { if (from < to) { // Increasing for (int i = start; i < end; i++) { - preloadAdapterPosition(this.preloadModelProvider.getPreloadItems(i), i, true); + preloadAdapterPosition(preloadModelProvider.getPreloadItems(i), i, true); } } else { // Decreasing for (int i = end - 1; i >= start; i--) { - preloadAdapterPosition(this.preloadModelProvider.getPreloadItems(i), i, false); + preloadAdapterPosition(preloadModelProvider.getPreloadItems(i), i, false); } } diff --git a/library/src/main/java/com/bumptech/glide/util/FixedPreloadSizeProvider.java b/library/src/main/java/com/bumptech/glide/util/FixedPreloadSizeProvider.java index d047eada07..92daaf11ba 100644 --- a/library/src/main/java/com/bumptech/glide/util/FixedPreloadSizeProvider.java +++ b/library/src/main/java/com/bumptech/glide/util/FixedPreloadSizeProvider.java @@ -1,5 +1,7 @@ package com.bumptech.glide.util; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import com.bumptech.glide.ListPreloader; /** @@ -21,8 +23,9 @@ public FixedPreloadSizeProvider(int width, int height) { this.size = new int[] { width, height }; } + @Nullable @Override - public int[] getPreloadSize(T item, int adapterPosition, int itemPosition) { + public int[] getPreloadSize(@NonNull T item, int adapterPosition, int itemPosition) { return size; } } diff --git a/library/src/main/java/com/bumptech/glide/util/ViewPreloadSizeProvider.java b/library/src/main/java/com/bumptech/glide/util/ViewPreloadSizeProvider.java index 5659d41d58..4a85618faf 100644 --- a/library/src/main/java/com/bumptech/glide/util/ViewPreloadSizeProvider.java +++ b/library/src/main/java/com/bumptech/glide/util/ViewPreloadSizeProvider.java @@ -1,5 +1,7 @@ package com.bumptech.glide.util; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.view.View; import com.bumptech.glide.ListPreloader; import com.bumptech.glide.request.target.SizeReadyCallback; @@ -43,8 +45,9 @@ public ViewPreloadSizeProvider(View view) { this.viewTarget = new SizeViewTarget(view, this); } + @Nullable @Override - public int[] getPreloadSize(T item, int adapterPosition, int itemPosition) { + public int[] getPreloadSize(@NonNull T item, int adapterPosition, int itemPosition) { if (size == null) { return null; } else { diff --git a/library/src/test/java/com/bumptech/glide/ListPreloaderTest.java b/library/src/test/java/com/bumptech/glide/ListPreloaderTest.java index d5dd52e1e4..e6c957d272 100644 --- a/library/src/test/java/com/bumptech/glide/ListPreloaderTest.java +++ b/library/src/test/java/com/bumptech/glide/ListPreloaderTest.java @@ -9,6 +9,7 @@ import static org.mockito.Mockito.verify; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import com.bumptech.glide.request.target.SizeReadyCallback; import com.bumptech.glide.request.target.Target; import java.util.ArrayList; @@ -36,7 +37,7 @@ public class ListPreloaderTest { @Mock private RequestManager requestManager; @Before - public void setUp() throws Exception { + public void setUp() { MockitoAnnotations.initMocks(this); } @@ -73,7 +74,7 @@ public void testGetItemsIsCalledInOrderIncreasing() { private int expectedPosition; @Override - public int[] getPreloadSize(Object item, int adapterPosition, int itemPosition) { + public int[] getPreloadSize(@NonNull Object item, int adapterPosition, int itemPosition) { return new int[] { 10, 10 }; } @@ -83,10 +84,10 @@ public List getPreloadItems(int position) { return objects.subList(position - 11, position + 1 - 11); } - @NonNull + @Nullable @Override @SuppressWarnings("unchecked") - public RequestBuilder getPreloadRequestBuilder(Object item) { + public RequestBuilder getPreloadRequestBuilder(@NonNull Object item) { assertEquals(objects.get(expectedPosition), item); expectedPosition++; return mock(RequestBuilder.class); @@ -134,7 +135,7 @@ public void testGetItemsIsCalledInOrderDecreasing() { private int expectedPosition = toPreload - 1; @Override - public int[] getPreloadSize(Object item, int adapterPosition, int itemPosition) { + public int[] getPreloadSize(@NonNull Object item, int adapterPosition, int itemPosition) { return new int[] { 10, 10 }; } @@ -147,10 +148,10 @@ public List getPreloadItems(int position) { return objects.subList(position, position + 1); } - @NonNull + @Nullable @Override @SuppressWarnings("unchecked") - public RequestBuilder getPreloadRequestBuilder(Object item) { + public RequestBuilder getPreloadRequestBuilder(@NonNull Object item) { assertEquals(objects.get(expectedPosition), item); expectedPosition--; return mock(RequestBuilder.class); @@ -253,7 +254,7 @@ public List getPreloadItems(int position) { } @Test - public void testMultipleItemsForPositionIncreasing() throws NoSuchFieldException { + public void testMultipleItemsForPositionIncreasing() { final List objects = new ArrayList<>(); objects.add(new Object()); objects.add(new Object()); @@ -267,16 +268,16 @@ public List getPreloadItems(int position) { } @Override - public int[] getPreloadSize(Object item, int adapterPosition, int itemPosition) { + public int[] getPreloadSize(@NonNull Object item, int adapterPosition, int itemPosition) { assertEquals(expectedPosition / 2, adapterPosition); assertEquals(expectedPosition % 2, itemPosition); expectedPosition++; return itemPosition == 0 ? new int[] { 10, 11 } : new int[] { 20, 21 }; } - @NonNull + @Nullable @Override - public RequestBuilder getPreloadRequestBuilder(Object item) { + public RequestBuilder getPreloadRequestBuilder(@NonNull Object item) { return request; } }; @@ -291,7 +292,7 @@ public RequestBuilder getPreloadRequestBuilder(Object item) { } @Test - public void testMultipleItemsForPositionDecreasing() throws NoSuchFieldException { + public void testMultipleItemsForPositionDecreasing() { final List objects = new ArrayList<>(); objects.add(new Object()); objects.add(new Object()); @@ -305,16 +306,16 @@ public List getPreloadItems(int position) { } @Override - public int[] getPreloadSize(Object item, int adapterPosition, int itemPosition) { + public int[] getPreloadSize(@NonNull Object item, int adapterPosition, int itemPosition) { assertEquals(expectedPosition / 2, adapterPosition); assertEquals(expectedPosition % 2, itemPosition); expectedPosition--; return itemPosition == 0 ? new int[] { 10, 11 } : new int[] { 20, 21 }; } - @NonNull + @Nullable @Override - public RequestBuilder getPreloadRequestBuilder(Object item) { + public RequestBuilder getPreloadRequestBuilder(@NonNull Object item) { return request; } }; @@ -358,9 +359,9 @@ public List getPreloadItems(int position) { return objects.subList(position - 11, position - 10); } - @NonNull + @Nullable @Override - public RequestBuilder getPreloadRequestBuilder(Object item) { + public RequestBuilder getPreloadRequestBuilder(@NonNull Object item) { loadedObjects.add(item); return super.getPreloadRequestBuilder(item); } @@ -386,15 +387,16 @@ public List getPreloadItems(int position) { return result; } - @NonNull + @Nullable @Override @SuppressWarnings("unchecked") - public RequestBuilder getPreloadRequestBuilder(Object item) { + public RequestBuilder getPreloadRequestBuilder(@NonNull Object item) { return mock(RequestBuilder.class); } + @Nullable @Override - public int[] getPreloadSize(Object item, int adapterPosition, int itemPosition) { + public int[] getPreloadSize(@NonNull Object item, int adapterPosition, int itemPosition) { return new int[] { 100, 100 }; } } diff --git a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java index fa6bc110c0..65bc1ceabc 100644 --- a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java +++ b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java @@ -5,6 +5,7 @@ import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; @@ -78,7 +79,7 @@ public View onCreateView( final int gridMargin = getResources().getDimensionPixelOffset(R.dimen.grid_margin); int spanCount = getResources().getDisplayMetrics().widthPixels / (photoSize + (2 * gridMargin)); - grid = (RecyclerView) result.findViewById(R.id.flickr_photo_grid); + grid = result.findViewById(R.id.flickr_photo_grid); layoutManager = new GridLayoutManager(getActivity(), spanCount); grid.setLayoutManager(layoutManager); @@ -199,9 +200,9 @@ public List getPreloadItems(int position) { return photos.subList(position, position + 1); } - @NonNull + @Nullable @Override - public RequestBuilder getPreloadRequestBuilder(Photo item) { + public RequestBuilder getPreloadRequestBuilder(@NonNull Photo item) { return preloadRequest.load(item); } } diff --git a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java index 9737fc245b..a9423c0912 100644 --- a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java +++ b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java @@ -8,6 +8,7 @@ import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; @@ -58,7 +59,7 @@ public View onCreateView( @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View result = inflater.inflate(R.layout.flickr_photo_list, container, false); - list = (RecyclerView) result.findViewById(R.id.flickr_photo_list); + list = result.findViewById(R.id.flickr_photo_list); layoutManager = new LinearLayoutManager(getActivity()); list.setLayoutManager(layoutManager); adapter = new FlickrPhotoListAdapter(); @@ -172,9 +173,9 @@ public List getPreloadItems(int position) { return photos.subList(position, position + 1); } - @NonNull + @Nullable @Override - public RequestBuilder getPreloadRequestBuilder(Photo item) { + public RequestBuilder getPreloadRequestBuilder(@NonNull Photo item) { return fullRequest.thumbnail(thumbRequest.load(item)).load(item); } } @@ -185,8 +186,8 @@ private static final class PhotoTitleViewHolder extends RecyclerView.ViewHolder PhotoTitleViewHolder(View itemView) { super(itemView); - imageView = (ImageView) itemView.findViewById(R.id.photo_view); - titleView = (TextView) itemView.findViewById(R.id.title_view); + imageView = itemView.findViewById(R.id.photo_view); + titleView = itemView.findViewById(R.id.title_view); } } } diff --git a/samples/gallery/src/main/java/com/bumptech/glide/samples/gallery/RecyclerAdapter.java b/samples/gallery/src/main/java/com/bumptech/glide/samples/gallery/RecyclerAdapter.java index 4b81fd51ea..5b4b9330eb 100644 --- a/samples/gallery/src/main/java/com/bumptech/glide/samples/gallery/RecyclerAdapter.java +++ b/samples/gallery/src/main/java/com/bumptech/glide/samples/gallery/RecyclerAdapter.java @@ -3,6 +3,7 @@ import android.content.Context; import android.graphics.Point; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.view.Display; import android.view.LayoutInflater; @@ -99,9 +100,9 @@ public List getPreloadItems(int position) { return Collections.singletonList(data.get(position)); } - @NonNull + @Nullable @Override - public RequestBuilder getPreloadRequestBuilder(MediaStoreData item) { + public RequestBuilder getPreloadRequestBuilder(@NonNull MediaStoreData item) { MediaStoreSignature signature = new MediaStoreSignature(item.mimeType, item.dateModified, item.orientation); return requestBuilder @@ -110,8 +111,10 @@ public RequestBuilder getPreloadRequestBuilder(MediaStoreData item) .load(item.uri); } + @Nullable @Override - public int[] getPreloadSize(MediaStoreData item, int adapterPosition, int perItemPosition) { + public int[] getPreloadSize(@NonNull MediaStoreData item, int adapterPosition, + int perItemPosition) { return actualDimensions; } @@ -135,7 +138,7 @@ static final class ListViewHolder extends RecyclerView.ViewHolder { ListViewHolder(View itemView) { super(itemView); - image = (ImageView) itemView.findViewById(R.id.image); + image = itemView.findViewById(R.id.image); } } } diff --git a/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java b/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java index 9b7373834c..947518ccaa 100644 --- a/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java +++ b/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java @@ -8,6 +8,7 @@ import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.RecyclerListener; @@ -35,13 +36,13 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - ImageView giphyLogoView = (ImageView) findViewById(R.id.giphy_logo_view); + ImageView giphyLogoView = findViewById(R.id.giphy_logo_view); GlideApp.with(this) .load(R.raw.large_giphy_logo) .into(giphyLogoView); - RecyclerView gifList = (RecyclerView) findViewById(R.id.gif_list); + RecyclerView gifList = findViewById(R.id.gif_list); LinearLayoutManager layoutManager = new LinearLayoutManager(this); gifList.setLayoutManager(layoutManager); @@ -158,9 +159,9 @@ public List getPreloadItems(int position) { return Collections.singletonList(results[position]); } - @NonNull + @Nullable @Override - public RequestBuilder getPreloadRequestBuilder(Api.GifResult item) { + public RequestBuilder getPreloadRequestBuilder(@NonNull Api.GifResult item) { return requestBuilder.load(item); } } @@ -170,7 +171,7 @@ private static class GifViewHolder extends RecyclerView.ViewHolder { GifViewHolder(View itemView) { super(itemView); - gifView = (ImageView) itemView.findViewById(R.id.gif_view); + gifView = itemView.findViewById(R.id.gif_view); } } }