Skip to content

Commit e305dc4

Browse files
author
fankarl
committed
添加local数据存储,待测试
1 parent 45c4b64 commit e305dc4

File tree

10 files changed

+198
-34
lines changed

10 files changed

+198
-34
lines changed

app/src/main/AndroidManifest.xml

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
android:supportsRtl="true"
1616
android:theme="@style/AppTheme">
1717
<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">
1825
<intent-filter>
1926
<action android:name="android.intent.action.MAIN" />
2027

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

-14
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,6 @@ class MainActivity : AppCompatActivity() {
2626

2727
}
2828

29-
override fun onResume() {
30-
super.onResume()
31-
DataSourceImpl
32-
.getData(PostType.ANDROID)
33-
.subscribe(
34-
{
35-
list->Log.e("RPG","list ---> " + list)
36-
},
37-
{
38-
e->e.printStackTrace()
39-
}
40-
)
41-
}
42-
4329
override fun onDestroy() {
4430
super.onDestroy()
4531
weatherWv.onDestroy()

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object DataSourceImpl : DataSource {
1919

2020
private val remote: RemoteDataSource
2121

22-
private val local: LocalDataSource
22+
private val local: LocalDataSource<GankData>
2323

2424
init {
2525
remote = RemoteDataSourceImpl()
@@ -71,13 +71,13 @@ object DataSourceImpl : DataSource {
7171
* 优先从本地获取数据
7272
*/
7373
private fun getLocalData(type: String): Observable<List<GankData>> {
74-
return local.getLocalData(type)
74+
return Observable.just(local.getLocalData(type))
7575
}
7676

7777
/**
7878
* 刷新本地序列化存储数据
7979
*/
80-
private fun <T> refreshLocalData(type: String, list: Collection<T>) {
80+
private fun refreshLocalData(type: String, list: List<GankData>) {
8181
local.refreshLocalData(type,list)
8282
}
8383

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,7 @@ object CacheValues{
8181

8282
val SP_CONFIG_NAME = "GankKotlin"
8383

84-
val FILE_NAME = "GankFile"
84+
val DEFAULT_FILE_NAME = "GankFile"
85+
86+
val DEFAULT_FILE_COUNT = "GankFileCount"//统计本地存储的文件
8587
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ 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>
1515

16-
fun <T> refreshLocalData(type: String,list: Collection<T>)
16+
fun refreshLocalData(type: String,list: List<T>)
1717

1818
fun clearLocalData(type: String)
1919

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

+17-7
Original file line numberDiff line numberDiff line change
@@ -2,31 +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.
910
* desc:数据缓存管理类
1011
*/
11-
class LocalDataSourceImpl : LocalDataSource {
12+
class LocalDataSourceImpl<T> : LocalDataSource<T> {
13+
14+
val map = HashMap<String,List<T>>()
1215

1316
override fun json2DB(): Map<String, List<Town>> {
1417
return null!!
1518
}
1619

17-
override fun getLocalData(type: String): Observable<List<GankData>> {
18-
TODO("获取本地缓存的数据,检查缓存、sp/file/db(待定),取数据需要先本地取数据,然后检查是否有符合的数据") //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+
}
1926
}
2027

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.
28+
override fun refreshLocalData(type: String, list: List<T>) {
29+
map.put(type,list)
30+
write2File(type, listToJson(list))
2331
}
2432

2533
override fun clearLocalData(type: String) {
26-
TODO("清空本地指定缓存") //To change body of created functions use File | Settings | File Templates.
34+
if (map.containsKey(type))map.remove(type)
35+
deleteCacheFile(type)
2736
}
2837

2938
override fun clearAllData() {
30-
TODO("清空本地所有缓存") //To change body of created functions use File | Settings | File Templates.
39+
map.clear()
40+
deleteAllCacheFile()
3141
}
3242
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.xiasuhuei321.gank_kotlin.extension
22

3-
import android.os.Environment
43
import com.xiasuhuei321.gank_kotlin.GankApplication
54
import com.xiasuhuei321.gank_kotlin.context
65
import com.xiasuhuei321.gank_kotlin.datasource.bean.CacheValues
@@ -12,19 +11,117 @@ import java.io.*
1211
*/
1312

1413
/**
15-
* 序列化对象
14+
* write cache file
1615
*/
16+
fun write2File(content:Any){
17+
write2File(CacheValues.DEFAULT_FILE_NAME,content)
18+
}
19+
20+
/**
21+
* write cache file with key
22+
*/
23+
24+
@Throws(IOException::class)
25+
fun write2File(fileName:String, content: Any) {
26+
val fos = FileOutputStream(File(GankApplication.context.filesDir , fileName))
27+
val oos = ObjectOutputStream(fos)
28+
oos.writeObject(content)
29+
write2FileCount(fileName)
30+
oos.close()
31+
}
32+
33+
/**
34+
* read cache
35+
*/
36+
fun readCacheFile():Any?{
37+
return readCacheFile(CacheValues.DEFAULT_FILE_NAME)
38+
}
1739

40+
/**
41+
* read cache with key
42+
*
43+
*/
1844
@Throws(IOException::class)
19-
fun <T> serialize(t: T) {
45+
fun readCacheFile(fileName: String): Any? {
46+
val file = File(GankApplication.context.filesDir,fileName)
47+
if (!file.exists()){
48+
file.createNewFile()
49+
}
50+
if (file.length() == 0L){
51+
return null
52+
}
53+
val fis = FileInputStream(file)
54+
val ois = ObjectInputStream(fis)
55+
val result = ois.readObject()
56+
ois.close()
57+
return result
58+
}
59+
60+
/**
61+
* delete default cache
62+
*/
63+
fun deleteCacheFile() : Boolean{
64+
return deleteCacheFile(CacheValues.DEFAULT_FILE_NAME)
65+
}
2066

67+
/**
68+
* delete all cache
69+
*/
70+
fun deleteAllCacheFile(){
71+
val list = readFileCount()
72+
for(name in list){
73+
deleteFile(name)
74+
}
75+
}
76+
77+
/**
78+
* delete cache with key
79+
*/
80+
fun deleteCacheFile(fileName: String):Boolean{
81+
return when(deleteFile(fileName)){
82+
true -> {
83+
deleteFileCount(fileName)
84+
true
85+
}
86+
false -> {
87+
false
88+
}
89+
}
90+
91+
}
92+
93+
/**
94+
* delete cache count
95+
*/
96+
private fun deleteFileCount(fileName: String){
97+
deleteFile(fileName)
2198
}
2299

100+
/**
101+
* delete cache
102+
*/
103+
private fun deleteFile(fileName: String):Boolean{
104+
val file = File(GankApplication.context.filesDir,fileName)
105+
return file.exists() && file.delete()
106+
}
23107

108+
/**
109+
* write count to cache
110+
*/
111+
private fun write2FileCount(fileName: String){
112+
val local = readCacheFile(CacheValues.DEFAULT_FILE_COUNT)
113+
if (local==null){
114+
write2File(CacheValues.DEFAULT_FILE_COUNT,fileName)
115+
}else{
116+
val contents = readCacheFile(CacheValues.DEFAULT_FILE_COUNT).toString()
117+
write2File(CacheValues.DEFAULT_FILE_COUNT,contents+","+fileName)
118+
}
119+
}
24120

25121
/**
26-
* 外部存储路径检查
122+
* read count
27123
*/
28-
fun checkDir():Boolean{
29-
return Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED)
124+
private fun readFileCount():List<String>{
125+
return readCacheFile(CacheValues.DEFAULT_FILE_COUNT).toString().split(",")
30126
}
127+

app/src/main/res/layout/activity_main.xml

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
tools:context="com.xiasuhuei321.gank_kotlin.MainActivity">
77

88

9+
10+
911
<com.xiasuhuei321.gank_kotlin.customview.weather.WeatherView
1012
android:id="@+id/weatherWv"
1113
android:layout_width="match_parent"
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
7+
<TextView
8+
android:id="@+id/tv_write"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:layout_margin="10dp"
12+
android:padding="10dp"
13+
android:background="@color/colorPrimary"
14+
android:text="write"/>
15+
<TextView
16+
android:id="@+id/tv_read"
17+
android:layout_width="wrap_content"
18+
android:layout_height="wrap_content"
19+
android:background="@color/colorAccent"
20+
android:layout_margin="10dp"
21+
android:padding="10dp"
22+
android:text="read"/>
23+
24+
<TextView
25+
android:id="@+id/tv_delete"
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:background="@color/colorPrimaryDark"
29+
android:layout_margin="10dp"
30+
android:padding="10dp"
31+
android:text="delete"/>
32+
33+
<TextView
34+
android:id="@+id/tv_result"
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
android:layout_margin="20dp"/>
38+
39+
</LinearLayout>

0 commit comments

Comments
 (0)