Skip to content

Commit 2c82db2

Browse files
author
Karl
committed
设计本地缓存类,添加SP工具类
1 parent a500182 commit 2c82db2

File tree

12 files changed

+210
-72
lines changed

12 files changed

+210
-72
lines changed

app/src/main/AndroidManifest.xml

+2
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"

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ 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
1821
fun getRemoteData(type: String, count: Int,pageIndex: Int): Observable<List<GankData>>

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

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.xiasuhuei321.gank_kotlin.datasource
22

3-
import android.util.Log
4-
import com.xiasuhuei321.gank_kotlin.app
53
import com.xiasuhuei321.gank_kotlin.datasource.bean.GankData
6-
import com.xiasuhuei321.gank_kotlin.datasource.bean.JsonResult
74
import com.xiasuhuei321.gank_kotlin.datasource.bean.Weather
85
import com.xiasuhuei321.gank_kotlin.datasource.local.LocalDataSource
96
import com.xiasuhuei321.gank_kotlin.datasource.local.LocalDataSourceImpl
@@ -12,7 +9,6 @@ import com.xiasuhuei321.gank_kotlin.datasource.remote.RemoteDataSourceImpl
129
import com.xiasuhuei321.gank_kotlin.extension.handleResult
1310
import com.xiasuhuei321.gank_kotlin.extension.io_main
1411
import io.reactivex.Observable
15-
import io.reactivex.functions.Function
1612

1713
/**
1814
* Created by coderFan on 2017/8/11.
@@ -37,44 +33,52 @@ object DataSourceImpl : DataSource {
3733

3834
}
3935

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)
4242
}
4343

4444
override fun getRemoteData(type: String, count: Int, pageIndex: Int): Observable<List<GankData>> {
4545
return remote
46-
.getRemoteData(type,10,1)
46+
.getRemoteData(type, 10, 1)
4747
.compose(handleResult())
48+
.doOnNext({ list ->
49+
if (pageIndex == 1) refreshLocalData(type, list)//只有当加载第一页数据的时候才缓存
50+
}
51+
)
4852
.io_main()
4953
}
5054

5155
/**
5256
* 清除本地指定缓存
5357
*/
5458
override fun clearData(type: String) {
55-
TODO("清除本地指定缓存数据")
59+
local.clearLocalData(type)
5660
}
5761

5862
/**
5963
* 清除本地所有缓存
6064
*/
6165
override fun clearAllData() {
62-
// TODO("清除本地所有缓存数据")
66+
local.clearAllData()
6367
}
6468

6569

6670
/**
6771
* 优先从本地获取数据
6872
*/
6973
private fun getLocalData(type: String): Observable<List<GankData>> {
70-
TODO("获取本地的缓存数据")
74+
return local.getLocalData(type)
7175
}
7276

7377
/**
7478
* 刷新本地序列化存储数据
7579
*/
76-
private fun refreshLocalData(type: String) {
77-
// TODO("序列化存储指定数据到本地")
80+
private fun <T> refreshLocalData(type: String, list: Collection<T>) {
81+
local.refreshLocalData(type,list)
7882
}
7983

8084
/**

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

+7
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,10 @@ object ResponseCode {
7676
*/
7777
val SEVER_ERROR = 500
7878
}
79+
80+
object CacheValues{
81+
82+
val SP_CONFIG_NAME = "GankKotlin"
83+
84+
val FILE_NAME = "GankFile"
85+
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ interface LocalDataSource {
1313
//本地缓存
1414
fun getLocalData(type: String): Observable<List<GankData>>
1515

16+
fun <T> refreshLocalData(type: String,list: Collection<T>)
17+
18+
fun clearLocalData(type: String)
19+
20+
fun clearAllData()
21+
1622
fun json2DB(): Map<String, List<Town>>
1723
}

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,27 @@ import io.reactivex.Observable
66

77
/**
88
* Created by coderfan on 2017/8/11.
9-
* desc:
9+
* desc:数据缓存管理类
1010
*/
1111
class LocalDataSourceImpl : LocalDataSource {
12+
1213
override fun json2DB(): Map<String, List<Town>> {
1314
return null!!
1415
}
1516

1617
override fun getLocalData(type: String): Observable<List<GankData>> {
17-
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
18+
TODO("获取本地缓存的数据,检查缓存、sp/file/db(待定),取数据需要先本地取数据,然后检查是否有符合的数据") //To change body of created functions use File | Settings | File Templates.
19+
}
20+
21+
override fun <T> refreshLocalData(type: String, list: Collection<T>) {
22+
TODO("将数据存到本地,这里传入的list应该先装入Map,然后map转换成json再存入本地") //To change body of created functions use File | Settings | File Templates.
23+
}
24+
25+
override fun clearLocalData(type: String) {
26+
TODO("清空本地指定缓存") //To change body of created functions use File | Settings | File Templates.
27+
}
28+
29+
override fun clearAllData() {
30+
TODO("清空本地所有缓存") //To change body of created functions use File | Settings | File Templates.
1831
}
1932
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.xiasuhuei321.gank_kotlin.extension
2+
3+
import android.os.Environment
4+
import com.xiasuhuei321.gank_kotlin.GankApplication
5+
import com.xiasuhuei321.gank_kotlin.context
6+
import com.xiasuhuei321.gank_kotlin.datasource.bean.CacheValues
7+
import java.io.File
8+
import java.io.FileOutputStream
9+
import java.io.IOException
10+
import java.io.ObjectOutputStream
11+
12+
/**
13+
* Created by CoderFan on 2017/9/15.
14+
* desc:File 序列化读写
15+
*/
16+
17+
/**
18+
* 序列化对象
19+
*/
20+
@Throws(IOException::class)
21+
fun <T> setSerializable(t: T) {
22+
// if (checkDir()){
23+
// val oos:ObjectOutputStream
24+
// try {
25+
// val file = File(Environment.getExternalStorageDirectory(),CacheValues.FILE_NAME)
26+
// if (!file.exists())file.createNewFile()
27+
// val fos = FileOutputStream(file)
28+
//
29+
// oos.writeObject(t)
30+
// }catch (e:IOException){
31+
// e.printStackTrace()
32+
// }finally {
33+
// oos.close()
34+
// }
35+
// }
36+
}
37+
38+
/**
39+
* 外部存储路径检查
40+
*/
41+
fun checkDir():Boolean{
42+
return Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED)
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.xiasuhuei321.gank_kotlin.extension
2+
3+
import com.google.gson.GsonBuilder
4+
import com.google.gson.reflect.TypeToken
5+
import java.lang.reflect.Type
6+
7+
/**
8+
* Created by CoderFan on 2017/9/14.
9+
* desc:json转换
10+
*/
11+
12+
/**
13+
* 对象转换成json字符串
14+
*/
15+
fun <T> toJson(t:T):String = GsonBuilder().create().toJson(t)
16+
17+
fun <T> listToJson(list: Collection<T>):String = GsonBuilder().create().toJson(list)
18+
19+
fun <K,T> mapToJson(map: HashMap<K,T>):String = GsonBuilder().create().toJson(map)
20+
21+
22+
/**
23+
* json字符串转换指定类型
24+
*/
25+
fun <T> jsonToList(json:String):ArrayList<T>{
26+
val type = object:TypeToken<ArrayList<T>>(){}
27+
return GsonBuilder().create().fromJson<ArrayList<T>>(json,type.type)
28+
}
29+
fun <K,T> jsonToMap(json: String):Map<K,T>{
30+
val type = object : TypeToken<Map<K,T>>(){}
31+
return GsonBuilder().create().fromJson(json,type.type)
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.xiasuhuei321.gank_kotlin.extension
2+
3+
import com.xiasuhuei321.gank_kotlin.GankApplication
4+
import com.xiasuhuei321.gank_kotlin.context
5+
import com.xiasuhuei321.gank_kotlin.datasource.bean.CacheValues
6+
7+
/**
8+
* Created by CoderFan on 2017/9/14.
9+
* desc:本地存储
10+
*/
11+
12+
/**
13+
* 存入数据
14+
*/
15+
fun <T> putSPValue(key: String, value: T) {
16+
val sp = GankApplication.context.getSharedPreferences(CacheValues.SP_CONFIG_NAME, 0x0000)
17+
val edit = sp.edit()
18+
when (value) {
19+
is Boolean -> edit.putBoolean(key, value)
20+
is String -> edit.putString(key, value)
21+
is Long -> edit.putLong(key, value)
22+
is Float -> edit.putFloat(key, value)
23+
is Int -> edit.putInt(key, value)
24+
else -> throw IllegalArgumentException("u can'y use this type")
25+
}
26+
edit.apply()
27+
}
28+
29+
/**
30+
* 取出数据
31+
*/
32+
fun getSPValues(key: String, default: Any): Any {
33+
val sp = GankApplication.context.getSharedPreferences(CacheValues.SP_CONFIG_NAME, 0x0000)
34+
return when (default) {
35+
is Boolean -> sp.getBoolean(key, default)
36+
is String -> sp.getString(key, default)
37+
is Long -> sp.getLong(key, default)
38+
is Float -> sp.getFloat(key, default)
39+
is Int -> sp.getInt(key, default)
40+
else -> throw IllegalArgumentException("u can't use this type")
41+
}
42+
}
43+
44+
/**
45+
* 检查key是否存在
46+
*/
47+
fun checkSPKeys(key: String):Boolean{
48+
val sp = GankApplication.context
49+
.getSharedPreferences(CacheValues.SP_CONFIG_NAME, 0x0000)
50+
return sp.contains(key)
51+
}
52+
53+
/**
54+
* 清除指定数据
55+
*/
56+
fun clearSPCache(key: String) {
57+
val sp = GankApplication.context
58+
.getSharedPreferences(CacheValues.SP_CONFIG_NAME, 0x0000)
59+
if (sp.contains(key)){
60+
sp.edit().remove(key).apply()
61+
}
62+
}
63+
64+
/**
65+
* 清除所有数据
66+
*/
67+
fun clearSPCache() = GankApplication.context
68+
.getSharedPreferences(CacheValues.SP_CONFIG_NAME, 0x0000)
69+
.edit()
70+
.clear()
71+
.apply()
72+
73+

0 commit comments

Comments
 (0)