Skip to content

Commit

Permalink
add DiffUtil support, update gradle dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivchar Vitaly committed Mar 20, 2017
1 parent c739367 commit 0823d28
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.1'
compile "com.android.support:cardview-v7:25.1.1"
compile "com.android.support:percent:25.1.1"
compile 'com.android.support:appcompat-v7:25.3.0'
compile "com.android.support:cardview-v7:25.3.0"
compile "com.android.support:percent:25.3.0"
compile 'com.android.support:design:25.3.0'
testCompile 'junit:junit:4.12'
compile project(path: ':rendererrecyclerviewadapter')
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.github.vivchar.immutableadapter.items.content.ContentModel;
import com.github.vivchar.immutableadapter.items.content.ContentViewRenderer;
import com.github.vivchar.immutableadapter.items.BaseItemModel;
import com.github.vivchar.immutableadapter.items.category.CategoryModel;
import com.github.vivchar.immutableadapter.items.category.CategoryViewRenderer;
import com.github.vivchar.immutableadapter.items.content.ContentModel;
import com.github.vivchar.immutableadapter.items.content.ContentViewRenderer;
import com.github.vivchar.immutableadapter.items.header.HeaderModel;
import com.github.vivchar.immutableadapter.items.header.HeaderViewRenderer;
import com.github.vivchar.rendererrecyclerviewadapter.ItemModel;
import com.github.vivchar.rendererrecyclerviewadapter.RendererRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public
class MainActivity
Expand All @@ -27,13 +32,24 @@ class MainActivity
private RendererRecyclerViewAdapter mRecyclerViewAdapter;
private RecyclerView mRecyclerView;
private GridLayoutManager mLayoutManager;
private SwipeRefreshLayout mSwipeToRefresh;

@Override
protected
void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mSwipeToRefresh = (SwipeRefreshLayout) findViewById(R.id.refresh);
mSwipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
{
@Override
public
void onRefresh() {
updateItems();
}
});

mRecyclerViewAdapter = new RendererRecyclerViewAdapter();
mRecyclerViewAdapter.registerRenderer(new HeaderViewRenderer(HeaderModel.TYPE, this));
mRecyclerViewAdapter.registerRenderer(new CategoryViewRenderer(CategoryModel.TYPE, this, mListener));
Expand All @@ -60,25 +76,49 @@ int getSpanSize(final int position) {
mRecyclerView.setAdapter(mRecyclerViewAdapter);
mRecyclerView.addItemDecoration(new EqualSpacesItemDecoration(20));

mRecyclerViewAdapter.setItems(getItems());
mRecyclerViewAdapter.notifyDataSetChanged();
updateItems();
}

private
void updateItems() {
// mRecyclerViewAdapter.setItems(getItems());
// mRecyclerViewAdapter.notifyDataSetChanged();

mRecyclerViewAdapter.setItems(getItems(), mDiffCallback);
mSwipeToRefresh.setRefreshing(false);
}

@NonNull
private
ArrayList<ItemModel> getItems() {
final ArrayList<ItemModel> items = new ArrayList<>();
items.add(new HeaderModel("header"));
for (int i = 0; i < 5; i++) {
items.add(new CategoryModel("some category #" + (i + 1)));
for (int j = 0; j < 12; j++) {
items.add(new ContentModel("content " + (j + 1)));
ArrayList<BaseItemModel> getItems() {
final ArrayList<BaseItemModel> items = new ArrayList<>();
final int headerID = 1;
items.add(new HeaderModel(headerID, "header"));

final int categoryCount = random(3, 9);
for (int i = 0; i < categoryCount; i++) {
final int categoryID = i * 10;
items.add(new CategoryModel(categoryID, "some category #" + (i + 1)));

final ArrayList<BaseItemModel> content = new ArrayList<>();
final int contentCount = random(1, 9);
for (int j = 0; j < contentCount; j++) {
final int contentID = i * 10 + j;
content.add(new ContentModel(contentID, "content " + (j + 1)));
}
Collections.shuffle(content);
items.addAll(content);
}

return items;
}

private
int random(final int min, final int max) {
final Random random = new Random();
return random.nextInt(max - min + 1) + min;
}

@NonNull
private final ContentViewRenderer.Listener mListener = new ContentViewRenderer.Listener()
{
Expand All @@ -94,4 +134,52 @@ void onContentItemClicked(@NonNull final ContentModel model) {
Toast.makeText(MainActivity.this, model.getName(), Toast.LENGTH_SHORT).show();
}
};

@NonNull
private final RendererRecyclerViewAdapter.DiffCallback<BaseItemModel> mDiffCallback = new RendererRecyclerViewAdapter
.DiffCallback<BaseItemModel>()
{

private final ArrayList<BaseItemModel> mOldItems = new ArrayList<>();
private final ArrayList<BaseItemModel> mNewItems = new ArrayList<>();

@Override
public
void setItems(@NonNull final List<BaseItemModel> oldItems, @NonNull final List<BaseItemModel> newItems) {
mOldItems.clear();
mOldItems.addAll(oldItems);

mNewItems.clear();
mNewItems.addAll(newItems);
}

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

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

@Override
public
boolean areItemsTheSame(final int oldItemPosition, final int newItemPosition) {
final BaseItemModel oldItem = mOldItems.get(oldItemPosition);
final BaseItemModel newItem = mNewItems.get(newItemPosition);
return oldItem.getID() == newItem.getID();
}

@Override
public
boolean areContentsTheSame(final int oldItemPosition, final int newItemPosition) {
final ItemModel oldItem = mOldItems.get(oldItemPosition);
final ItemModel newItem = mNewItems.get(newItemPosition);
return oldItem.equals(newItem);
}
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.vivchar.immutableadapter.items;

import com.github.vivchar.rendererrecyclerviewadapter.ItemModel;

/**
* Created by Vivchar Vitaly on 3/20/17.
*/
public interface BaseItemModel
extends ItemModel
{
int getID();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@

import android.support.annotation.NonNull;

import com.github.vivchar.rendererrecyclerviewadapter.ItemModel;
import com.github.vivchar.immutableadapter.items.BaseItemModel;

/**
* Created by Vivchar Vitaly on 1/10/17.
*/
public
class CategoryModel
implements ItemModel
implements BaseItemModel
{

public static final int TYPE = 0;
private final int mID;
@NonNull
private final String mTitle;

public
CategoryModel(@NonNull final String title) {
CategoryModel(final int ID, @NonNull final String title) {
mID = ID;
mTitle = title;
}

@Override
public
int getID() {
return mID;
}

@Override
public
int getType() {
Expand All @@ -32,4 +40,31 @@ int getType() {
String getName() {
return mTitle;
}

@Override
public
boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

final CategoryModel that = (CategoryModel) o;

if (mID != that.mID) {
return false;
}
return mTitle.equals(that.mTitle);

}

@Override
public
int hashCode() {
int result = mID;
result = 31 * result + mTitle.hashCode();
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@

import android.support.annotation.NonNull;

import com.github.vivchar.rendererrecyclerviewadapter.ItemModel;
import com.github.vivchar.immutableadapter.items.BaseItemModel;

/**
* Created by Vivchar Vitaly on 1/10/17.
*/
public
class ContentModel implements ItemModel
class ContentModel
implements BaseItemModel
{

public static final int TYPE = 1;
private final int mID;
@NonNull
private final String mName;

public
ContentModel(@NonNull final String name) {
ContentModel(final int ID, @NonNull final String name) {
mID = ID;
mName = name;
}

@Override
public
int getID() {
return mID;
}

@Override
public
int getType() {
Expand All @@ -31,4 +40,31 @@ int getType() {
String getName() {
return mName;
}

@Override
public
boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

final ContentModel that = (ContentModel) o;

if (mID != that.mID) {
return false;
}
return mName.equals(that.mName);

}

@Override
public
int hashCode() {
int result = mID;
result = 31 * result + mName.hashCode();
return result;
}
}
Loading

0 comments on commit 0823d28

Please sign in to comment.