Skip to content

Commit

Permalink
Fix the bug of create AsyncDataProvider.
Browse files Browse the repository at this point in the history
Add a new constructor of AsyncDataProvider.

Add exception of calling setData int PageDataProvider directly.
  • Loading branch information
zhouyi05 committed Sep 20, 2015
1 parent b84b225 commit 5b6eae3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,45 @@
import com.kifile.android.cornerstone.core.DataProvider;
import com.kifile.android.cornerstone.utils.WorkerThreadPool;

import java.util.concurrent.Executor;

/**
* Fetch Data on a no-ui thread.
*
* @author kifile
*/
public class AsyncDataProvider<DATA> extends DecoratorDataProvider<DATA> {

private AsyncDataProvider(DataProvider<DATA> proxy) {
private Executor mExecutor;

public AsyncDataProvider(DataProvider<DATA> proxy) {
super(proxy);
}

public AsyncDataProvider(DataProvider<DATA> proxy, Executor executor) {
super(proxy);
mExecutor = executor;
}

private WorkerThreadPool.WorkerRunnable mWorker;

private final Runnable mSuperRefresh = new Runnable() {
@Override
public void run() {
AsyncDataProvider.super.refresh();
}
};

@Override
public void refresh() {
if (mWorker != null) {
mWorker.cancel();
}
mWorker = WorkerThreadPool.getInstance().execute(new Runnable() {
@Override
public void run() {
AsyncDataProvider.super.refresh();
if (mExecutor != null) {
mExecutor.execute(mSuperRefresh);
} else {
if (mWorker != null) {
mWorker.cancel();
}
});
mWorker = WorkerThreadPool.getInstance().execute(mSuperRefresh);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public boolean isDataNeedUpdate() {
return page != null && page.page == 0;
}

@Override
protected synchronized void setData(PageData<DATA> dataPageData) {
throw new RuntimeException("Should call #setPageData to setData");
}

public void setPageData(int page, DATA data) {
super.setData(new PageData<>(page, data));
}

public static class PageData<DATA> {

public int page;
Expand Down

0 comments on commit 5b6eae3

Please sign in to comment.