Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
Reimplement AsyncDiffer#getItem() method
Browse files Browse the repository at this point in the history
- More docs.
- `ModularAdapter` no longer implements `ItemManager`.
- New `getItemCount()` method of `ItemManager` interface.
  • Loading branch information
belinwu committed Jan 25, 2019
1 parent a8923dc commit 1fa8d77
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private void checkDiffer(AsyncDiffer<E> differ) {
*
* @return The item count.
*/
@Override
public int getItemCount() {
return differ.getItemCount();
}
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/com/samelody/modapter/ItemManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public interface ItemManager<E> {
@Nullable
E getItem(int position);

/**
* Gets item count of current list.
*
* @return The item count of current list.
*/
int getItemCount();

/**
* Registers the item metadata.
*
Expand Down
16 changes: 15 additions & 1 deletion core/src/main/java/com/samelody/modapter/ItemViewHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* A holder with bound data item for item view.
*
* @param <T> The type of item.
* @param <T> The type of data item.
* @author Belin Wu
*/
public abstract class ItemViewHolder<T extends AdapterItem> extends ViewHolder {
Expand All @@ -27,18 +27,32 @@ void setItem(@Nullable T item) {
this.item = item;
}

/**
* Called when item view has been bound.
*
* @param item The data item bound within this view holder.
*/
protected void onViewBound(T item) {
// empty
}

/**
* Called when item view has been recycled.
*/
protected void onViewRecycled() {
// empty
}

/**
* Called when item view has been attached to a window.
*/
protected void onViewAttachedToWindow() {
// empty
}

/**
* Called when item view has been detached from its window.
*/
protected void onViewDetachedFromWindow() {
// empty
}
Expand Down
54 changes: 1 addition & 53 deletions core/src/main/java/com/samelody/modapter/ModularAdapter.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package com.samelody.modapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.ViewGroup;

import com.samelody.modapter.differ.AsyncDiffer;

import java.util.List;

/**
* A modular list adapter for presenting List data in a {@link RecyclerView}, including computing
* diffs between Lists on a background thread by {@link AsyncDiffer<E>}.
*
* @param <E> The type of elements in the list.
* @author Belin Wu
*/
public class ModularAdapter<E extends AdapterItem>
extends Adapter<ViewHolder>
implements ItemManager<E> {
public class ModularAdapter<E extends AdapterItem> extends Adapter<ViewHolder> {

/**
* The delegated implementation.
Expand Down Expand Up @@ -74,52 +69,5 @@ public final void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
public ItemManager<E> getManager() {
return delegate;
}

// implements ItemManager interface

@Override
public ItemManager<E> setDiffer(AsyncDiffer<E> differ) {
delegate.setDiffer(differ);
return delegate;
}

@NonNull
@Override
public ItemManager<E> submitList(List<? extends E> list) {
delegate.submitList(list);
return delegate;
}

@Override
public List<E> getCurrentList() {
return delegate.getCurrentList();
}

@Nullable
@Override
public E getItem(int position) {
return delegate.getItem(position);
}

@NonNull
@Override
public ItemManager<E> register(int layoutId, Class<? extends ViewHolder> holderClass) {
delegate.register(layoutId, holderClass);
return delegate;
}

@NonNull
@Override
public ItemManager<E> register(ItemMetadata metadata) {
delegate.register(metadata);
return delegate;
}

@NonNull
@Override
public ItemManager<E> unregister(int layoutId) {
delegate.unregister(layoutId);
return delegate;
}
}

21 changes: 21 additions & 0 deletions core/src/main/java/com/samelody/modapter/differ/AsyncDiffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,32 @@
*/
public interface AsyncDiffer<E> {

/**
* Submits the new list to be displayed.
*
* @param list The new list to be displayed.
*/
void submitList(@Nullable List<? extends E> list);

/**
* Get the item from the current list at the specified index.
*
* @param position The position of item.
* @return The item at the given position.
*/
E getItem(int position);

/**
* Gets item count of current list.
*
* @return The item count of current list.
*/
int getItemCount();

/**
* Gets the list currently being displayed by the Adapter.
*
* @return The list currently being displayed.
*/
List<E> getCurrentList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import android.support.annotation.Nullable;
import android.support.v7.recyclerview.extensions.AsyncDifferConfig;
import android.support.v7.recyclerview.extensions.AsyncListDiffer;
import android.support.v7.util.DiffUtil;
import android.support.v7.util.DiffUtil.ItemCallback;
import android.support.v7.util.ListUpdateCallback;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;

import java.util.List;

Expand All @@ -23,8 +23,8 @@ public class ListAsyncDiffer<E> implements AsyncDiffer<E> {
*/
private final AsyncListDiffer<E> differ;

public ListAsyncDiffer(@NonNull RecyclerView.Adapter adapter,
@NonNull DiffUtil.ItemCallback<E> callback) {
public ListAsyncDiffer(@NonNull Adapter adapter,
@NonNull ItemCallback<E> callback) {
differ = new AsyncListDiffer<>(adapter, callback);
}

Expand All @@ -33,17 +33,15 @@ public ListAsyncDiffer(@NonNull ListUpdateCallback callback,
differ = new AsyncListDiffer<>(callback, config);
}

@SuppressWarnings("unchecked")
@Override
public void submitList(@Nullable List<? extends E> list) {
differ.submitList((List<E>) list);
}

@Override
public E getItem(int position) {
if (position < 0 || position >= getCurrentList().size()) {
return null;
}
return getCurrentList().get(position);
return NonAsyncDiffer.getItem(this, position);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ public class NonAsyncDiffer<E> implements AsyncDiffer<E> {
@NonNull
private List<E> list = emptyList();

@SuppressWarnings("unchecked")
@Override
public void submitList(@Nullable List<? extends E> list) {
this.list = list == null ? Collections.<E>emptyList() : (List<E>) list;
}

@Override
public E getItem(int position) {
if (position < 0 || position >= list.size()) {
return null;
}
return list.get(position);
return getItem(this, position);
}

@Override
Expand All @@ -45,4 +43,26 @@ public int getItemCount() {
public List<E> getCurrentList() {
return unmodifiableList(list);
}

/**
* Gets the item with given position from given differ.
*
* @param differ The async differ.
* @param position The position of item.
* @param <E> The type of item.
* @return The item at given position.
*/
@Nullable
public static <E> E getItem(AsyncDiffer<E> differ, int position) {
if (differ == null) {
return null;
}

List<E> list = differ.getCurrentList();
if (list == null || position < 0 || position >= list.size()) {
return null;
}

return list.get(position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.recyclerview.extensions.AsyncDifferConfig;
import android.support.v7.util.DiffUtil;
import android.support.v7.util.DiffUtil.ItemCallback;
import android.support.v7.util.ListUpdateCallback;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;

import java.util.List;

Expand All @@ -24,16 +24,17 @@ public class PagedAsyncDiffer<E> implements AsyncDiffer<E> {
*/
private final AsyncPagedListDiffer<E> differ;

public PagedAsyncDiffer(@NonNull RecyclerView.Adapter adapter,
@NonNull DiffUtil.ItemCallback<E> callback) {
public PagedAsyncDiffer(@NonNull Adapter adapter,
@NonNull ItemCallback<E> callback) {
differ = new AsyncPagedListDiffer<>(adapter, callback);
}

public PagedAsyncDiffer(@NonNull ListUpdateCallback listUpdateCallback,
public PagedAsyncDiffer(@NonNull ListUpdateCallback callback,
@NonNull AsyncDifferConfig<E> config) {
differ = new AsyncPagedListDiffer<>(listUpdateCallback, config);
differ = new AsyncPagedListDiffer<>(callback, config);
}

@SuppressWarnings("unchecked")
@Override
public void submitList(@Nullable List<? extends E> list) {
if (list instanceof PagedList) {
Expand All @@ -45,13 +46,7 @@ public void submitList(@Nullable List<? extends E> list) {

@Override
public E getItem(int position) {
if (getCurrentList() == null) {
return null;
}
if (position < 0 || position >= getCurrentList().size()) {
return null;
}
return getCurrentList().get(position);
return NonAsyncDiffer.getItem(this, position);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ protected void onCreate(Bundle savedInstanceState) {
list.add(new ImageItem());
list.add(new ImageItem());
list.add(new ImageItem());
adapter.submitList(list);
manager.submitList(list);
adapter.notifyDataSetChanged();

manager.getCurrentList();
adapter.getItem(0);
manager.getItem(0);
}
}

0 comments on commit 1fa8d77

Please sign in to comment.