Skip to content

Commit

Permalink
Change the use of AdapteeCollection to List (#58)
Browse files Browse the repository at this point in the history
* Opening new brach

* Deprecate AdapteeCollection in favor of List as type of collection inside adapters

* Provide DiffCallback constructor with AdapteeCollection.

* Fix checkstyles

* Revert unnecessary bump of recyclerview

Co-authored-by: ruben.abadsanchez <[email protected]>
  • Loading branch information
Lukard and Lukard authored Jun 13, 2021
1 parent 9ddc156 commit 2018395
Show file tree
Hide file tree
Showing 22 changed files with 496 additions and 275 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ RendererBuilder<Video> rendererBuilder = new RendererBuilder<Video>()
.bind(Video.class, new LikeVideoRenderer());
```

* 3. Initialize your ``ListView`` or ``RecyclerView`` with your ``RendererBuilder`` and your ``AdapteeCollection`` instances inside your Activity or Fragment. **You can use ``ListAdapteeCollection`` or create your own implementation creating a class which implements ``AdapteeCollection`` to configure your ``RendererAdapter`` or ``RVRendererAdapter``.**
* 3. Initialize your ``ListView`` or ``RecyclerView`` with your ``RendererBuilder`` and an optional ``List`` inside your Activity or Fragment. **You should provide a list of items to configure your ``RendererAdapter`` or ``RVRendererAdapter``.**

```java
private void initListView() {
adapter = new RendererAdapter<Video>(rendererBuilder, adapteeCollection);
adapter = new RendererAdapter<Video>(rendererBuilder, list);
listView.setAdapter(adapter);
}
```
Expand All @@ -93,7 +93,7 @@ or

```java
private void initListView() {
adapter = new RVRendererAdapter<Video>(rendererBuilder, adapteeCollection);
adapter = new RVRendererAdapter<Video>(rendererBuilder, list);
recyclerView.setAdapter(adapter);
}
```
Expand Down Expand Up @@ -123,7 +123,7 @@ Add this dependency to your ``build.gradle``:

```groovy
dependencies{
implementation 'com.github.pedrovgs:renderers:4.0.0'
implementation 'com.github.pedrovgs:renderers:4.0.2'
}
```

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ POM_NAME=Renderers
POM_ARTIFACT_ID=renderers
POM_PACKAGING=aar

VERSION_NAME=4.0.1-SNAPSHOT
VERSION_CODE=040001
VERSION_NAME=4.0.2-SNAPSHOT
VERSION_CODE=040002
GROUP=com.github.pedrovgs

POM_DESCRIPTION=Android library to avoid duplicated adapters code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.util.Collection;

/**
* @deprecated Adapters collections are provided through List class. This class has no purpose
* anymore. This class is going to be removed in upcoming version.
*
* Interface created to represent the adaptee collection used in RendererAdapter and
* RVRendererAdapter. RendererAdapter and RVRendererAdapter will be created with a RendererBuilder
* and an AdapteeCollection that store all the content to show in a ListView or RecyclerView
Expand All @@ -28,6 +31,7 @@
*
* @author Pedro Vicente Gómez Sánchez.
*/
@Deprecated
public interface AdapteeCollection<T> {

/**
Expand Down
19 changes: 17 additions & 2 deletions renderers/src/main/java/com/pedrogomez/renderers/DiffCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,25 @@

class DiffCallback<T> extends DiffUtil.Callback {

private final AdapteeCollection<T> oldList;
private final List<T> oldList;
private final List<T> newList;

/**
* @deprecated Use {@link #DiffCallback(List, List)} function instead.
* This constructor is going to be removed in upcoming version.
*/
@Deprecated
DiffCallback(AdapteeCollection<T> oldList, List<T> newList) {
try {
this.oldList = (List) oldList;
} catch (ClassCastException exception) {
throw new ClassCastException("oldList parameter needs to implement List interface. "
+ "AdapteeCollection has been deprecated and will disappear in upcoming version");
}
this.newList = newList;
}

DiffCallback(List<T> oldList, List<T> newList) {
this.oldList = oldList;
this.newList = newList;
}
Expand All @@ -49,4 +64,4 @@ class DiffCallback<T> extends DiffUtil.Callback {
@Override public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldList.get(oldItemPosition).equals(newList.get(newItemPosition));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
import java.util.List;

/**
* @deprecated Adapters collections are provided through List class. This class has no purpose
* anymore. This class is going to be removed in upcoming version.
*
* Generic AdapteeCollection implementation based on ArrayList. Library clients can use this
* class instead of create his own AdapteeCollections.
*
* @author Pedro Vicente Gómez Sánchez.
*/
@Deprecated
public class ListAdapteeCollection<T> extends ArrayList<T> implements AdapteeCollection<T> {

public ListAdapteeCollection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.pedrogomez.renderers.exception.NullRendererBuiltException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

Expand All @@ -30,28 +31,62 @@
public class RVListRendererAdapter<T> extends ListAdapter<T, RendererViewHolder> {

private final RendererBuilder<T> rendererBuilder;
private AdapteeCollection<T> collection;
private List<T> list;

public RVListRendererAdapter(RendererBuilder<T> rendererBuilder) {
this(rendererBuilder, new DefaultDiffUtilItemCallback<T>(), new ListAdapteeCollection<T>());
this(rendererBuilder, new DefaultDiffUtilItemCallback<T>(), new ArrayList<T>());
}

/**
* @deprecated Use {@link #RVListRendererAdapter(RendererBuilder, List)} function instead.
* This constructor is going to be removed in upcoming version.
*/
@Deprecated
public RVListRendererAdapter(RendererBuilder<T> rendererBuilder, AdapteeCollection<T> collection) {
super(new DefaultDiffUtilItemCallback<T>());
this.rendererBuilder = rendererBuilder;
this.collection = collection;
try {
this.list = (List) collection;
} catch (ClassCastException exception) {
throw new ClassCastException("collection parameter needs to implement List interface. "
+ "AdapteeCollection has been deprecated and will disappear in upcoming version");
}
}

public RVListRendererAdapter(RendererBuilder<T> rendererBuilder, List<T> list) {
super(new DefaultDiffUtilItemCallback<T>());
this.rendererBuilder = rendererBuilder;
this.list = list;
}

public RVListRendererAdapter(RendererBuilder<T> rendererBuilder, @NonNull DiffUtil.ItemCallback diffCallback) {
this(rendererBuilder, diffCallback, new ListAdapteeCollection<T>());
this(rendererBuilder, diffCallback, new ArrayList<T>());
}

/**
* @deprecated Use RVListRendererAdapter(RendererBuilder, DiffUtil.ItemCallback, List) constructor instead.
* This constructor is going to be removed in upcoming version.
*/
@Deprecated
public RVListRendererAdapter(RendererBuilder<T> rendererBuilder,
@NonNull DiffUtil.ItemCallback diffCallback,
AdapteeCollection<T> collection) {
super(diffCallback);
this.rendererBuilder = rendererBuilder;
this.collection = collection;
try {
this.list = (List) collection;
} catch (ClassCastException exception) {
throw new ClassCastException("collection parameter needs to implement List interface. "
+ "AdapteeCollection has been deprecated and will disappear in upcoming version");
}
}

public RVListRendererAdapter(RendererBuilder<T> rendererBuilder,
@NonNull DiffUtil.ItemCallback diffCallback,
List<T> list) {
super(diffCallback);
this.rendererBuilder = rendererBuilder;
this.list = list;
}

public RVListRendererAdapter(RendererBuilder<T> rendererBuilder, @NonNull AsyncDifferConfig config) {
Expand All @@ -60,32 +95,46 @@ public RVListRendererAdapter(RendererBuilder<T> rendererBuilder, @NonNull AsyncD

public RVListRendererAdapter(RendererBuilder<T> rendererBuilder,
@NonNull AsyncDifferConfig config,
AdapteeCollection<T> collection) {
List<T> list) {
super(config);
this.rendererBuilder = rendererBuilder;
this.collection = collection;
this.list = list;
}

@Override
public int getItemCount() {
return collection.size();
return list.size();
}

public T getItem(int position) {
return collection.get(position);
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

/**
* @deprecated Use {@link #setList} function instead.
* This method is going to be removed in upcoming version.
*/
@Deprecated
public void setCollection(AdapteeCollection<T> collection) {
if (collection == null) {
throw new IllegalArgumentException("The AdapteeCollection configured can't be null");
try {
setList((List) collection);
} catch (ClassCastException exception) {
throw new ClassCastException("collection parameter needs to implement List interface. "
+ "AdapteeCollection has been deprecated and will disappear in upcoming version");
}
}

public void setList(List<T> list) {
if (list == null) {
throw new IllegalArgumentException("The List configured can't be null");
}

this.collection = collection;
this.list = list;
}

/**
Expand Down Expand Up @@ -147,7 +196,7 @@ public void onBindViewHolder(RendererViewHolder viewHolder, int position) {
* @return if the element has been added.
*/
public boolean add(T element) {
return collection.add(element);
return list.add(element);
}

/**
Expand All @@ -157,7 +206,7 @@ public boolean add(T element) {
* @return if the element has been removed.
*/
public boolean remove(Object element) {
return collection.remove(element);
return list.remove(element);
}

/**
Expand All @@ -167,7 +216,7 @@ public boolean remove(Object element) {
* @return if the elements have been added.
*/
public boolean addAll(Collection<? extends T> elements) {
return collection.addAll(elements);
return list.addAll(elements);
}

/**
Expand All @@ -177,23 +226,35 @@ public boolean addAll(Collection<? extends T> elements) {
* @return if the elements have been removed.
*/
public boolean removeAll(Collection<?> elements) {
return collection.removeAll(elements);
return list.removeAll(elements);
}

/**
* Remove all elements inside the AdapteeCollection.
*/
public void clear() {
collection.clear();
list.clear();
}

/**
* @deprecated Use {@link #getList()} function instead.
* This method is going to be removed in upcoming version.
*
* Allows the client code to access the AdapteeCollection from subtypes of RendererAdapter.
*
* @return collection used in the adapter as the adaptee class.
*/
protected AdapteeCollection<T> getCollection() {
return collection;
return new ListAdapteeCollection(getList());
}

/**
* Allows the client code to access the list from subtypes of RendererAdapter.
*
* @return collection used in the adapter as the adaptee class.
*/
protected List<T> getList() {
return list;
}

/**
Expand All @@ -220,7 +281,7 @@ protected void updateRendererExtraValues(T content, Renderer<T> renderer, int po
*/
@Override
public void submitList(@Nullable List<T> newList) {
if (getCollection().size() > 0) {
if (getList().size() > 0) {
clear();
}
addAll(newList);
Expand Down
Loading

0 comments on commit 2018395

Please sign in to comment.