Skip to content

Commit

Permalink
1. 升级 MMKV 和 GSON
Browse files Browse the repository at this point in the history
2. 收拢 Json 相关方法
  • Loading branch information
leavesCZY committed Jan 15, 2022
1 parent 0fa5da5 commit c6a3183
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MainActivity : AppCompatActivity() {
UserBean("公众号", "字节数组"),
UserBean("GitHub", "https://github.com/leavesCZY")
)
UserKV.map = hashMapOf(1 to "hello", 2 to "hi")
UserKV.map = mutableMapOf(1 to "hello", 2 to "hi")
}
btnPrintUserGroup.setOnClickListener {
val log = "UserKV.name: " + UserKV.name + "\n" +
Expand All @@ -76,8 +76,8 @@ class MainActivity : AppCompatActivity() {
PreferenceKV.fontSize = Random.nextInt(1, 300)
}
btnPrintPreferenceGroup.setOnClickListener {
val log = "PreferenceKV.appTheme: " + PreferenceKV.appTheme + "\n" +
"PreferenceKV.fontSize: " + PreferenceKV.fontSize
val log =
"PreferenceKV.appTheme: " + PreferenceKV.appTheme + "\n" + "PreferenceKV.fontSize: " + PreferenceKV.fontSize
log(log)
}
btnCleanPreferenceGroup.setOnClickListener {
Expand All @@ -86,9 +86,7 @@ class MainActivity : AppCompatActivity() {
btnPrintFinalKVGroup.setOnClickListener {
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
val log =
"FinalKV.firstInstallTime: " + sdf.format(Date(FinalKV.firstInstallTime)) + "\n" +
"FinalKV.firstVersionCode: " + FinalKV.firstVersionCode + "\n" +
"FinalKV.firstVersionName: " + FinalKV.firstVersionName
"FinalKV.firstInstallTime: " + sdf.format(Date(FinalKV.firstInstallTime)) + "\n" + "FinalKV.firstVersionCode: " + FinalKV.firstVersionCode + "\n" + "FinalKV.firstVersionName: " + FinalKV.firstVersionName
log(log)
}
btnClearLog.setOnClickListener {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
android:layout_weight="1"
android:padding="10dp">

<TextView
android:id="@+id/tvLog"
Expand Down
4 changes: 2 additions & 2 deletions kvholder/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
}

dependencies {
implementation 'com.google.code.gson:gson:2.8.8'
implementation 'com.tencent:mmkv-static:1.2.10'
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'com.tencent:mmkv:1.2.11'
}

afterEvaluate {
Expand Down
32 changes: 25 additions & 7 deletions kvholder/src/main/java/github/leavesczy/kvholder/IKVHolder.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package github.leavesczy.kvholder

import com.google.gson.Gson

/**
* @Author: leavesCZY
* @Date: 2021/2/21 0:07
Expand All @@ -10,20 +12,36 @@ interface IKVHolder {

companion object {

val gson = Gson()

fun toJson(ob: Any?): String {
try {
return gson.toJson(ob)
} catch (e: Throwable) {
e.printStackTrace()
}
return ""
}

inline fun <reified T> toBean(json: String?): T {
return gson.fromJson(json, T::class.java)
}

inline fun <reified T> IKVHolder.getBean(key: String): T {
return JsonHolder.toBean(get(key, ""))
return toBean(json = get(key = key, default = ""))
}

inline fun <reified T> IKVHolder.getBeanOrNull(key: String): T? {
return JsonHolder.toBeanOrNull(get(key, ""))
try {
return getBean(key = key)
} catch (e: Throwable) {
e.printStackTrace()
}
return null
}

inline fun <reified T> IKVHolder.getBeanOrDefault(key: String, defaultValue: T): T {
return JsonHolder.toBeanOrDefault(get(key, ""), defaultValue)
}

fun toJson(ob: Any?): String {
return JsonHolder.toJson(ob)
return getBeanOrNull(key = key) ?: defaultValue
}

}
Expand Down
67 changes: 0 additions & 67 deletions kvholder/src/main/java/github/leavesczy/kvholder/JsonHolder.kt

This file was deleted.

0 comments on commit c6a3183

Please sign in to comment.