Skip to content

Commit a1f82da

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 900d46f + 771c0a4 commit a1f82da

20 files changed

+323
-178
lines changed

License2

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Version 1.0, September 2017
55

66
https://github.com/unbug/snts
77

8-
Copyright (C) [2017] [xiasuhuei321]
8+
Copyright (C) 2017 xiasuhuei321
99

1010
Everyone is permitted to copy and distribute verbatim copies
1111
of this license document.

app/src/main/java/com/xiasuhuei321/gank_kotlin/MainActivity.kt

+6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import android.os.Bundle
44
import android.support.v7.app.AppCompatActivity
55
import android.view.Window
66
import android.view.WindowManager
7+
import com.xiasuhuei321.gank_kotlin.datasource.DataSourceImpl
78
import com.xiasuhuei321.gank_kotlin.extension.shortToast
89
import kotlinx.android.synthetic.main.activity_main.*
910

1011
class MainActivity : AppCompatActivity() {
12+
val data = DataSourceImpl()
1113
override fun onCreate(savedInstanceState: Bundle?) {
1214
super.onCreate(savedInstanceState)
1315
supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
@@ -20,6 +22,10 @@ class MainActivity : AppCompatActivity() {
2022
}
2123
}
2224

25+
override fun onResume() {
26+
super.onResume()
27+
}
28+
2329
override fun onDestroy() {
2430
super.onDestroy()
2531
weatherWv.onDestroy()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.xiasuhuei321.gank_kotlin.datasource
2+
3+
import com.xiasuhuei321.gank_kotlin.datasource.bean.GankData
4+
import io.reactivex.Observable
5+
6+
7+
/**
8+
* Created by CoderFan on 2017/9/12.
9+
* desc:
10+
*/
11+
interface DataSource{
12+
13+
//获取Data
14+
fun getData(type:String): Observable<List<GankData>>
15+
16+
//获取服务器数据,每页数据默认为10, 页码默认为1
17+
fun getRemoteData(type: String,pageIndex:Int = 1,count:Int = 10):Observable<List<GankData>>
18+
19+
//清除本地指定缓存
20+
fun clearData(type: String)
21+
22+
//清除本地所有缓存
23+
fun clearAllData()
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.xiasuhuei321.gank_kotlin.datasource
2+
3+
import com.xiasuhuei321.gank_kotlin.datasource.bean.GankData
4+
import com.xiasuhuei321.gank_kotlin.datasource.local.LocalDataSource
5+
import com.xiasuhuei321.gank_kotlin.datasource.local.LocalDataSourceImpl
6+
import com.xiasuhuei321.gank_kotlin.datasource.remote.RemoteDataSource
7+
import com.xiasuhuei321.gank_kotlin.datasource.remote.RemoteDataSourceImpl
8+
import io.reactivex.Observable
9+
10+
/**
11+
* Created by coderFan on 2017/8/11.
12+
* author:karl
13+
14+
*/
15+
class DataSourceImpl :DataSource {
16+
17+
private val remote :RemoteDataSource
18+
19+
private val local : LocalDataSource
20+
21+
init {
22+
remote = RemoteDataSourceImpl()
23+
local = LocalDataSourceImpl()
24+
}
25+
26+
override fun getData(type: String):Observable<List<GankData>>{
27+
TODO("获取数据,进行网络请求,加载失败再从本地读取缓存")
28+
}
29+
30+
override fun getRemoteData(type: String, pageIndex: Int, count: Int): Observable<List<GankData>> {
31+
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
32+
}
33+
34+
/**
35+
* 清除本地指定缓存
36+
*/
37+
override fun clearData(type: String){
38+
TODO("清除本地指定缓存数据")
39+
}
40+
41+
/**
42+
* 清除本地所有缓存
43+
*/
44+
override fun clearAllData(){
45+
TODO("清除本地所有缓存数据")
46+
}
47+
48+
49+
/**
50+
* 优先从本地获取数据
51+
*/
52+
private fun getLocalData(type: String) : Observable<List<GankData>>{
53+
TODO("获取本地的缓存数据")
54+
55+
}
56+
57+
/**
58+
* 刷新本地序列化存储数据
59+
*/
60+
private fun refreshLocalData(type: String){
61+
TODO("序列化存储指定数据到本地")
62+
}
63+
64+
}
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,20 @@
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)
19+
20+

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/LocalData.kt

-9
This file was deleted.

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

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.xiasuhuei321.gank_kotlin.datasource.local
2+
3+
import com.xiasuhuei321.gank_kotlin.datasource.bean.GankData
4+
import io.reactivex.Observable
5+
6+
/**
7+
* Created by coderfan on 2017/8/11.
8+
* desc:
9+
*/
10+
interface LocalDataSource {
11+
12+
//本地缓存
13+
fun getRemoteData(type:String):Observable<List<GankData>>
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.xiasuhuei321.gank_kotlin.datasource.local
2+
3+
import com.xiasuhuei321.gank_kotlin.datasource.bean.GankData
4+
import io.reactivex.Observable
5+
6+
/**
7+
* Created by coderfan on 2017/8/11.
8+
* desc:
9+
*/
10+
class LocalDataSourceImpl : LocalDataSource {
11+
12+
override fun getRemoteData(type: String): Observable<List<GankData>> {
13+
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
14+
}
15+
}

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

+12-2
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
@@ -10,12 +13,19 @@ import retrofit2.http.Path
1013
* author:luo
1114
1215
*/
13-
interface ApiStore {
16+
interface ApiService {
1417
/**
1518
* http://gank.io/api/data/数据类型/请求个数/第几页
1619
*/
1720
@GET("data/{type}/{count}/{pageIndex}")
1821
fun getData(@Path("type") type: String,
1922
@Path("count") count: String,
2023
@Path("pageIndex") pageIndex: String): Observable<TechBean>
24+
25+
@GET("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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.xiasuhuei321.gank_kotlin.datasource.net
2+
3+
import android.util.Log
4+
import com.xiasuhuei321.gank_kotlin.datasource.bean.API
5+
import okhttp3.Interceptor
6+
import okhttp3.OkHttpClient
7+
import okhttp3.logging.HttpLoggingInterceptor
8+
import retrofit2.Retrofit
9+
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
10+
import retrofit2.converter.gson.GsonConverterFactory
11+
12+
/**
13+
* Created by Karl on 2017/9/7 0007.
14+
* Retrofit Client
15+
*/
16+
17+
object Network {
18+
19+
private val retrofit:Retrofit
20+
private val okHttpClient:OkHttpClient
21+
22+
init {
23+
val logging = Interceptor{chain ->
24+
val request = chain.request()
25+
Log.w("okhttp","okhttp--->"+request.url().toString())
26+
chain.proceed(request)
27+
}
28+
29+
okHttpClient =
30+
OkHttpClient.Builder().addInterceptor(logging).build()
31+
32+
retrofit =
33+
Retrofit.Builder()
34+
.baseUrl(API.BASE_PATH)
35+
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
36+
.addConverterFactory(GsonConverterFactory.create())
37+
.client(okHttpClient)
38+
.build()
39+
40+
41+
}
42+
43+
val service:ApiService by lazy { retrofit.create(ApiService::class.java) }
44+
45+
}
46+

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

-30
This file was deleted.

0 commit comments

Comments
 (0)