Skip to content

Commit 28bb034

Browse files
author
artshell
committed
Mofiy: code enhancements
1 parent 14bb582 commit 28bb034

9 files changed

Lines changed: 180 additions & 35 deletions

File tree

app/src/main/java/com/artshell/misc/rv/official_paging_example/repository/inDb/DbRedditPostRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public Listing<RedditPost> postsSubreddit(String subReddit, int pageSize) {
129129
builder.build(),
130130
boundaryCallback.getNetworkState(),
131131
refreshState,
132-
() -> boundaryCallback.getHelper().retryAllFailed(),
133-
() -> refreshTrigger.postValue(null)
132+
() -> refreshTrigger.postValue(null),
133+
() -> boundaryCallback.getHelper().retryAllFailed()
134134
);
135135
}
136136
}

app/src/main/java/com/artshell/misc/rv/official_paging_example/repository/inMemory/byItem/InMemoryByItemRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public Listing<RedditPost> postsSubreddit(String subReddit, int pageSize) {
5353
() -> {
5454
ItemKeyedSubredditDataSource source = sourceFactory.getSourceLiveData().getValue();
5555
if (source != null) {
56-
source.retryAllFailed();
56+
source.invalidate();
5757
}
5858
},
5959
() -> {
6060
ItemKeyedSubredditDataSource source = sourceFactory.getSourceLiveData().getValue();
6161
if (source != null) {
62-
source.invalidate();
62+
source.retryAllFailed();
6363
}
6464
}
6565
);

app/src/main/java/com/artshell/misc/rv/official_paging_example/repository/inMemory/byPage/InMemoryByPageKeyRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public Listing<RedditPost> postsSubreddit(String subReddit, int pageSize) {
4545
() -> {
4646
PageKeyedSubredditDataSource source = sourceFactory.getSourceLiveData().getValue();
4747
if (source != null) {
48-
source.retryAllFailed();
48+
source.invalidate();
4949
}
5050
},
5151
() -> {
5252
PageKeyedSubredditDataSource source = sourceFactory.getSourceLiveData().getValue();
5353
if (source != null) {
54-
source.invalidate();
54+
source.retryAllFailed();
5555
}
5656
}
5757
);

app/src/main/java/com/artshell/misc/rv/official_paging_example/ui/SubRedditViewModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void refresh() {
4040
public void retry() {
4141
Listing<RedditPost> value = repoResult.getValue();
4242
if (value != null && value.getRetry() != null) {
43-
value.getRefresh().invoke();
43+
value.getRetry().invoke();
4444
}
4545
}
4646

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.artshell.arch.common;
2+
3+
/**
4+
* @author artshell on 2018/7/31
5+
*/
6+
public class EmptyDataException extends IllegalStateException {
7+
public EmptyDataException() {
8+
}
9+
10+
public EmptyDataException(String s) {
11+
super(s);
12+
}
13+
14+
public EmptyDataException(String message, Throwable cause) {
15+
super(message, cause);
16+
}
17+
18+
public EmptyDataException(Throwable cause) {
19+
super(cause);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.artshell.arch.common;
2+
3+
/**
4+
* @author artshell on 2018/7/31
5+
*/
6+
public class FetchFailedException extends IllegalStateException {
7+
public FetchFailedException() {
8+
}
9+
10+
public FetchFailedException(String s) {
11+
super(s);
12+
}
13+
14+
public FetchFailedException(String message, Throwable cause) {
15+
super(message, cause);
16+
}
17+
18+
public FetchFailedException(Throwable cause) {
19+
super(cause);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.artshell.arch.common;
2+
3+
/**
4+
* @author artshell on 2018/7/31
5+
*/
6+
public class NoMoreDataException extends IllegalStateException {
7+
public NoMoreDataException() {
8+
}
9+
10+
public NoMoreDataException(String s) {
11+
super(s);
12+
}
13+
14+
public NoMoreDataException(String message, Throwable cause) {
15+
super(message, cause);
16+
}
17+
18+
public NoMoreDataException(Throwable cause) {
19+
super(cause);
20+
}
21+
}

arch/src/main/java/com/artshell/arch/storage/server/model/HttpPagingResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public class HttpPagingResult<T> {
77
* pageData :
88
*/
99

10-
private int code;
11-
private String msg;
10+
private int code;
11+
private String msg;
1212
private Pageable<T> pageData;
1313

1414
public int getCode() {
Lines changed: 108 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,155 @@
11
package com.artshell.arch.widget.rv;
22

3+
import android.annotation.SuppressLint;
34
import android.arch.lifecycle.MutableLiveData;
45
import android.arch.paging.DataSource;
56
import android.arch.paging.PageKeyedDataSource;
67
import android.arch.paging.PagedList;
78
import android.support.annotation.NonNull;
89

10+
import com.artshell.arch.common.EmptyDataException;
11+
import com.artshell.arch.common.FetchFailedException;
12+
import com.artshell.arch.common.NoMoreDataException;
913
import com.artshell.arch.storage.Resource;
1014
import com.artshell.arch.storage.server.model.HttpPagingResult;
15+
import com.artshell.arch.storage.server.model.Pageable;
1116

17+
import java.util.Collections;
18+
import java.util.List;
1219
import java.util.concurrent.Executor;
1320

1421
import io.reactivex.Flowable;
1522
import io.reactivex.Scheduler;
1623
import io.reactivex.schedulers.Schedulers;
1724

1825
/**
19-
* @param <Key> 页码(第1页,第2页等)
20-
* @param <Value> {@link PagedList} 最终得到结果
26+
* @param <Value> {@link PagedList}的结果类型
2127
* @param <Response> 服务器端返回的json字符串
2228
*/
23-
public abstract class PageableDataSourceFactory<Key extends Integer, Value, Response extends HttpPagingResult>
24-
extends DataSource.Factory<Key, Value> {
29+
public abstract class PageableDataSourceFactory<Value, Response extends HttpPagingResult<Value>> extends DataSource.Factory<Integer, Value> {
2530

26-
private MutableLiveData<Resource<Value>> mNetworkState; /* 网络状态(这里并不用来传递获取成功的结果) */
27-
private PageState mState; /* 分页算法 */
28-
protected Scheduler mNetworkScheduler;
31+
/* 网络状态(这里并不用来传递获取成功的结果, 只传递正在加载/加载失败状态) */
32+
private MutableLiveData<Resource<Void>> mNetworkState;
2933

30-
public PageableDataSourceFactory(Executor networkExecutor) {
34+
private Executor retryExecutor;
35+
protected Scheduler mNetworkScheduler;
36+
37+
/* 持有一个重试引用 */
38+
private Runnable retry;
39+
40+
public PageableDataSourceFactory(Executor workerExecutor) {
3141
mNetworkState = new MutableLiveData<>();
32-
mState = new PageState();
33-
mNetworkScheduler = Schedulers.from(networkExecutor);
42+
retryExecutor = workerExecutor;
43+
mNetworkScheduler = Schedulers.from(workerExecutor);
3444
}
3545

36-
public MutableLiveData<Resource<Value>> getNetworkState() {
46+
public MutableLiveData<Resource<Void>> getNetworkState() {
3747
return mNetworkState;
3848
}
3949

50+
/**
51+
* 重试
52+
*/
53+
public void retryFailed() {
54+
Runnable prevRetry = retry;
55+
retry = null;
56+
if (prevRetry != null) {
57+
retryExecutor.execute(prevRetry);
58+
}
59+
}
60+
4061
@Override
41-
public DataSource<Key, Value> create() {
42-
return null;
62+
public DataSource<Integer, Value> create() {
63+
return new PageableDataSource();
4364
}
4465

45-
/**
46-
* 获取初始分页数据/加载下一页
47-
*/
48-
private class PageableDataSource extends PageKeyedDataSource<Key, Response> {
66+
private class PageableDataSource extends PageKeyedDataSource<Integer, Value> {
4967

50-
@Override
51-
public final void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<Key> params, @NonNull LoadInitialCallback<Key, Response> callback) {
68+
private PageState mState; /* 分页算法 */
69+
70+
private PageableDataSource() {
71+
this(new PageState());
72+
}
5273

74+
private PageableDataSource(PageState state) {
75+
mState = state;
5376
}
5477

78+
/**
79+
* 获取第一页数据 & 计算分页信息
80+
* @param params
81+
* @param callback
82+
*/
83+
@SuppressLint("CheckResult")
5584
@Override
56-
public final void loadBefore(@NonNull LoadParams<Key> params, @NonNull LoadCallback<Key, Response> callback) {
57-
// 不在某一页之前插入数据,故此忽略实现
85+
public final void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, Value> callback) {
86+
source(1)
87+
.doOnSubscribe(subscription -> mNetworkState.postValue(Resource.loading()))
88+
.subscribe(
89+
response -> {
90+
Pageable<Value> pageableData = response.getPageData();
91+
String total = pageableData.getTotal();
92+
List<Value> dataList = pageableData.getList() == null ? Collections.emptyList() : pageableData.getList();
93+
if ("".equals(total) || "0".equals(total) || dataList.isEmpty()) {
94+
mNetworkState.postValue(Resource.error(new EmptyDataException("无数据")));
95+
}
96+
// 计算分页状态
97+
mState.clear();
98+
mState.calculate(Integer.valueOf(total));
99+
100+
// 回传结果, 设置上一页/下一页的页码
101+
callback.onResult(dataList, 1, mState.hasNext() ? mState.getCurrPage() + 1 : null /* 没有下一页 */);
102+
}, throwable -> {
103+
retry = () -> loadInitial(params, callback);
104+
mNetworkState.postValue(Resource.error(throwable));
105+
});
58106
}
59107

60108
@Override
61-
public final void loadAfter(@NonNull LoadParams<Key> params, @NonNull LoadCallback<Key, Response> callback) {
109+
public final void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Value> callback) {
110+
// 不在某一页之前插入数据,故此忽略实现
111+
}
62112

113+
/**
114+
* 获取下一页数据
115+
* @param params
116+
* @param callback
117+
*/
118+
@SuppressLint("CheckResult")
119+
@Override
120+
public final void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Value> callback) {
121+
if (params.key == null) {
122+
mNetworkState.postValue(Resource.error(new NoMoreDataException("没有更多数据")));
123+
return;
124+
}
125+
source(params.key)
126+
.doOnSubscribe(subscription -> mNetworkState.postValue(Resource.loading()))
127+
.subscribe(
128+
response -> {
129+
Pageable<Value> pageableData = response.getPageData();
130+
String total = pageableData.getTotal();
131+
List<Value> dataList = pageableData.getList() == null ? Collections.emptyList() : pageableData.getList();
132+
if ("".equals(total) || "0".equals(total) || dataList.isEmpty()) {
133+
retry = () -> loadAfter(params, callback);
134+
mNetworkState.postValue(Resource.error(new FetchFailedException("获取数据失败")));
135+
} else {
136+
// 设置当前页
137+
mState.setCurrPage(mState.getCurrPage() + 1);
138+
139+
// 回传结果, 设置下一页的页码
140+
callback.onResult(dataList, mState.hasNext() ? mState.getCurrPage() + 1 : null /* 没有下一页 */);
141+
}
142+
}, throwable -> {
143+
retry = () -> loadAfter(params, callback);
144+
mNetworkState.postValue(Resource.error(throwable));
145+
});
63146
}
64147
}
65148

66149
/**
67-
* @param currentPage 当前页
68-
* @param <Key> 页码(第1页,第2页等)
69-
* @param <Response> 服务器端返回的json字符串
150+
* 获取第一页/下一页数据
151+
* @param nextPage 下一页
70152
* @return
71153
*/
72-
abstract <Key, Response> Flowable<Response> source(Key currentPage);
154+
public abstract Flowable<Response> source(Integer nextPage);
73155
}

0 commit comments

Comments
 (0)