Skip to content

Commit

Permalink
Merge pull request #16 from AlanCheen/develop
Browse files Browse the repository at this point in the history
Support AsyncListDiffer feature
  • Loading branch information
AlanCheen authored Jan 8, 2019
2 parents a217888 + 8131b3f commit 3613fd7
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flap

[![Download](https://api.bintray.com/packages/alancheen/maven/flap/images/download.svg?version=0.9.1)](https://bintray.com/alancheen/maven/flap/0.9.1/link) [![Build Status](https://travis-ci.org/AlanCheen/Flap.svg?branch=master)](https://travis-ci.org/AlanCheen/Flap) ![RecyclerView](https://img.shields.io/badge/RecyclerView-28.0.0-brightgreen.svg) ![API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat) [![license](https://img.shields.io/github/license/AlanCheen/Flap.svg)](./LICENSE) [![Author](https://img.shields.io/badge/%E4%BD%9C%E8%80%85-%E7%A8%8B%E5%BA%8F%E4%BA%A6%E9%9D%9E%E7%8C%BF-blue.svg)](https://github.com/AlanCheen) [![PRs welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/AlanCheen/Flap/pulls)
[![Download](https://api.bintray.com/packages/alancheen/maven/flap/images/download.svg?version=0.9.2)](https://bintray.com/alancheen/maven/flap/0.9.2/link) [![Build Status](https://travis-ci.org/AlanCheen/Flap.svg?branch=master)](https://travis-ci.org/AlanCheen/Flap) ![RecyclerView](https://img.shields.io/badge/RecyclerView-28.0.0-brightgreen.svg) ![API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat) [![license](https://img.shields.io/github/license/AlanCheen/Flap.svg)](./LICENSE) [![Author](https://img.shields.io/badge/%E4%BD%9C%E8%80%85-%E7%A8%8B%E5%BA%8F%E4%BA%A6%E9%9D%9E%E7%8C%BF-blue.svg)](https://github.com/AlanCheen) [![PRs welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/AlanCheen/Flap/pulls)

**[WIP]WARNING: Flap is still under development.**

Expand Down
65 changes: 35 additions & 30 deletions app/src/main/java/me/yifeiyuan/flapdev/DifferActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,23 @@
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.recyclerview.extensions.AsyncListDiffer;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

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

import me.yifeiyuan.flap.FlapAdapter;
import me.yifeiyuan.flap.extensions.DifferFlapAdapter;
import me.yifeiyuan.flapdev.simpletext.SimpleTextModel;

/**
* todo support
* AsyncListDiffer
* ListAdapter
* Testing for DifferFlapAdapter
*/
public class DifferActivity extends AppCompatActivity {

private static final String TAG = "DifferActivity";

private FlapAdapter flapAdapter;
private AsyncListDiffer<SimpleTextModel> differ;
private DifferFlapAdapter<SimpleTextModel> flapAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -34,49 +29,59 @@ protected void onCreate(Bundle savedInstanceState) {

RecyclerView recyclerView = findViewById(R.id.rv_items);

flapAdapter = new FlapAdapter();

List<SimpleTextModel> models = new ArrayList<>();

models.add(new SimpleTextModel("Android"));
models.add(new SimpleTextModel("Java"));
models.add(new SimpleTextModel("Kotlin"));

flapAdapter.setModels(models);

recyclerView.setAdapter(flapAdapter);

differ = new AsyncListDiffer<SimpleTextModel>(flapAdapter, new DiffUtil.ItemCallback<SimpleTextModel>() {
flapAdapter = new DifferFlapAdapter<>(new DiffUtil.ItemCallback<SimpleTextModel>() {
@Override
public boolean areItemsTheSame(@NonNull final SimpleTextModel simpleTextModel, @NonNull final SimpleTextModel t1) {
Log.d(TAG, "areItemsTheSame() called with: simpleTextModel = [" + simpleTextModel + "], t1 = [" + t1 + "]");
return false;
return true;
}

@Override
public boolean areContentsTheSame(@NonNull final SimpleTextModel simpleTextModel, @NonNull final SimpleTextModel t1) {
Log.d(TAG, "areContentsTheSame() called with: simpleTextModel = [" + simpleTextModel + "], t1 = [" + t1 + "]");
return false;
return simpleTextModel.equals(t1);
}
});

List<SimpleTextModel> models = new ArrayList<>();

for (int i = 0; i < 20; i++) {
models.add(new SimpleTextModel("Android :" + i));
}

flapAdapter.setData(models);

recyclerView.setAdapter(flapAdapter);
}

@Override
protected void onResume() {
super.onResume();
changeModels();
// testClear();
}

private void testClear() {
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
// flapAdapter.getData().clear(); will throw UnsupportedOperationException
flapAdapter.setData(new ArrayList<>());
}
}, 3000);
}

private void changeModels() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<SimpleTextModel> newModels = new ArrayList<>();
newModels.add(new SimpleTextModel("iOS"));
newModels.add(new SimpleTextModel("OC"));
newModels.add(new SimpleTextModel("Kotlin"));
flapAdapter.setModels(newModels);
differ.submitList(newModels);

for (int i = 0; i < 20; i++) {
// newModels.add(new SimpleTextModel("Android :" + i));
newModels.add(new SimpleTextModel("Android :" + (i % 2 == 0 ? i : (666))));
}

flapAdapter.setData(newModels);
}
}, 5000);
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/me/yifeiyuan/flapdev/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void createSimpleTestCase(final RecyclerView recyclerView) {
models.add(new SimpleImageModel());
models.add(new SimpleImageModel());

adapter.setModels(models);
adapter.setData(models);

recyclerView.setAdapter(adapter);
}
Expand All @@ -52,7 +52,7 @@ private void createAdvanceTestCase(final RecyclerView recyclerView) {
adapter.setUseFlapItemPool(true)
.setLifecycleEnable(true)
.setLifecycleOwner(this)
.setModels(models);
.setData(models);

recyclerView.setAdapter(adapter);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.yifeiyuan.flapdev.simpletext;

import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

Expand All @@ -27,6 +28,7 @@ public SimpleTextItem(final View itemView) {

@Override
protected void onBind(@NonNull final SimpleTextModel model, @NonNull final FlapAdapter adapter, @NonNull final List<Object> payloads) {
Log.d(TAG, "onBind: " + getAdapterPosition());
tvContent.setText(model.content);
}

Expand Down
2 changes: 1 addition & 1 deletion flap/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies {

apply plugin: "guru.stefma.bintrayrelease"

version = "0.9.1"
version = "0.9.3"
group = "me.yifeiyuan.flap"
androidArtifact {
artifactId = "flap"
Expand Down
22 changes: 11 additions & 11 deletions flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class FlapAdapter extends RecyclerView.Adapter<FlapItem> {
private final Flap flap = Flap.getDefault();

@NonNull
private List<?> models = new ArrayList<>();
private List<?> data = new ArrayList<>();

private LifecycleOwner lifecycleOwner;

Expand All @@ -44,7 +44,7 @@ public final void onBindViewHolder(@NonNull final FlapItem holder, final int pos
@Override
public void onBindViewHolder(@NonNull final FlapItem holder, final int position, @NonNull final List<Object> payloads) {
attachLifecycleOwnerIfNeed(holder);
flap.onBindViewHolder(holder, getModel(position), this, payloads);
flap.onBindViewHolder(holder, getItem(position), this, payloads);
}

/**
Expand All @@ -60,12 +60,12 @@ private void attachLifecycleOwnerIfNeed(final FlapItem holder) {

@Override
public int getItemCount() {
return getModels().size();
return getData().size();
}

@Override
public int getItemViewType(final int position) {
return flap.getItemViewType(getModel(position));
return flap.getItemViewType(getItem(position));
}

@Override
Expand Down Expand Up @@ -116,19 +116,19 @@ public FlapAdapter setLifecycleEnable(boolean lifecycleEnable) {
return this;
}

private Object getModel(final int position) {
return getModels().get(position);
protected Object getItem(final int position) {
return getData().get(position);
}

public FlapAdapter setModels(@NonNull List<?> models) {
checkNotNull(models, "models can't be null here");
this.models = models;
public FlapAdapter setData(@NonNull List<?> data) {
checkNotNull(data, "data can't be null here");
this.data = data;
return this;
}

@NonNull
public List<?> getModels() {
return models;
public List<?> getData() {
return data;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.yifeiyuan.flap.extensions;

import android.support.annotation.NonNull;
import android.support.v7.recyclerview.extensions.AsyncDifferConfig;
import android.support.v7.recyclerview.extensions.AsyncListDiffer;
import android.support.v7.util.AdapterListUpdateCallback;
import android.support.v7.util.DiffUtil;

import java.util.List;

import me.yifeiyuan.flap.FlapAdapter;

/**
* Created by 程序亦非猿 on 2019/1/4.
*/
@SuppressWarnings("unchecked")
public class DifferFlapAdapter<T> extends FlapAdapter {

private AsyncListDiffer<T> differ;

public DifferFlapAdapter(final @NonNull DiffUtil.ItemCallback<T> itemCallback) {
differ = new AsyncListDiffer(this, itemCallback);
}

public DifferFlapAdapter(@NonNull AsyncDifferConfig<T> config) {
differ = new AsyncListDiffer(new AdapterListUpdateCallback(this), config);
}

@Override
public DifferFlapAdapter setData(@NonNull final List<?> data) {
differ.submitList((List<T>) data);
return this;
}

@Override
protected T getItem(int position) {
return differ.getCurrentList().get(position);
}

@Override
public int getItemCount() {
return differ.getCurrentList().size();
}

@NonNull
@Override
public List<T> getData() {
return differ.getCurrentList();
}
}

0 comments on commit 3613fd7

Please sign in to comment.