Last active
November 17, 2016 14:36
-
-
Save henrytao-me/2d00eaf27197ee1504c7c834895c2500 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2016 "Henry Tao <[email protected]>" | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package me.henrytao.util; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import java.util.List; | |
import rx.Observable; | |
/** | |
* Created by henrytao on 8/14/16. | |
*/ | |
public class Pagination<D, I> { | |
public static <D, I> Pagination<D, I> create(@NonNull OnLoadListener<D, I> onLoadListener) { | |
return new Pagination<>(null, onLoadListener); | |
} | |
private static RuntimeException createOnFinishException() { | |
return new IllegalStateException("Pagination is finished"); | |
} | |
private final I mPageInfo; | |
private boolean mFinished; | |
private Pagination<D, I> mNextPagination; | |
private OnLoadListener<D, I> mOnLoadListener; | |
private OnLoadedListener<D, I> mOnLoadedListener; | |
private Result<D, I> mResult; | |
protected Pagination(I pageInfo, @NonNull OnLoadListener<D, I> onLoadListener) { | |
mPageInfo = pageInfo; | |
mOnLoadListener = onLoadListener; | |
} | |
public void finish() { | |
mFinished = true; | |
} | |
@Nullable | |
public I getPageInfo() { | |
return mPageInfo; | |
} | |
public boolean isFinished() { | |
return mFinished; | |
} | |
public Observable<List<D>> load() { | |
return Observable.just(null) | |
.flatMap(o -> !mFinished ? mOnLoadListener.onLoad(this) : Observable.error(createOnFinishException())) | |
.flatMap(result -> { | |
mResult = result; | |
return Observable.just(mResult.data); | |
}) | |
.doOnNext(data -> { | |
if (mOnLoadedListener != null) { | |
mOnLoadedListener.onLoaded(this, data); | |
} | |
}); | |
} | |
public Pagination<D, I> next() { | |
if (mNextPagination != null) { | |
return mNextPagination; | |
} | |
if (!mFinished) { | |
OnLoadListener<D, I> listener = mOnLoadListener; | |
Result<D, I> data = mResult; | |
return new Pagination<>(data != null ? data.nextPageInfo : null, listener); | |
} | |
return this; | |
} | |
public void overrideNextPagination(Pagination<D, I> nextPagination) { | |
mNextPagination = nextPagination; | |
} | |
public void setOnLoadedListener(OnLoadedListener<D, I> onLoadedListener) { | |
mOnLoadedListener = onLoadedListener; | |
} | |
public interface OnLoadListener<D, I> { | |
@NonNull | |
Observable<Result<D, I>> onLoad(Pagination<D, I> pagination); | |
} | |
public interface OnLoadedListener<D, I> { | |
void onLoaded(Pagination<D, I> pagination, List<D> data); | |
} | |
public static class Result<D, I> { | |
public static <D, I> Result<D, I> create(List<D> data, I nextPageInfo) { | |
return new Result<>(data, nextPageInfo); | |
} | |
private final List<D> data; | |
private final I nextPageInfo; | |
private Result(List<D> data, I nextPageInfo) { | |
this.data = data; | |
this.nextPageInfo = nextPageInfo; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment