Skip to content

Commit

Permalink
Improve Kotlin support for RecyclerView module (#2670)
Browse files Browse the repository at this point in the history
* Improve Kotlin support for RecyclerView module with nullability annotations

Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX authored and sjudd committed Dec 5, 2017
1 parent 6778396 commit dc12b60
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,38 +35,42 @@ public final class RecyclerViewPreloader<T> extends RecyclerView.OnScrollListene
/**
* Helper constructor that accepts an {@link Activity}.
*/
public RecyclerViewPreloader(Activity activity,
PreloadModelProvider<T> preloadModelProvider,
PreloadSizeProvider<T> preloadDimensionProvider, int maxPreload) {
public RecyclerViewPreloader(@NonNull Activity activity,
@NonNull PreloadModelProvider<T> preloadModelProvider,
@NonNull PreloadSizeProvider<T> preloadDimensionProvider, int maxPreload) {
this(Glide.with(activity), preloadModelProvider, preloadDimensionProvider, maxPreload);
}

/**
* Helper constructor that accepts an {@link FragmentActivity}.
*/
public RecyclerViewPreloader(FragmentActivity fragmentActivity,
PreloadModelProvider<T> preloadModelProvider, PreloadSizeProvider<T> preloadDimensionProvider,
public RecyclerViewPreloader(@NonNull FragmentActivity fragmentActivity,
@NonNull PreloadModelProvider<T> preloadModelProvider,
@NonNull PreloadSizeProvider<T> preloadDimensionProvider,
int maxPreload) {
this(Glide.with(fragmentActivity), preloadModelProvider, preloadDimensionProvider, maxPreload);
}

/**
* Helper constructor that accepts an {@link Fragment}.
*/
public RecyclerViewPreloader(Fragment fragment,
PreloadModelProvider<T> preloadModelProvider, PreloadSizeProvider<T> preloadDimensionProvider,
public RecyclerViewPreloader(@NonNull Fragment fragment,
@NonNull PreloadModelProvider<T> preloadModelProvider,
@NonNull PreloadSizeProvider<T> preloadDimensionProvider,
int maxPreload) {
this(Glide.with(fragment), preloadModelProvider, preloadDimensionProvider, maxPreload);
}

/**
* Helper constructor that accepts an {@link android.support.v4.app.Fragment}.
*/
public RecyclerViewPreloader(android.support.v4.app.Fragment fragment,
PreloadModelProvider<T> preloadModelProvider, PreloadSizeProvider<T> preloadDimensionProvider,
public RecyclerViewPreloader(@NonNull android.support.v4.app.Fragment fragment,
@NonNull PreloadModelProvider<T> preloadModelProvider,
@NonNull PreloadSizeProvider<T> 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.
Expand All @@ -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<T> preloadModelProvider,
PreloadSizeProvider<T> preloadDimensionProvider, int maxPreload) {
public RecyclerViewPreloader(@NonNull RequestManager requestManager,
@NonNull PreloadModelProvider<T> preloadModelProvider,
@NonNull PreloadSizeProvider<T> preloadDimensionProvider, int maxPreload) {

ListPreloader<T> listPreloader = new ListPreloader<>(requestManager, preloadModelProvider,
preloadDimensionProvider, maxPreload);
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 7 additions & 6 deletions library/src/main/java/com/bumptech/glide/ListPreloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public interface PreloadModelProvider<U> {
* @param item The model to load.
*/
@Nullable
RequestBuilder<?> getPreloadRequestBuilder(U item);
RequestBuilder<?> getPreloadRequestBuilder(@NonNull U item);
}

/**
Expand All @@ -107,7 +107,7 @@ public interface PreloadSizeProvider<T> {
* @param item A model
*/
@Nullable
int[] getPreloadSize(T item, int adapterPosition, int perItemPosition);
int[] getPreloadSize(@NonNull T item, int adapterPosition, int perItemPosition);
}

/**
Expand All @@ -119,8 +119,9 @@ public interface PreloadSizeProvider<T> {
* @param preloadDimensionProvider Provides the dimensions of images to load.
* @param maxPreload Maximum number of items to preload.
*/
public ListPreloader(RequestManager requestManager, PreloadModelProvider<T> preloadModelProvider,
PreloadSizeProvider<T> preloadDimensionProvider, int maxPreload) {
public ListPreloader(@NonNull RequestManager requestManager,
@NonNull PreloadModelProvider<T> preloadModelProvider,
@NonNull PreloadSizeProvider<T> preloadDimensionProvider, int maxPreload) {
this.requestManager = requestManager;
this.preloadModelProvider = preloadModelProvider;
this.preloadDimensionProvider = preloadDimensionProvider;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bumptech.glide.util;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.bumptech.glide.ListPreloader;

/**
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
42 changes: 22 additions & 20 deletions library/src/test/java/com/bumptech/glide/ListPreloaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,7 +37,7 @@ public class ListPreloaderTest {
@Mock private RequestManager requestManager;

@Before
public void setUp() throws Exception {
public void setUp() {
MockitoAnnotations.initMocks(this);
}

Expand Down Expand Up @@ -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 };
}

Expand All @@ -83,10 +84,10 @@ public List<Object> getPreloadItems(int position) {
return objects.subList(position - 11, position + 1 - 11);
}

@NonNull
@Nullable
@Override
@SuppressWarnings("unchecked")
public RequestBuilder<Object> getPreloadRequestBuilder(Object item) {
public RequestBuilder<Object> getPreloadRequestBuilder(@NonNull Object item) {
assertEquals(objects.get(expectedPosition), item);
expectedPosition++;
return mock(RequestBuilder.class);
Expand Down Expand Up @@ -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 };
}

Expand All @@ -147,10 +148,10 @@ public List<Object> getPreloadItems(int position) {
return objects.subList(position, position + 1);
}

@NonNull
@Nullable
@Override
@SuppressWarnings("unchecked")
public RequestBuilder<Object> getPreloadRequestBuilder(Object item) {
public RequestBuilder<Object> getPreloadRequestBuilder(@NonNull Object item) {
assertEquals(objects.get(expectedPosition), item);
expectedPosition--;
return mock(RequestBuilder.class);
Expand Down Expand Up @@ -253,7 +254,7 @@ public List<Object> getPreloadItems(int position) {
}

@Test
public void testMultipleItemsForPositionIncreasing() throws NoSuchFieldException {
public void testMultipleItemsForPositionIncreasing() {
final List<Object> objects = new ArrayList<>();
objects.add(new Object());
objects.add(new Object());
Expand All @@ -267,16 +268,16 @@ public List<Object> 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<Object> getPreloadRequestBuilder(Object item) {
public RequestBuilder<Object> getPreloadRequestBuilder(@NonNull Object item) {
return request;
}
};
Expand All @@ -291,7 +292,7 @@ public RequestBuilder<Object> getPreloadRequestBuilder(Object item) {
}

@Test
public void testMultipleItemsForPositionDecreasing() throws NoSuchFieldException {
public void testMultipleItemsForPositionDecreasing() {
final List<Object> objects = new ArrayList<>();
objects.add(new Object());
objects.add(new Object());
Expand All @@ -305,16 +306,16 @@ public List<Object> 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<Object> getPreloadRequestBuilder(Object item) {
public RequestBuilder<Object> getPreloadRequestBuilder(@NonNull Object item) {
return request;
}
};
Expand Down Expand Up @@ -358,9 +359,9 @@ public List<Object> getPreloadItems(int position) {
return objects.subList(position - 11, position - 10);
}

@NonNull
@Nullable
@Override
public RequestBuilder<Object> getPreloadRequestBuilder(Object item) {
public RequestBuilder<Object> getPreloadRequestBuilder(@NonNull Object item) {
loadedObjects.add(item);
return super.getPreloadRequestBuilder(item);
}
Expand All @@ -386,15 +387,16 @@ public List<Object> getPreloadItems(int position) {
return result;
}

@NonNull
@Nullable
@Override
@SuppressWarnings("unchecked")
public RequestBuilder<Object> getPreloadRequestBuilder(Object item) {
public RequestBuilder<Object> 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 };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -199,9 +200,9 @@ public List<Photo> getPreloadItems(int position) {
return photos.subList(position, position + 1);
}

@NonNull
@Nullable
@Override
public RequestBuilder<Drawable> getPreloadRequestBuilder(Photo item) {
public RequestBuilder<Drawable> getPreloadRequestBuilder(@NonNull Photo item) {
return preloadRequest.load(item);
}
}
Expand Down
Loading

0 comments on commit dc12b60

Please sign in to comment.