Skip to content

Commit d2eaf9b

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # app/src/main/java/com/xiasuhuei321/gank_kotlin/MainActivity.kt # app/src/main/res/layout/activity_main.xml
2 parents ed45fe7 + e305dc4 commit d2eaf9b

17 files changed

+407
-77
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# GankKotlin
22
A practice for kotlin, use gank.io's data.
33

4-
Now the app is under development, here are some pictures for preview.
4+
Now the app is under development, here are some pictures for preview.The native code built by CMake.
55

66
![rain](./screenshot/rain.gif)
77

app/src/main/AndroidManifest.xml

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package="com.xiasuhuei321.gank_kotlin">
44

55
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
7+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
68

79
<application
810
android:name=".GankApplication"
@@ -13,6 +15,13 @@
1315
android:supportsRtl="true"
1416
android:theme="@style/AppTheme">
1517
<activity android:name=".MainActivity">
18+
<!--<intent-filter>-->
19+
<!--<action android:name="android.intent.action.MAIN" />-->
20+
21+
<!--<category android:name="android.intent.category.LAUNCHER" />-->
22+
<!--</intent-filter>-->
23+
</activity>
24+
<activity android:name=".FileTestActivity">
1625
<intent-filter>
1726
<action android:name="android.intent.action.MAIN" />
1827

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.xiasuhuei321.gank_kotlin
2+
3+
import android.os.Bundle
4+
import android.support.v7.app.AppCompatActivity
5+
6+
/**
7+
* Created by Karl on 2017/9/21 0021.
8+
* desc:测试用,暂时别删
9+
*/
10+
class FileTestActivity : AppCompatActivity(){
11+
12+
override fun onCreate(savedInstanceState: Bundle?) {
13+
super.onCreate(savedInstanceState)
14+
setContentView(R.layout.activity_test)
15+
16+
17+
}
18+
19+
20+
21+
}

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ import io.reactivex.Observable
1212
interface DataSource {
1313

1414
// 获取Data
15-
fun getData(type: String): Observable<List<GankData>>
15+
fun getRemoteData(type: String): Observable<List<GankData>>
16+
17+
//获取Data,默认请求数据为10
18+
fun getRemoteData(type: String,pageIndex: Int):Observable<List<GankData>>
1619

1720
// 获取服务器数据,每页数据默认为10, 页码默认为1
18-
fun getRemoteData(type: String, pageIndex: Int = 1, count: Int = 10): Observable<List<GankData>>
21+
fun getRemoteData(type: String, count: Int,pageIndex: Int): Observable<List<GankData>>
1922

2023
// 清除本地指定缓存
2124
fun clearData(type: String)

app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/DataSourceImpl.kt

+23-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import com.xiasuhuei321.gank_kotlin.datasource.local.LocalDataSource
66
import com.xiasuhuei321.gank_kotlin.datasource.local.LocalDataSourceImpl
77
import com.xiasuhuei321.gank_kotlin.datasource.remote.RemoteDataSource
88
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
911
import io.reactivex.Observable
1012

1113
/**
@@ -17,7 +19,7 @@ object DataSourceImpl : DataSource {
1719

1820
private val remote: RemoteDataSource
1921

20-
private val local: LocalDataSource
22+
private val local: LocalDataSource<GankData>
2123

2224
init {
2325
remote = RemoteDataSourceImpl()
@@ -31,41 +33,52 @@ object DataSourceImpl : DataSource {
3133

3234
}
3335

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)
3638
}
3739

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()
4053
}
4154

4255
/**
4356
* 清除本地指定缓存
4457
*/
4558
override fun clearData(type: String) {
46-
TODO("清除本地指定缓存数据")
59+
local.clearLocalData(type)
4760
}
4861

4962
/**
5063
* 清除本地所有缓存
5164
*/
5265
override fun clearAllData() {
53-
// TODO("清除本地所有缓存数据")
66+
local.clearAllData()
5467
}
5568

5669

5770
/**
5871
* 优先从本地获取数据
5972
*/
6073
private fun getLocalData(type: String): Observable<List<GankData>> {
61-
TODO("获取本地的缓存数据")
74+
return Observable.just(local.getLocalData(type))
6275
}
6376

6477
/**
6578
* 刷新本地序列化存储数据
6679
*/
67-
private fun refreshLocalData(type: String) {
68-
// TODO("序列化存储指定数据到本地")
80+
private fun refreshLocalData(type: String, list: List<GankData>) {
81+
local.refreshLocalData(type,list)
6982
}
7083

7184
/**

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

+9
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,12 @@ object ResponseCode {
7676
*/
7777
val SEVER_ERROR = 500
7878
}
79+
80+
object CacheValues{
81+
82+
val SP_CONFIG_NAME = "GankKotlin"
83+
84+
val DEFAULT_FILE_NAME = "GankFile"
85+
86+
val DEFAULT_FILE_COUNT = "GankFileCount"//统计本地存储的文件
87+
}

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ package com.xiasuhuei321.gank_kotlin.datasource.bean
55
* desc:数据
66
*/
77

8-
data class JsonResult<T>(var error: Boolean, var results: T)
8+
data class JsonResult<T>(val error: Boolean, val results: T)
99

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)
10+
data class GankData(val _id: String,
11+
val createAt: String,
12+
val desc: String,
13+
val publishedAt: String,
14+
val source: String,
15+
val type: String,
16+
val url: String,
17+
val used: Boolean,
18+
val who: String){
19+
fun create() = createAt.substring(0,10)
20+
}
1921

2022

2123
data class City(val cityName: String,

app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/code/ErrorCode.kt

-33
This file was deleted.

app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/code/Type.kt

-14
This file was deleted.

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import io.reactivex.Observable
88
* Created by coderfan on 2017/8/11.
99
* desc:
1010
*/
11-
interface LocalDataSource {
11+
interface LocalDataSource<T> {
1212

1313
//本地缓存
14-
fun getLocalData(type: String): Observable<List<GankData>>
14+
fun getLocalData(type: String): List<T>
15+
16+
fun refreshLocalData(type: String,list: List<T>)
17+
18+
fun clearLocalData(type: String)
19+
20+
fun clearAllData()
1521

1622
fun json2DB(): Map<String, List<Town>>
1723
}

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

+27-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,41 @@ package com.xiasuhuei321.gank_kotlin.datasource.local
22

33
import com.xiasuhuei321.gank_kotlin.datasource.bean.GankData
44
import com.xiasuhuei321.gank_kotlin.datasource.bean.Town
5+
import com.xiasuhuei321.gank_kotlin.extension.*
56
import io.reactivex.Observable
67

78
/**
89
* Created by coderfan on 2017/8/11.
9-
* desc:
10+
* desc:数据缓存管理类
1011
*/
11-
class LocalDataSourceImpl : LocalDataSource {
12+
class LocalDataSourceImpl<T> : LocalDataSource<T> {
13+
14+
val map = HashMap<String,List<T>>()
15+
1216
override fun json2DB(): Map<String, List<Town>> {
1317
return null!!
1418
}
1519

16-
override fun getLocalData(type: String): Observable<List<GankData>> {
17-
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
20+
override fun getLocalData(type: String): List<T>{
21+
if (map.containsKey(type)){
22+
return map.get(type)!!
23+
}else{
24+
TODO("I don't know how to cast result to list")
25+
}
26+
}
27+
28+
override fun refreshLocalData(type: String, list: List<T>) {
29+
map.put(type,list)
30+
write2File(type, listToJson(list))
31+
}
32+
33+
override fun clearLocalData(type: String) {
34+
if (map.containsKey(type))map.remove(type)
35+
deleteCacheFile(type)
36+
}
37+
38+
override fun clearAllData() {
39+
map.clear()
40+
deleteAllCacheFile()
1841
}
1942
}

0 commit comments

Comments
 (0)