Skip to content

Commit

Permalink
provide event for detecting "required scroll", 0.9.4
Browse files Browse the repository at this point in the history
  • Loading branch information
doodeec committed Jun 9, 2017
1 parent 527791d commit 615278c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ org.gradle.jvmargs=-Xmx1536m
# org.gradle.parallel=true

POM_PACKAGING=aar
VERSION_NAME=0.9.3
VERSION_NAME=0.9.4
VERSION_CODE=1
GROUP=sk.teamsoft
ORGANIZATION_NAME=team-softsk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.res.Resources;
import android.support.annotation.Nullable;
import android.support.v7.util.DiffUtil;
import android.support.v7.util.ListUpdateCallback;
import android.support.v7.widget.RecyclerView;
import android.view.InflateException;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -37,6 +38,7 @@ public class ObservableAdapter<T> extends RecyclerView.Adapter<ObservableAdapter

private final AdapterSource<T> source;
private final PublishSubject<ViewEvent> itemEvent = PublishSubject.create();
private final PublishSubject<Boolean> requestScrollEvent = PublishSubject.create();
private final CompositeDisposable changeWatcher = new CompositeDisposable();

public ObservableAdapter(AdapterSource<T> source) {
Expand Down Expand Up @@ -80,6 +82,13 @@ public ObservableAdapter(AdapterSource<T> source) {
.subscribe(new Consumer<DiffUtil.DiffResult>() {
@Override
public void accept(@NonNull DiffUtil.DiffResult diffResult) throws Exception {
diffResult.dispatchUpdatesTo(new SimpleUpdateCallback() {
@Override public void onInserted(int position, int count) {
if (position == 0) {
requestScrollEvent.onNext(Boolean.TRUE);
}
}
});
diffResult.dispatchUpdatesTo(ObservableAdapter.this);
}
},
Expand Down Expand Up @@ -138,9 +147,6 @@ public void accept(@NonNull Throwable throwable) throws Exception {
Timber.e(throwable, "Error:viewHolder:event");
}
});
} else {
Timber.v("onAttached:noObservable");
//TODO check if we need to dispose observable if there was any
}
}

Expand All @@ -154,6 +160,16 @@ public void accept(@NonNull Throwable throwable) throws Exception {
super.onViewRecycled(holder);
}

/**
* Notify that list inserted new items in front of the existing collection
* You can use this to detect when you need to force list to scroll up (i.e. when list
* was scrolled to the top, and UI maintained scroll position after collection was updated)
* @return observable event
*/
public Observable<Boolean> onRequestScrollUp() {
return requestScrollEvent;
}

/**
* Generic method which dispatches viewHolder events (click, longtap, whatever) to
* the listeners (typically presenters).
Expand Down Expand Up @@ -260,4 +276,18 @@ public Object getData() {
'}';
}
}

/**
* Helper class for better readability
*/
static class SimpleUpdateCallback implements ListUpdateCallback {

@Override public void onInserted(int position, int count) {}

@Override public void onRemoved(int position, int count) {}

@Override public void onMoved(int fromPosition, int toPosition) {}

@Override public void onChanged(int position, int count, Object payload) {}
}
}

0 comments on commit 615278c

Please sign in to comment.