@@ -6,6 +6,8 @@ import com.xiasuhuei321.gank_kotlin.datasource.local.LocalDataSource
6
6
import com.xiasuhuei321.gank_kotlin.datasource.local.LocalDataSourceImpl
7
7
import com.xiasuhuei321.gank_kotlin.datasource.remote.RemoteDataSource
8
8
import com.xiasuhuei321.gank_kotlin.datasource.remote.RemoteDataSourceImpl
9
+ import com.xiasuhuei321.gank_kotlin.extension.handleResult
10
+ import com.xiasuhuei321.gank_kotlin.extension.io_main
9
11
import io.reactivex.Observable
10
12
11
13
/* *
@@ -17,7 +19,7 @@ object DataSourceImpl : DataSource {
17
19
18
20
private val remote: RemoteDataSource
19
21
20
- private val local: LocalDataSource
22
+ private val local: LocalDataSource < GankData >
21
23
22
24
init {
23
25
remote = RemoteDataSourceImpl ()
@@ -31,41 +33,52 @@ object DataSourceImpl : DataSource {
31
33
32
34
}
33
35
34
- override fun getData (type : String ): Observable <List <GankData >> {
35
- TODO ( " 获取数据,进行网络请求,加载失败再从本地读取缓存 " )
36
+ override fun getRemoteData (type : String ): Observable <List <GankData >> {
37
+ return getRemoteData(type, 10 , 1 )
36
38
}
37
39
38
- override fun getRemoteData (type : String , pageIndex : Int , count : Int ): Observable <List <GankData >> {
39
- TODO (" not implemented" ) // To change body of created functions use File | Settings | File Templates.
40
+ override fun getRemoteData (type : String , pageIndex : Int ): Observable <List <GankData >> {
41
+ return getRemoteData(type, 10 , pageIndex)
42
+ }
43
+
44
+ override fun getRemoteData (type : String , count : Int , pageIndex : Int ): Observable <List <GankData >> {
45
+ return remote
46
+ .getRemoteData(type, 10 , 1 )
47
+ .compose(handleResult())
48
+ .doOnNext({ list ->
49
+ if (pageIndex == 1 ) refreshLocalData(type, list)// 只有当加载第一页数据的时候才缓存
50
+ }
51
+ )
52
+ .io_main()
40
53
}
41
54
42
55
/* *
43
56
* 清除本地指定缓存
44
57
*/
45
58
override fun clearData (type : String ) {
46
- TODO ( " 清除本地指定缓存数据 " )
59
+ local.clearLocalData(type )
47
60
}
48
61
49
62
/* *
50
63
* 清除本地所有缓存
51
64
*/
52
65
override fun clearAllData () {
53
- // TODO("清除本地所有缓存数据" )
66
+ local.clearAllData( )
54
67
}
55
68
56
69
57
70
/* *
58
71
* 优先从本地获取数据
59
72
*/
60
73
private fun getLocalData (type : String ): Observable <List <GankData >> {
61
- TODO ( " 获取本地的缓存数据 " )
74
+ return Observable .just(local.getLocalData(type) )
62
75
}
63
76
64
77
/* *
65
78
* 刷新本地序列化存储数据
66
79
*/
67
- private fun refreshLocalData (type : String ) {
68
- // TODO("序列化存储指定数据到本地" )
80
+ private fun refreshLocalData (type : String , list : List < GankData > ) {
81
+ local.refreshLocalData(type,list )
69
82
}
70
83
71
84
/* *
0 commit comments