Skip to content

Commit 7ca6348

Browse files
author
fankarl
committed
添加Remote单例类
1 parent 5450bbd commit 7ca6348

File tree

11 files changed

+160
-39
lines changed

11 files changed

+160
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.xiasuhuei321.gank_kotlin.datasource
2+
3+
/**
4+
* Created by Karl on 2017/9/11 0011.
5+
*
6+
*/
7+
8+
interface DataSource{
9+
10+
}

app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/souce/DataSource.kt app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/DataSourceManager.kt

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,50 @@
1-
package com.xiasuhuei321.gank_kotlin.datasource.souce
1+
package com.xiasuhuei321.gank_kotlin.datasource
22

3-
import com.xiasuhuei321.gank_kotlin.datasource.code.Type
3+
import com.xiasuhuei321.gank_kotlin.datasource.bean.PostType
44
import com.xiasuhuei321.gank_kotlin.datasource.local.LocalDataImpl
5-
import com.xiasuhuei321.gank_kotlin.datasource.remote.ServerDataImpl
5+
import com.xiasuhuei321.gank_kotlin.datasource.remote.RemoteDataImpl
66
import com.xiasuhuei321.gank_kotlin.extension.io_main
7-
import io.reactivex.android.schedulers.AndroidSchedulers
8-
import io.reactivex.schedulers.Schedulers
97

108
/**
119
* Created by xiasuhuei321 on 2017/8/11.
1210
* author:luo
1311
1412
*/
15-
object DataSource {
13+
object DataSourceManager :DataSource {
1614
val TAG: String = "DataSource"
17-
val server = ServerDataImpl()
15+
val server = RemoteDataImpl()
1816
val local = LocalDataImpl()
1917

2018
/**
2119
* 从网络获取数据,保存数据
2220
*/
2321
fun initData() {
24-
server.getRemoteTechBeanStaredList(Type.WELFARE, 100, 1)
22+
server.getRemoteTechBeanStaredList(PostType.WELFARE, 100, 1)
2523
.io_main()
2624
.subscribe({
2725

2826
}, {
2927
e ->
3028
e.printStackTrace()
3129
})
32-
server.getRemoteTechBeanStaredList(Type.WELFARE, 100, 1)
33-
.subscribeOn(Schedulers.newThread())
34-
.observeOn(AndroidSchedulers.mainThread())
30+
server.getRemoteTechBeanStaredList(PostType.WELFARE, 100, 1)
31+
.io_main()
3532
.subscribe({
3633

3734
}, {
3835
e ->
3936
e.printStackTrace()
4037
})
41-
server.getRemoteTechBeanStaredList(Type.WELFARE, 100, 1)
42-
.subscribeOn(Schedulers.newThread())
43-
.observeOn(AndroidSchedulers.mainThread())
38+
server.getRemoteTechBeanStaredList(PostType.WELFARE, 100, 1)
39+
.io_main()
4440
.subscribe({
4541

4642
}, {
4743
e ->
4844
e.printStackTrace()
4945
})
50-
server.getRemoteTechBeanStaredList(Type.WELFARE, 100, 1)
51-
.subscribeOn(Schedulers.newThread())
52-
.observeOn(AndroidSchedulers.mainThread())
46+
server.getRemoteTechBeanStaredList(PostType.WELFARE, 100, 1)
47+
.io_main()
5348
.subscribe({
5449

5550
}, {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.xiasuhuei321.gank_kotlin.datasource.bean
2+
3+
/**
4+
* Created by Karl on 2017/9/11 0011.
5+
* desc: 一些code集合
6+
*/
7+
8+
9+
/**
10+
* 请求API
11+
*/
12+
object API{
13+
14+
const val BASE_PATH = "http://gank.io/api/"
15+
//搜索API
16+
const val SEARCH = "search/query/listview/category/"
17+
18+
//获取历史数据
19+
const val HISTORY = "history/content/"
20+
21+
//获取分类
22+
const val DATA = "data/"
23+
24+
//每日数据
25+
const val DAY_DATA = "day/"
26+
27+
//随机数据
28+
const val RANDOM_DATA = "random/data/"
29+
30+
31+
}
32+
33+
/**
34+
* 请求类型
35+
*/
36+
object PostType{
37+
38+
// 福利 | Android | iOS | 休息视频 | 拓展资源 | 前端 | all
39+
40+
val WELFARE = "福利"
41+
val ANDROID = "Android"
42+
val IOS = "iOS"
43+
val FRONT = "前端"
44+
val ALL = "all"
45+
46+
val VIDEO = "休息视频"
47+
val MORE = "拓展资源"
48+
}
49+
50+
/**
51+
* 请求响应代码
52+
*/
53+
object ResponseCode{
54+
/**
55+
* 加载成功
56+
*/
57+
val SUCCESS = 100
58+
59+
/**
60+
* 连接超时
61+
*/
62+
val CONNECT_TIME_OUT = 200
63+
64+
/**
65+
* 网络异常
66+
*/
67+
val NET_ERROR = 300
68+
69+
/**
70+
* 本地缓存数据
71+
*/
72+
val LOCAL_CACHE = 400
73+
74+
/**
75+
* 服务器异常
76+
*/
77+
val SEVER_ERROR = 500
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.xiasuhuei321.gank_kotlin.datasource.bean
2+
3+
/**
4+
* Created by Karl on 2017/9/7 0007.
5+
* desc:数据
6+
*/
7+
8+
data class JsonResult<T>(var error:Boolean, var results:T)
9+
10+
data class GankData(var _id:String,
11+
var createAt:String,
12+
var desc:String,
13+
var publishedAt:String,
14+
var source:String,
15+
var type:String,
16+
var url:String,
17+
var used:Boolean,
18+
var who:String)

app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/bean/data.kt

-8
This file was deleted.

app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/local/LocalDataImpl.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ package com.xiasuhuei321.gank_kotlin.datasource.local
55
* author:luo
66
77
*/
8-
class LocalDataImpl : LocalData {
8+
class LocalDataImpl : LocalDataSource {
99
}

app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/local/LocalData.kt app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/local/LocalDataSource.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ package com.xiasuhuei321.gank_kotlin.datasource.local
55
* author:luo
66
77
*/
8-
interface LocalData {
8+
interface LocalDataSource {
99
}

app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/ApiStore.kt app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/net/ApiStore.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
package com.xiasuhuei321.gank_kotlin.datasource
1+
package com.xiasuhuei321.gank_kotlin.datasource.net
22

3+
import com.xiasuhuei321.gank_kotlin.datasource.bean.API
4+
import com.xiasuhuei321.gank_kotlin.datasource.bean.GankData
5+
import com.xiasuhuei321.gank_kotlin.datasource.bean.JsonResult
36
import com.xiasuhuei321.gank_kotlin.datasource.bean.TechBean
47
import io.reactivex.Observable
58
import retrofit2.http.GET
@@ -18,4 +21,11 @@ interface ApiStore {
1821
fun getData(@Path("type") type: String,
1922
@Path("count") count: String,
2023
@Path("pageIndex") pageIndex: String): Observable<TechBean>
24+
25+
@GET(API.DATA+"/{type}/{count}/{pageIndex}")
26+
fun getCategoricalData(
27+
@Path("type") type:String,
28+
@Path("count") count: String,
29+
@Path("pageIndex") pageIndex: String
30+
): Observable<JsonResult<List<GankData>>>
2131
}

app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/net/RetrofitHelper.kt app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/net/ReHelper.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xiasuhuei321.gank_kotlin.datasource.net
22

3+
import com.xiasuhuei321.gank_kotlin.datasource.bean.API
34
import okhttp3.OkHttpClient
45
import okhttp3.logging.HttpLoggingInterceptor
56
import retrofit2.Retrofit
@@ -11,15 +12,17 @@ import retrofit2.converter.gson.GsonConverterFactory
1112
* Retrofit Client
1213
*/
1314

14-
object RetrofitHelper {
15+
object ReHelper {
16+
17+
1518

1619
fun getInstance(): Retrofit{
1720

1821
val loggingInterceptor = HttpLoggingInterceptor()
1922
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
2023

2124
return Retrofit.Builder()
22-
.baseUrl("http://gank.io/api/")
25+
.baseUrl(API.BASE_PATH)
2326
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
2427
.addConverterFactory(GsonConverterFactory.create())
2528
.client(OkHttpClient().newBuilder().addInterceptor(loggingInterceptor).build())
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.xiasuhuei321.gank_kotlin.datasource.remote
22

3-
import com.xiasuhuei321.gank_kotlin.datasource.ApiStore
3+
import com.xiasuhuei321.gank_kotlin.datasource.net.ApiStore
4+
import com.xiasuhuei321.gank_kotlin.datasource.bean.GankData
5+
import com.xiasuhuei321.gank_kotlin.datasource.bean.JsonResult
46
import com.xiasuhuei321.gank_kotlin.datasource.bean.TechBean
5-
import com.xiasuhuei321.gank_kotlin.datasource.net.RetrofitHelper
7+
import com.xiasuhuei321.gank_kotlin.datasource.net.ReHelper
8+
import com.xiasuhuei321.gank_kotlin.extension.io_main
69
import io.reactivex.Observable
710

811

@@ -11,15 +14,20 @@ import io.reactivex.Observable
1114
* author:luo
1215
1316
*/
14-
class ServerDataImpl : ServerData {
17+
class RemoteDataImpl : RemoteDataSource {
1518

16-
val TAG: String = "ServerDataImpl"
17-
18-
var apiStore: ApiStore? = null
19+
private var apiStore: ApiStore? = null
1920

2021
init {
21-
val retrofit = RetrofitHelper.getInstance()
22-
apiStore = retrofit.create(ApiStore::class.java)
22+
if (apiStore==null) ReHelper.getInstance().create(ApiStore::class.java)
23+
}
24+
25+
companion object {
26+
val INSTANCE: RemoteDataImpl by lazy { this.INSTANCE }
27+
}
28+
29+
override fun getRemoteData(type: String, count: Int, pageIndex: Int): Observable<JsonResult<List<GankData>>> {
30+
return apiStore!!.getCategoricalData(type,count.toString(),pageIndex.toString()).io_main()
2331
}
2432

2533
override fun getRemoteTechBeanStaredList(type: String, count: Int, pageIndex: Int): Observable<TechBean> {
@@ -32,4 +40,5 @@ class ServerDataImpl : ServerData {
3240
val newPageIndex = pageIndex.toString()
3341
return apiStore!!.getData(type, newCount, newPageIndex)
3442
}
43+
3544
}

app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/remote/ServerData.kt app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/remote/RemoteDataSource.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.xiasuhuei321.gank_kotlin.datasource.remote
22

3+
import com.xiasuhuei321.gank_kotlin.datasource.bean.GankData
4+
import com.xiasuhuei321.gank_kotlin.datasource.bean.JsonResult
35
import com.xiasuhuei321.gank_kotlin.datasource.bean.TechBean
46
import io.reactivex.Observable
57

@@ -9,7 +11,11 @@ import io.reactivex.Observable
911
* author:luo
1012
1113
*/
12-
interface ServerData {
14+
interface RemoteDataSource {
1315

1416
fun getRemoteTechBeanStaredList(type: String, count: Int, pageIndex: Int): Observable<TechBean>
17+
18+
//获取分类数据
19+
fun getRemoteData(type:String, count: Int,pageIndex: Int):Observable<JsonResult<List<GankData>>>
20+
1521
}

0 commit comments

Comments
 (0)