Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
Merge branch 'async-differ'
Browse files Browse the repository at this point in the history
  • Loading branch information
belinwu committed Jan 25, 2019
2 parents 44bb074 + 85813f9 commit fab0b65
Show file tree
Hide file tree
Showing 31 changed files with 557 additions and 104 deletions.
66 changes: 53 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@ Modular adapter for Android RecyclerView.
**DO NOT USE THIS LIBRARY IN PRODUCTION UNTIL V1.0.0 IS RELEASED.**

# Installation

Add the following dependency to your `build.gradle` file:

```groovy
dependencies {
implementation 'com.samelody.modapter:modapter:0.1.0'
// required, core
implementation 'com.samelody.modapter:modapter-core:0.2.0'
// optional, support android paging library
implementation 'com.samelody.modapter:modapter-paging:0.2.0'
}
```

# Usage

```java
// create a ModularAdapter object instead of creating new Subclass of RecyclerView.Adapter.
ModularAdapter adapter = new ModularAdapter();
recyclerView.setAdapter(adapter);
ModularAdapter<AdapterItem> adapter = new ModularAdapter<>();
listView.setAdapter(adapter);

// register item metadata to manager
ItemManager manager = adapter.getManager();
ItemManager<AdapterItem> manager = adapter.getManager();
manager.register(R.layout.item_gallery_image, ImageViewHolder.class)
.register(R.layout.item_gallery_date, DateViewHolder.class);

Expand All @@ -35,32 +40,67 @@ List<Item> list = new ArrayList<>();
list.add(new ImageItem());
list.add(new DateItem());

// Setup list and notify
manager.setList(list);
// submit list
manager.submitList(list);

// notify adapter if no async differs used
adapter.notifyDataSetChanged();
```

# Docs
## AsyncDiffer

The `ItemManager` not enable async diffing by default.

You can setup `AsyncDiffer` by your choice. The setting API is

`ItemManager#setDiffer()`.

The implemented async differs are following:

- `NonAsyncDiffer`: The non implementation. (default used)
- `ListAsyncDiffer`: The `AsyncListDiffer` implementation.
- `PagedAsyncDiffer`: The `AsyncPagedListDiffer` implementation.

### Using Async Differ

```java
// set differ
manager.setDiffer(new ListAsyncDiffer<>());

// submit list
manager.submitList(list);

// not need to call adapter.notify APIs
```

## ItemManager

All APIs are encapsulated in `ItemManager` interface returned by `ModularAdapter#getItemManager()`.
All APIs are encapsulated in `ItemManager` interface returned by `ModularAdapter#getManager()`.

```java
// register via layoutId and holderClass
ItemManager register(@LayoutRes int layoutId, Class<T> holderClass);
ItemManager<E> register(@LayoutRes int layoutId, Class<T> holderClass);

// register via ItemMetadata
ItemManager register(ItemMetadata metadata);
ItemManager<E> register(ItemMetadata metadata);

// unregister via layoutId
ItemManager unregister(@LayoutRes int layoutId);
ItemManager<E> unregister(@LayoutRes int layoutId);

// setup the data list
ItemManager setList(List<? extends AdapterItem> list);
ItemManager<E> submitList(List<? extends E> list);

// Gets the item with given position
T getItem(int position);
E getItem(int position);

// Gets current displayed list
List<E> getCurrentList();

// Gets item count
int getItemCount();

// setup async differ
ItemManager<E> setDiffer(AsyncDiffer<E> differ);
```

# License
Expand Down
5 changes: 1 addition & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ buildscript {
def deps = [:]
ext.deps = deps

deps.libVersionCode = 1
deps.libVersionName = "0.1.0"
deps.demoVersionCode = 1
deps.demoVersionName = "0.1.0"
deps.compileSdk = 28
deps.targetSdk = 28
deps.minSdk = 15

deps.appcompat = "com.android.support:appcompat-v7:28.0.0"
deps.recyclerview = "com.android.support:recyclerview-v7:28.0.0"
deps.paging = "android.arch.paging:runtime:1.0.1"
deps.constraintlayout = "com.android.support.constraint:constraint-layout:1.1.3"
deps.junit = "junit:junit:4.12"
deps.test = "com.android.support.test:runner:1.0.2"
Expand Down
File renamed without changes.
43 changes: 22 additions & 21 deletions library/build.gradle → core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
apply plugin: 'com.android.library'

ext.pubspec = [:]

pubspec.packaging = 'aar'
pubspec.groupId = 'com.samelody.modapter'
pubspec.artifactId = 'modapter-core'
pubspec.repo = 'maven'
pubspec.name = 'Modapter'
pubspec.description = 'Modular adapter for Android RecyclerView.'
pubspec.version = "0.2.0"
pubspec.versionCode = 2
pubspec.url = 'https://github.com/samelody/modapter'
pubspec.gitUrl = 'https://github.com/samelody/modapter.git'
pubspec.issueUrl = 'https://github.com/samelody/modapter/issues'
pubspec.developerId = 'belinwu'
pubspec.developerName = 'Belin Wu'
pubspec.developerEmail = '[email protected]'
pubspec.licenseName = 'Apache License 2.0'
pubspec.licenseUrl = 'https://raw.githubusercontent.com/samelody/modapter/master/LICENSE'
pubspec.licenses = ["Apache-2.0"]

android {
compileSdkVersion deps.compileSdk
defaultConfig {
minSdkVersion deps.minSdk
targetSdkVersion deps.targetSdk
versionCode deps.libVersionCode
versionName deps.libVersionName
versionCode pubspec.versionCode
versionName pubspec.version
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand All @@ -29,23 +49,4 @@ dependencies {
androidTestImplementation deps.espresso
}

ext.pubspec = [:]

pubspec.packaging = 'aar'
pubspec.groupId = 'com.samelody.modapter'
pubspec.artifactId = 'modapter'
pubspec.repo = 'maven'
pubspec.name = 'Modapter'
pubspec.description = 'Modular adapter for Android RecyclerView.'
pubspec.version = deps.libVersionName
pubspec.url = 'https://github.com/samelody/modapter'
pubspec.gitUrl = 'https://github.com/samelody/modapter.git'
pubspec.issueUrl = 'https://github.com/samelody/modapter/issues'
pubspec.developerId = 'belinwu'
pubspec.developerName = 'Belin Wu'
pubspec.developerEmail = '[email protected]'
pubspec.licenseName = 'Apache License 2.0'
pubspec.licenseUrl = 'https://raw.githubusercontent.com/samelody/modapter/master/LICENSE'
pubspec.licenses = ["Apache-2.0"]

apply from: "https://raw.githubusercontent.com/samelody/pubman/master/pub.gradle"
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.samelody.modapter" />
package="com.samelody.modapter.core" />
70 changes: 44 additions & 26 deletions ...om/samelody/modapter/AdapterDelegate.java → ...om/samelody/modapter/AdapterDelegate.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,83 @@
import android.view.View;
import android.view.ViewGroup;

import com.samelody.modapter.differ.AsyncDiffer;
import com.samelody.modapter.differ.NonAsyncDiffer;

import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.List;

import static java.util.Collections.emptyList;


/**
* The delegated implementation of modular adapter.
*
* @author Belin Wu
*/
public final class AdapterDelegate implements ItemManager {
public final class AdapterDelegate<E extends AdapterItem> implements ItemManager<E> {

/**
* The item metadata registry.
*/
private SparseArray<ItemMetadata> registry = new SparseArray<>();

/**
* The list of items.
* A async differ.
*/
@NonNull
private List<? extends AdapterItem> list = emptyList();
private AsyncDiffer<E> differ;

public AdapterDelegate() {
this(new NonAsyncDiffer<E>());
}

public AdapterDelegate(AsyncDiffer<E> differ) {
checkDiffer(differ);
this.differ = differ;
}

private void checkDiffer(AsyncDiffer<E> differ) {
if (differ == null) {
throw new IllegalArgumentException("differ argument must not be null");
}
}

/**
* Gets item count.
*
* @return The item count.
*/
@Override
public int getItemCount() {
return list.size();
return differ.getItemCount();
}

/**
* {@inheritDoc}
*/
@Nullable
@SuppressWarnings("unchecked")
@Override
public <T extends AdapterItem> T getItem(int position) {
if (position < 0 || position >= list.size()) {
return null;
}
return (T) list.get(position);
public ItemManager<E> setDiffer(AsyncDiffer<E> differ) {
checkDiffer(differ);
this.differ = differ;
return this;
}

/**
* {@inheritDoc}
*/
@NonNull
@Override
public ItemManager setList(List<? extends AdapterItem> list) {
this.list = list == null ? Collections.<AdapterItem>emptyList() : list;
public ItemManager<E> submitList(List<? extends E> list) {
differ.submitList(list);
return this;
}

@Override
public List<E> getCurrentList() {
return differ.getCurrentList();
}

@Nullable
@Override
public E getItem(int position) {
return differ.getItem(position);
}

@NonNull
@Override
public <T extends ViewHolder> ItemManager register(int layoutId, Class<T> holderClass) {
public ItemManager<E> register(int layoutId, Class<? extends ViewHolder> holderClass) {
ItemMetadata metadata = new ItemMetadata();
metadata.setLayoutId(layoutId);
metadata.setHolderClass(holderClass);
Expand All @@ -76,7 +94,7 @@ public <T extends ViewHolder> ItemManager register(int layoutId, Class<T> holder

@NonNull
@Override
public ItemManager register(ItemMetadata metadata) {
public ItemManager<E> register(ItemMetadata metadata) {
if (metadata != null) {
registry.put(metadata.getLayoutId(), metadata);
}
Expand All @@ -85,7 +103,7 @@ public ItemManager register(ItemMetadata metadata) {

@NonNull
@Override
public ItemManager unregister(int layoutId) {
public ItemManager<E> unregister(int layoutId) {
registry.delete(layoutId);
return this;
}
Expand Down
Loading

0 comments on commit fab0b65

Please sign in to comment.