Skip to content

Commit

Permalink
Create DiffRVRendererAdapter to use RecyclerView's DiffUtil (#36)
Browse files Browse the repository at this point in the history
* Update support library in order to add the DiffUtil, also update gradle version

* Add DiffRVRendererAdapter class to take advantage of the DiffUtil in our RendererAdapters

* Implement the Diff

* Create a renderer with the hability of remove items from the list in order to show the Diff animation

* Fix checkstyle and change android sdk version on travis

* Fix lint

* Remove DiffRVRendererAdapter and move update to RVRendererAdapter then fix the sample project

* Rename the class RemoveItemCallback to RemovableVideoRenderer.Listener

* Rename the method removeItem in RemoveItemCallback to onRemoveButtonTapped

* Add a documentation comment for our new diffUpdate method

* Override hashCode in order to fix the checkstyle
  • Loading branch information
FireZenk authored and pedrovgs committed May 1, 2017
1 parent fc7297d commit 84a9818
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 18 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ language: android
android:
components:
- tools
- build-tools-23.0.2
- android-23
- build-tools-25.0.2
- android-25
- extra-android-support
- extra-google-m2repository
- extra-android-m2repository

script:
script:
- ./gradlew checkstyle build

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:2.3.1'
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Jan 03 17:21:12 CET 2016
#Fri Apr 07 09:29:52 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
11 changes: 7 additions & 4 deletions renderers/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apply plugin: 'com.android.library'
apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 25
buildToolsVersion '25.0.2'

defaultConfig {
minSdkVersion 10
targetSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand All @@ -16,11 +16,14 @@ android {
minifyEnabled false
}
}
lintOptions {
abortOnError false
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
testCompile 'org.robolectric:robolectric:2.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package com.pedrogomez.renderers;

import android.support.v7.util.DiffUtil;
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
Expand Down Expand Up @@ -187,4 +189,70 @@ protected AdapteeCollection<T> getCollection() {
protected void updateRendererExtraValues(T content, Renderer<T> renderer, int position) {

}

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

this.clear();
this.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();
}
}
}
11 changes: 7 additions & 4 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 25
buildToolsVersion '25.0.2'

defaultConfig {
applicationId "com.github.pedrovgs.renderers.sample"
minSdkVersion 14
targetSdkVersion 23
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand All @@ -16,11 +16,14 @@ android {
minifyEnabled false
}
}
lintOptions {
abortOnError false
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile project(':renderers')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,21 @@ public boolean isLive() {
public void setLive(boolean live) {
this.live = live;
}

@Override public boolean equals(Object obj) {
if (obj instanceof Video) {
Video other = (Video) obj;
return title.equals(other.title)
&& thumbnail.equals(other.thumbnail)
&& favorite == other.favorite
&& liked == other.liked
&& live == other.live;
} else {
return false;
}
}

@Override public int hashCode() {
return super.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import com.pedrogomez.renderers.sample.ui.renderers.FavoriteVideoRenderer;
import com.pedrogomez.renderers.sample.ui.renderers.LikeVideoRenderer;
import com.pedrogomez.renderers.sample.ui.renderers.LiveVideoRenderer;
import com.pedrogomez.renderers.sample.ui.renderers.RemovableVideoRenderer;

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

Expand Down Expand Up @@ -58,11 +62,17 @@ public class RecyclerViewActivity extends BaseActivity {
private void initAdapter() {
RandomVideoCollectionGenerator randomVideoCollectionGenerator =
new RandomVideoCollectionGenerator();
AdapteeCollection<Video> videoCollection =
final AdapteeCollection<Video> videoCollection =
randomVideoCollectionGenerator.generateListAdapteeVideoCollection(VIDEO_COUNT);
RendererBuilder<Video> rendererBuilder = new RendererBuilder<Video>()
.withPrototype(new LikeVideoRenderer())
.bind(Video.class, LikeVideoRenderer.class);
.withPrototype(new RemovableVideoRenderer(new RemovableVideoRenderer.Listener() {
@Override public void onRemoveButtonTapped(Video video) {
ArrayList<Video> clonedList = new ArrayList<>((Collection<? extends Video>) videoCollection);
clonedList.remove(video);
adapter.diffUpdate(clonedList);
}
}))
.bind(Video.class, RemovableVideoRenderer.class);

adapter = new RVRendererAdapter<Video>(rendererBuilder, videoCollection);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.pedrogomez.renderers.sample.ui.renderers;

import com.pedrogomez.renderers.sample.R;
import com.pedrogomez.renderers.sample.model.Video;

import butterknife.OnClick;

public class RemovableVideoRenderer extends VideoRenderer {

private Listener removeItemListener;

public interface Listener {

void onRemoveButtonTapped(Video video);
}

public RemovableVideoRenderer(Listener removeItemListener) {
this.removeItemListener = removeItemListener;
}

@Override protected void renderLabel() {
String deleteLabel = getContext().getString(R.string.delete_label);
getLabel().setText(deleteLabel);
}

@Override protected void renderMarker(Video video) {

}

@OnClick(R.id.tv_label) void clickOnDelete() {
removeItemListener.onRemoveButtonTapped(getContent());
}

@OnClick(R.id.iv_marker) void clickOnLike() {
getContent().setLiked(!getContent().isLiked());
renderMarker(getContent());
}
}
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<string name="like_label">Like</string>
<string name="dislike_label">Dislike</string>
<string name="live_label">Live</string>
<string name="delete_label">Delete me</string>
<string name="open_lv_sample">Open ListView sample</string>
<string name="open_rv_sample">Open RecyclerView sample</string>

Expand Down

0 comments on commit 84a9818

Please sign in to comment.