Skip to content

Commit

Permalink
Clean diff feature before to publish the new release
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrovgs committed May 1, 2017
1 parent 84a9818 commit 93a2dba
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 87 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ private void initListView() {

**Remember if you are going to use ``RecyclerView`` instead of ``ListView`` you'll have to use ``RVRendererAdapter`` instead of ``RendererAdapter``.**

* 4. Diff updates.

If the ``RecyclerView`` performance is crucial in your application remember you can use ``diffUpdate`` method in your ``RVRendererAdapter`` instance to update just the items changed in your adapter and not the whole list.

```java
adapter.diffUpdate(newList)
```

This method provides a ready to use diff update for our adapter based on the implementation of the standard ``equals`` method from ``Object`` class.

Usage
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.pedrogomez.renderers;

import android.support.v7.util.DiffUtil;

import java.util.List;

class DiffCallbacks<T> extends DiffUtil.Callback {

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

private int oldItemPosition;

private boolean deep;

DiffCallbacks(AdapteeCollection<T> oldList, List<T> newList) {
this.oldList = oldList;
this.newList = newList;
}

@Override public int getOldListSize() {
return oldList.size();
}

@Override public int getNewListSize() {
return newList.size();
}

@Override public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
this.deep = false;
this.oldItemPosition = oldItemPosition;
return equals(newList.get(newItemPosition));
}

@Override public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
this.deep = true;
this.oldItemPosition = oldItemPosition;
return equals(newList.get(newItemPosition));
}

@Override public boolean equals(Object newItem) {
Object current = oldList.get(oldItemPosition);
if (deep) {
return newItem.equals(current);
} else {
return newItem.getClass().equals(current.getClass());
}
}

@Override public int hashCode() {
return super.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.pedrogomez.renderers.exception.NullRendererBuiltException;

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

/**
* RecyclerView.Adapter extension created to work RendererBuilders and Renderer instances. Other
* adapters have to use this one to show information into RecyclerView widgets.
*
* <p>
* This class is the heart of this library. It's used to avoid the library users declare a new
* renderer each time they have to show information into a RecyclerView.
*
* <p>
* RVRendererAdapter has to be constructed with a LayoutInflater to inflate views, one
* RendererBuilder to provide Renderer to RVRendererAdapter and one AdapteeCollection to
* provide the elements to render.
Expand Down Expand Up @@ -63,14 +65,14 @@ public T getItem(int position) {
}

public void setCollection(AdapteeCollection<T> collection) {
if (collection == null) {
throw new IllegalArgumentException("The AdapteeCollection configured can't be null");
}
if (collection == null) {
throw new IllegalArgumentException("The AdapteeCollection configured can't be null");
}

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

/**
/**
* Indicate to the RecyclerView the type of Renderer used to one position using a numeric value.
*
* @param position to analyze.
Expand Down Expand Up @@ -178,7 +180,7 @@ protected AdapteeCollection<T> getCollection() {
/**
* Empty implementation created to allow the client code to extend this class without override
* getView method.
*
* <p>
* This method is called before render the Renderer and can be used in RendererAdapter extension
* to add extra info to the renderer created like the position in the ListView/RecyclerView.
*
Expand All @@ -192,67 +194,20 @@ protected void updateRendererExtraValues(T content, Renderer<T> renderer, int po

/**
* Provides a ready to use diff update for our adapter based on the implementation of the
* standard equals method from Object
* standard equals method from Object.
*
* @param newList to refresh our content
*/
public void diffUpdate(List<T> newList) {
if (getCollection().size() == 0) {
this.addAll(newList);
addAll(newList);
notifyDataSetChanged();
} else {
DiffCallbacks diffCallbacks = new DiffCallbacks(newList);
DiffCallbacks diffCallbacks = new DiffCallbacks(collection, newList);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallbacks);

this.clear();
this.addAll(newList);
clear();
addAll(newList);
diffResult.dispatchUpdatesTo(this);
}
}

private class DiffCallbacks extends DiffUtil.Callback {

private final List<T> newList;

private int oldItemPosition;

private boolean deep;

DiffCallbacks(List<T> newList) {
this.newList = newList;
}

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

@Override public int getNewListSize() {
return newList.size();
}

@Override public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
this.deep = false;
this.oldItemPosition = oldItemPosition;
return equals(newList.get(newItemPosition));
}

@Override public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
this.deep = true;
this.oldItemPosition = oldItemPosition;
return equals(newList.get(newItemPosition));
}

@Override public boolean equals(Object newItem) {
Object current = collection.get(oldItemPosition);
if (deep) {
return newItem.equals(current);
} else {
return newItem.getClass().equals(current.getClass());
}
}

@Override public int hashCode() {
return super.hashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ private void initAdapter() {
new RandomVideoCollectionGenerator();
final AdapteeCollection<Video> videoCollection =
randomVideoCollectionGenerator.generateListAdapteeVideoCollection(VIDEO_COUNT);
RendererBuilder<Video> rendererBuilder = new RendererBuilder<Video>()
.withPrototype(new RemovableVideoRenderer(new RemovableVideoRenderer.Listener() {
RendererBuilder<Video> rendererBuilder = new RendererBuilder<Video>().withPrototype(
new RemovableVideoRenderer(new RemovableVideoRenderer.Listener() {
@Override public void onRemoveButtonTapped(Video video) {
ArrayList<Video> clonedList = new ArrayList<>((Collection<? extends Video>) videoCollection);
ArrayList<Video> clonedList =
new ArrayList<>((Collection<? extends Video>) videoCollection);
clonedList.remove(video);
adapter.diffUpdate(clonedList);
}
}))
.bind(Video.class, RemovableVideoRenderer.class);
})).bind(Video.class, RemovableVideoRenderer.class);

adapter = new RVRendererAdapter<Video>(rendererBuilder, videoCollection);
adapter = new RVRendererAdapter<>(rendererBuilder, videoCollection);
}

/**
Expand All @@ -84,25 +84,4 @@ private void initRecyclerView() {
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}

/**
* Create a list of prototypes to configure RendererBuilder.
* The list of Renderer<Video> that contains all the possible renderers that our RendererBuilder
* is going to use.
*
* @return Renderer<Video> prototypes for RendererBuilder.
*/
private List<Renderer<Video>> getRendererVideoPrototypes() {
List<Renderer<Video>> prototypes = new LinkedList<Renderer<Video>>();
LikeVideoRenderer likeVideoRenderer = new LikeVideoRenderer();
prototypes.add(likeVideoRenderer);

FavoriteVideoRenderer favoriteVideoRenderer = new FavoriteVideoRenderer();
prototypes.add(favoriteVideoRenderer);

LiveVideoRenderer liveVideoRenderer = new LiveVideoRenderer();
prototypes.add(liveVideoRenderer);

return prototypes;
}
}

0 comments on commit 93a2dba

Please sign in to comment.