1
1
package com.xiasuhuei321.gank_kotlin.datasource
2
2
3
- import android.util.Log
4
- import com.xiasuhuei321.gank_kotlin.app
5
3
import com.xiasuhuei321.gank_kotlin.datasource.bean.GankData
6
- import com.xiasuhuei321.gank_kotlin.datasource.bean.JsonResult
7
4
import com.xiasuhuei321.gank_kotlin.datasource.bean.Weather
8
5
import com.xiasuhuei321.gank_kotlin.datasource.local.LocalDataSource
9
6
import com.xiasuhuei321.gank_kotlin.datasource.local.LocalDataSourceImpl
@@ -12,7 +9,6 @@ import com.xiasuhuei321.gank_kotlin.datasource.remote.RemoteDataSourceImpl
12
9
import com.xiasuhuei321.gank_kotlin.extension.handleResult
13
10
import com.xiasuhuei321.gank_kotlin.extension.io_main
14
11
import io.reactivex.Observable
15
- import io.reactivex.functions.Function
16
12
17
13
/* *
18
14
* Created by coderFan on 2017/8/11.
@@ -37,44 +33,52 @@ object DataSourceImpl : DataSource {
37
33
38
34
}
39
35
40
- override fun getData (type : String ): Observable <List <GankData >> {
41
- return getRemoteData(type,10 ,1 )
36
+ override fun getRemoteData (type : String ): Observable <List <GankData >> {
37
+ return getRemoteData(type, 10 , 1 )
38
+ }
39
+
40
+ override fun getRemoteData (type : String , pageIndex : Int ): Observable <List <GankData >> {
41
+ return getRemoteData(type, 10 , pageIndex)
42
42
}
43
43
44
44
override fun getRemoteData (type : String , count : Int , pageIndex : Int ): Observable <List <GankData >> {
45
45
return remote
46
- .getRemoteData(type,10 ,1 )
46
+ .getRemoteData(type, 10 , 1 )
47
47
.compose(handleResult())
48
+ .doOnNext({ list ->
49
+ if (pageIndex == 1 ) refreshLocalData(type, list)// 只有当加载第一页数据的时候才缓存
50
+ }
51
+ )
48
52
.io_main()
49
53
}
50
54
51
55
/* *
52
56
* 清除本地指定缓存
53
57
*/
54
58
override fun clearData (type : String ) {
55
- TODO ( " 清除本地指定缓存数据 " )
59
+ local.clearLocalData(type )
56
60
}
57
61
58
62
/* *
59
63
* 清除本地所有缓存
60
64
*/
61
65
override fun clearAllData () {
62
- // TODO("清除本地所有缓存数据" )
66
+ local.clearAllData( )
63
67
}
64
68
65
69
66
70
/* *
67
71
* 优先从本地获取数据
68
72
*/
69
73
private fun getLocalData (type : String ): Observable <List <GankData >> {
70
- TODO ( " 获取本地的缓存数据 " )
74
+ return local.getLocalData(type )
71
75
}
72
76
73
77
/* *
74
78
* 刷新本地序列化存储数据
75
79
*/
76
- private fun refreshLocalData (type : String ) {
77
- // TODO("序列化存储指定数据到本地" )
80
+ private fun < T > refreshLocalData (type : String , list : Collection < T > ) {
81
+ local.refreshLocalData(type,list )
78
82
}
79
83
80
84
/* *
0 commit comments