├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── themes.xml
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── java
│ │ │ └── github
│ │ │ │ └── leavesczy
│ │ │ │ └── kvholder
│ │ │ │ └── samples
│ │ │ │ ├── Models.kt
│ │ │ │ ├── MainApplication.kt
│ │ │ │ ├── CacheProviderr.kt
│ │ │ │ └── MainActivity.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── github
│ │ │ └── leavesczy
│ │ │ └── kvholder
│ │ │ └── samples
│ │ │ └── ExampleUnitTest.kt
│ └── androidTest
│ │ └── java
│ │ └── github
│ │ └── leavesczy
│ │ └── kvholder
│ │ └── samples
│ │ └── ExampleInstrumentedTest.kt
├── proguard-rules.pro
└── build.gradle.kts
├── kvholder
├── .gitignore
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── github
│ │ └── leavesczy
│ │ └── kvholder
│ │ ├── KVHolder.kt
│ │ ├── KVLogLevel.kt
│ │ ├── MMKVKVHolder.kt
│ │ ├── IKVHolder.kt
│ │ └── KVDelegate.kt
├── consumer-rules.pro
├── proguard-rules.pro
└── build.gradle.kts
├── doc
└── key.jks
├── .gitignore
├── gradle
├── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
└── libs.versions.toml
├── README.md
├── settings.gradle.kts
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | /.idea
--------------------------------------------------------------------------------
/kvholder/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/kvholder/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/doc/key.jks:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leavesCZY/KVHolder/HEAD/doc/key.jks
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /build
5 | .idea
6 | /.kotlin/
--------------------------------------------------------------------------------
/kvholder/consumer-rules.pro:
--------------------------------------------------------------------------------
1 | # Keep all native methods, their classes and any classes in their descriptors
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | KVHolder
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leavesCZY/KVHolder/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # KVHolder
2 |
3 | 一个基于 Kotlin 对 MMKV 进行封装的 key-value 存储库,简化了 MMKV 的使用步骤
4 |
5 | 接入指南:[Wiki](https://github.com/leavesCZY/KVHolder/wiki)
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leavesCZY/KVHolder/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leavesCZY/KVHolder/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leavesCZY/KVHolder/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leavesCZY/KVHolder/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leavesCZY/KVHolder/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leavesCZY/KVHolder/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FF0277BD
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
--------------------------------------------------------------------------------
/app/src/main/java/github/leavesczy/kvholder/samples/Models.kt:
--------------------------------------------------------------------------------
1 | package github.leavesczy.kvholder.samples
2 |
3 | import android.os.Parcelable
4 | import kotlinx.parcelize.Parcelize
5 |
6 | /**
7 | * @Author: leavesCZY
8 | * @Date: 2023/12/5 14:22
9 | * @Desc:
10 | */
11 | data class JsonModel(
12 | val name: String,
13 | val age: Int
14 | )
15 |
16 | @Parcelize
17 | data class ParcelizeModel(
18 | val name: String?,
19 | val age: Int
20 | ) : Parcelable
--------------------------------------------------------------------------------
/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | @file:Suppress("UnstableApiUsage")
2 |
3 | pluginManagement {
4 | repositories {
5 | google()
6 | mavenCentral()
7 | gradlePluginPortal()
8 | }
9 | }
10 |
11 | dependencyResolutionManagement {
12 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
13 | repositories {
14 | google()
15 | mavenCentral()
16 | }
17 | }
18 |
19 | rootProject.name = "KVHolder"
20 | include(":app")
21 | include(":kvholder")
--------------------------------------------------------------------------------
/app/src/test/java/github/leavesczy/kvholder/samples/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package github.leavesczy.kvholder.samples
2 |
3 | import org.junit.Assert.assertEquals
4 | import org.junit.Test
5 |
6 | /**
7 | * Example local unit test, which will execute on the development machine (host).
8 | *
9 | * See [testing documentation](http://d.android.com/tools/testing).
10 | */
11 | class ExampleUnitTest {
12 | @Test
13 | fun addition_isCorrect() {
14 | assertEquals(4, 2 + 2)
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
11 |
12 |
--------------------------------------------------------------------------------
/kvholder/src/main/java/github/leavesczy/kvholder/KVHolder.kt:
--------------------------------------------------------------------------------
1 | package github.leavesczy.kvholder
2 |
3 | import android.content.Context
4 | import com.tencent.mmkv.MMKV
5 | import java.io.File
6 |
7 | /**
8 | * @Author: leavesCZY
9 | * @Date: 2023/12/10 16:38
10 | * @Desc:
11 | */
12 | object KVHolder {
13 |
14 | fun init(
15 | context: Context,
16 | rootDir: String = context.filesDir.absolutePath + "/mmkv",
17 | logLevel: KVLogLevel = KVLogLevel.LevelWarning
18 | ) {
19 | val file = File(rootDir)
20 | if (!file.exists()) {
21 | file.mkdirs()
22 | }
23 | MMKV.initialize(
24 | context.applicationContext,
25 | rootDir,
26 | logLevel.toMMKVLogLevel
27 | )
28 | }
29 |
30 | }
--------------------------------------------------------------------------------
/app/src/main/java/github/leavesczy/kvholder/samples/MainApplication.kt:
--------------------------------------------------------------------------------
1 | package github.leavesczy.kvholder.samples
2 |
3 | import android.app.Application
4 | import android.content.Context
5 | import github.leavesczy.kvholder.KVHolder
6 | import github.leavesczy.kvholder.KVLogLevel
7 |
8 | /**
9 | * @Author: leavesCZY
10 | * @Date: 2023/12/5 14:20
11 | * @Desc:
12 | */
13 | class MainApplication : Application() {
14 |
15 | override fun onCreate() {
16 | super.onCreate()
17 | initKVHolder(context = this)
18 | }
19 |
20 | private fun initKVHolder(context: Context) {
21 | KVHolder.init(
22 | context = context,
23 | rootDir = context.filesDir.absolutePath + "/mmkv",
24 | logLevel = KVLogLevel.LevelWarning
25 | )
26 | }
27 |
28 | }
--------------------------------------------------------------------------------
/kvholder/src/main/java/github/leavesczy/kvholder/KVLogLevel.kt:
--------------------------------------------------------------------------------
1 | package github.leavesczy.kvholder
2 |
3 | import com.tencent.mmkv.MMKVLogLevel
4 |
5 | /**
6 | * @Author: leavesCZY
7 | * @Date: 2023/12/10 16:38
8 | * @Desc:
9 | */
10 | enum class KVLogLevel {
11 |
12 | LevelDebug,
13 | LevelInfo,
14 | LevelWarning,
15 | LevelError,
16 | LevelNone;
17 |
18 | internal val toMMKVLogLevel: MMKVLogLevel
19 | get() {
20 | return when (this) {
21 | LevelDebug -> MMKVLogLevel.LevelDebug
22 | LevelInfo -> MMKVLogLevel.LevelInfo
23 | LevelWarning -> MMKVLogLevel.LevelWarning
24 | LevelError -> MMKVLogLevel.LevelError
25 | LevelNone -> MMKVLogLevel.LevelNone
26 | }
27 | }
28 |
29 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.kts.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/kvholder/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.kts.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/github/leavesczy/kvholder/samples/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package github.leavesczy.kvholder.samples
2 |
3 | import androidx.test.ext.junit.runners.AndroidJUnit4
4 | import androidx.test.platform.app.InstrumentationRegistry
5 | import org.junit.Assert.assertEquals
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | /**
10 | * Instrumented test, which will execute on an Android device.
11 | *
12 | * See [testing documentation](http://d.android.com/tools/testing).
13 | */
14 | @RunWith(AndroidJUnit4::class)
15 | class ExampleInstrumentedTest {
16 | @Test
17 | fun useAppContext() {
18 | // Context of the app under test.
19 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
20 | assertEquals("github.leavesczy.kvholder.samples", appContext.packageName)
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
13 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | kotlin.code.style=official
18 | android.useAndroidX=true
19 | android.nonFinalResIds=true
20 | android.nonTransitiveRClass=true
21 | android.enableR8.fullMode=true
22 | android.defaults.buildfeatures.buildconfig=false
23 | android.defaults.buildfeatures.resvalues=false
24 | android.defaults.buildfeatures.shaders=false
--------------------------------------------------------------------------------
/gradle/libs.versions.toml:
--------------------------------------------------------------------------------
1 | [versions]
2 | android-plugin = "8.7.0"
3 | kotlin-plugin = "2.0.20"
4 |
5 | junit-junit = "4.13.2"
6 | androidx-junit = "1.2.1"
7 | androidx-espresso = "3.6.1"
8 | androidx-appcompat = "1.7.0"
9 |
10 | google-material = "1.12.0"
11 | google-gson = "2.11.0"
12 |
13 | tencent-mmkv = "1.3.9"
14 |
15 | kvholder-remote = "1.0.8"
16 | kvholder-publishing = "1.0.8"
17 |
18 | [plugins]
19 | android-application = { id = "com.android.application", version.ref = "android-plugin" }
20 | android-library = { id = "com.android.library", version.ref = "android-plugin" }
21 | kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin-plugin" }
22 | kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin-plugin" }
23 |
24 | [libraries]
25 | androidx-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-junit" }
26 | androidx-espresso = { module = "androidx.test.espresso:espresso-core", version.ref = "androidx-espresso" }
27 | androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" }
28 |
29 | google-material = { module = "com.google.android.material:material", version.ref = "google-material" }
30 | google-gson = { module = "com.google.code.gson:gson", version.ref = "google-gson" }
31 |
32 | tencent-mmkv = { module = "com.tencent:mmkv", version.ref = "tencent-mmkv" }
33 |
34 | junit = { module = "junit:junit", version.ref = "junit-junit" }
35 |
36 | kvholder = { module = "io.github.leavesczy:kvholder", version.ref = "kvholder-remote" }
--------------------------------------------------------------------------------
/app/src/main/java/github/leavesczy/kvholder/samples/CacheProviderr.kt:
--------------------------------------------------------------------------------
1 | package github.leavesczy.kvholder.samples
2 |
3 | import github.leavesczy.kvholder.IKVHolder
4 | import github.leavesczy.kvholder.MMKVKVHolder
5 |
6 | /**
7 | * @Author: leavesCZY
8 | * @Date: 2023/12/5 14:20
9 | * @Desc:
10 | */
11 | //设置 encryptKey 以进行加密存储
12 | //该值可以自由定义,但设置后不能再次修改,否则数据将丢失
13 | private val UserKVHolder = MMKVKVHolder(
14 | keyGroup = "user",
15 | encryptKey = "encryptKey"
16 | )
17 |
18 | private val PreferenceKVHolder = MMKVKVHolder(
19 | keyGroup = "preference"
20 | )
21 |
22 | object UserKV : IKVHolder by UserKVHolder {
23 |
24 | var intValue by int(defaultValue = -1)
25 |
26 | var longValue by long(defaultValue = -1L)
27 |
28 | var floatValue by float(defaultValue = -1f)
29 |
30 | var doubleValue by double(defaultValue = -1.0)
31 |
32 | var booleanValue by boolean(defaultValue = false)
33 |
34 | var stringValue by string(defaultValue = "")
35 |
36 | var parcelableValue by parcelable(
37 | clazz = ParcelizeModel::class.java,
38 | defaultValue = ParcelizeModel(
39 | name = "default",
40 | age = 0
41 | )
42 | )
43 |
44 | var parcelableValueNullEnabled by parcelable(clazz = ParcelizeModel::class.java)
45 |
46 | var jsonValue by json(
47 | clazz = JsonModel::class.java,
48 | defaultValue = JsonModel(
49 | name = "default",
50 | age = 0
51 | )
52 | )
53 |
54 | var jsonValueNullEnabled by json(clazz = JsonModel::class.java)
55 |
56 | }
57 |
58 | object PreferenceKV : IKVHolder by PreferenceKVHolder {
59 |
60 | var parcelableValue by parcelable(
61 | clazz = ParcelizeModel::class.java,
62 | defaultValue = ParcelizeModel(
63 | name = "default",
64 | age = 0
65 | )
66 | )
67 |
68 | var parcelableValueNullEnabled by parcelable(clazz = ParcelizeModel::class.java)
69 |
70 | }
--------------------------------------------------------------------------------
/app/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | alias(libs.plugins.android.application)
3 | alias(libs.plugins.kotlin.android)
4 | alias(libs.plugins.kotlin.parcelize)
5 | }
6 |
7 | android {
8 | namespace = "github.leavesczy.kvholder.samples"
9 | compileSdk = 34
10 | defaultConfig {
11 | applicationId = "github.leavesczy.kvholder.samples"
12 | minSdk = 21
13 | targetSdk = 34
14 | versionCode = 1
15 | versionName = "1.0.0"
16 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
17 | }
18 | signingConfigs {
19 | create("release") {
20 | storeFile =
21 | File(rootDir.absolutePath + File.separator + "doc" + File.separator + "key.jks")
22 | keyAlias = "leavesCZY"
23 | storePassword = "123456"
24 | keyPassword = "123456"
25 | enableV1Signing = true
26 | enableV2Signing = true
27 | }
28 | }
29 | buildTypes {
30 | debug {
31 | signingConfig = signingConfigs.getByName("release")
32 | isMinifyEnabled = false
33 | isShrinkResources = false
34 | isDebuggable = true
35 | proguardFiles(
36 | getDefaultProguardFile("proguard-android-optimize.txt"),
37 | "proguard-rules.pro"
38 | )
39 | }
40 | release {
41 | signingConfig = signingConfigs.getByName("release")
42 | isMinifyEnabled = true
43 | isShrinkResources = true
44 | isDebuggable = false
45 | proguardFiles(
46 | getDefaultProguardFile("proguard-android-optimize.txt"),
47 | "proguard-rules.pro"
48 | )
49 | }
50 | }
51 | compileOptions {
52 | sourceCompatibility = JavaVersion.VERSION_1_8
53 | targetCompatibility = JavaVersion.VERSION_1_8
54 | }
55 | kotlinOptions {
56 | jvmTarget = JavaVersion.VERSION_1_8.toString()
57 | }
58 | }
59 |
60 | dependencies {
61 | testImplementation(libs.junit)
62 | androidTestImplementation(libs.androidx.junit)
63 | androidTestImplementation(libs.androidx.espresso)
64 | implementation(libs.androidx.appcompat)
65 | implementation(libs.google.material)
66 | // implementation(libs.kvholder)
67 | implementation(project(":kvholder"))
68 | }
--------------------------------------------------------------------------------
/kvholder/src/main/java/github/leavesczy/kvholder/MMKVKVHolder.kt:
--------------------------------------------------------------------------------
1 | package github.leavesczy.kvholder
2 |
3 | import android.os.Parcelable
4 | import com.tencent.mmkv.MMKV
5 |
6 | /**
7 | * @Author: leavesCZY
8 | * @Date: 2023/12/10 16:38
9 | * @Desc:
10 | */
11 | /**
12 | * @param keyGroup 用于指定数据分组,不同分组下的数据互不关联
13 | * @param encryptKey 加密 key,如果为空则表示不进行加密
14 | */
15 | class MMKVKVHolder(
16 | private val keyGroup: String,
17 | encryptKey: String? = null
18 | ) : IKVHolder {
19 |
20 | private val kv: MMKV = if (encryptKey.isNullOrBlank()) {
21 | MMKV.mmkvWithID(
22 | keyGroup,
23 | MMKV.MULTI_PROCESS_MODE
24 | )
25 | } else {
26 | MMKV.mmkvWithID(
27 | keyGroup,
28 | MMKV.MULTI_PROCESS_MODE,
29 | encryptKey
30 | )
31 | }
32 |
33 | override fun get(key: String, defaultValue: Int): Int {
34 | return kv.decodeInt(key, defaultValue)
35 | }
36 |
37 | override fun get(key: String, defaultValue: Long): Long {
38 | return kv.decodeLong(key, defaultValue)
39 | }
40 |
41 | override fun get(key: String, defaultValue: Float): Float {
42 | return kv.decodeFloat(key, defaultValue)
43 | }
44 |
45 | override fun get(key: String, defaultValue: Double): Double {
46 | return kv.decodeDouble(key, defaultValue)
47 | }
48 |
49 | override fun get(key: String, defaultValue: Boolean): Boolean {
50 | return kv.decodeBool(key, defaultValue)
51 | }
52 |
53 | override fun get(key: String, defaultValue: String): String {
54 | return kv.decodeString(key, defaultValue) ?: defaultValue
55 | }
56 |
57 | override fun get(key: String, clazz: Class): T? {
58 | return kv.decodeParcelable(key, clazz)
59 | }
60 |
61 | override fun set(key: String, value: Int) {
62 | kv.encode(key, value)
63 | }
64 |
65 | override fun set(key: String, value: Long) {
66 | kv.encode(key, value)
67 | }
68 |
69 | override fun set(key: String, value: Float) {
70 | kv.encode(key, value)
71 | }
72 |
73 | override fun set(key: String, value: Double) {
74 | kv.encode(key, value)
75 | }
76 |
77 | override fun set(key: String, value: Boolean) {
78 | kv.encode(key, value)
79 | }
80 |
81 | override fun set(key: String, value: String) {
82 | kv.encode(key, value)
83 | }
84 |
85 | override fun set(key: String, value: T?) {
86 | kv.encode(key, value)
87 | }
88 |
89 | override fun removeKey(vararg keys: String) {
90 | kv.removeValuesForKeys(keys)
91 | }
92 |
93 | override fun clear() {
94 | kv.clearAll()
95 | }
96 |
97 | }
--------------------------------------------------------------------------------
/kvholder/src/main/java/github/leavesczy/kvholder/IKVHolder.kt:
--------------------------------------------------------------------------------
1 | package github.leavesczy.kvholder
2 |
3 | import android.os.Parcelable
4 |
5 | /**
6 | * @Author: leavesCZY
7 | * @Date: 2023/12/10 16:38
8 | * @Desc:
9 | */
10 | interface IKVHolder {
11 |
12 | fun int(defaultValue: Int): IntDelegate {
13 | return IntDelegate(kvHolder = this, defaultValue = defaultValue)
14 | }
15 |
16 | fun long(defaultValue: Long): LongDelegate {
17 | return LongDelegate(kvHolder = this, defaultValue = defaultValue)
18 | }
19 |
20 | fun float(defaultValue: Float): FloatDelegate {
21 | return FloatDelegate(kvHolder = this, defaultValue = defaultValue)
22 | }
23 |
24 | fun double(defaultValue: Double): DoubleDelegate {
25 | return DoubleDelegate(kvHolder = this, defaultValue = defaultValue)
26 | }
27 |
28 | fun boolean(defaultValue: Boolean): BooleanDelegate {
29 | return BooleanDelegate(kvHolder = this, defaultValue = defaultValue)
30 | }
31 |
32 | fun string(defaultValue: String): StringDelegate {
33 | return StringDelegate(kvHolder = this, defaultValue = defaultValue)
34 | }
35 |
36 | fun parcelable(clazz: Class, defaultValue: T): ParcelableDelegate {
37 | return ParcelableDelegate(kvHolder = this, clazz = clazz, defaultValue = defaultValue)
38 | }
39 |
40 | fun parcelable(clazz: Class): ParcelableNullEnabledDelegate {
41 | return ParcelableNullEnabledDelegate(kvHolder = this, clazz = clazz)
42 | }
43 |
44 | fun json(clazz: Class, defaultValue: T): JsonDelegate {
45 | return JsonDelegate(kvHolder = this, clazz = clazz, defaultValue = defaultValue)
46 | }
47 |
48 | fun json(clazz: Class): JsonNullEnabledDelegate {
49 | return JsonNullEnabledDelegate(kvHolder = this, clazz = clazz)
50 | }
51 |
52 | fun get(key: String, defaultValue: Int): Int
53 |
54 | fun get(key: String, defaultValue: Long): Long
55 |
56 | fun get(key: String, defaultValue: Float): Float
57 |
58 | fun get(key: String, defaultValue: Double): Double
59 |
60 | fun get(key: String, defaultValue: Boolean): Boolean
61 |
62 | fun get(key: String, defaultValue: String): String
63 |
64 | fun get(key: String, clazz: Class): T?
65 |
66 | fun set(key: String, value: Int)
67 |
68 | fun set(key: String, value: Long)
69 |
70 | fun set(key: String, value: Float)
71 |
72 | fun set(key: String, value: Double)
73 |
74 | fun set(key: String, value: Boolean)
75 |
76 | fun set(key: String, value: String)
77 |
78 | fun set(key: String, value: T?)
79 |
80 | fun removeKey(vararg keys: String)
81 |
82 | fun clear()
83 |
84 | }
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
19 |
20 |
27 |
28 |
35 |
36 |
44 |
45 |
52 |
53 |
60 |
61 |
69 |
70 |
75 |
76 |
80 |
81 |
82 |
83 |
--------------------------------------------------------------------------------
/kvholder/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | alias(libs.plugins.android.library)
3 | alias(libs.plugins.kotlin.android)
4 | id("maven-publish")
5 | id("signing")
6 | }
7 |
8 | android {
9 | namespace = "github.leavesczy.kvholder"
10 | compileSdk = 34
11 | defaultConfig {
12 | minSdk = 21
13 | consumerProguardFiles.add(File("consumer-rules.pro"))
14 | }
15 | compileOptions {
16 | sourceCompatibility = JavaVersion.VERSION_1_8
17 | targetCompatibility = JavaVersion.VERSION_1_8
18 | }
19 | kotlinOptions {
20 | jvmTarget = JavaVersion.VERSION_1_8.toString()
21 | }
22 | publishing {
23 | singleVariant("release") {
24 | withSourcesJar()
25 | withJavadocJar()
26 | }
27 | }
28 | }
29 |
30 | dependencies {
31 | implementation(libs.tencent.mmkv)
32 | implementation(libs.google.gson)
33 | }
34 |
35 | val signingKeyId = properties["signing.keyId"]?.toString()
36 | val signingPassword = properties["signing.password"]?.toString()
37 | val signingSecretKeyRingFile = properties["signing.secretKeyRingFile"]?.toString()
38 | val mavenCentralUserName = properties["mavenCentral.username"]?.toString()
39 | val mavenCentralEmail = properties["mavenCentral.email"]?.toString()
40 | val mavenCentralPassword = properties["mavenCentral.password"]?.toString()
41 |
42 | if (signingKeyId != null
43 | && signingPassword != null
44 | && signingSecretKeyRingFile != null
45 | && mavenCentralUserName != null
46 | && mavenCentralEmail != null
47 | && mavenCentralPassword != null
48 | ) {
49 | publishing {
50 | publications {
51 | create("release") {
52 | groupId = "io.github.leavesczy"
53 | artifactId = "kvholder"
54 | version = libs.versions.kvholder.publishing.get()
55 | afterEvaluate {
56 | from(components["release"])
57 | }
58 | pom {
59 | name = "KVHolder"
60 | description = "MMKV ktx"
61 | url = "https://github.com/leavesCZY/KVHolder"
62 | licenses {
63 | license {
64 | name = "The Apache License, Version 2.0"
65 | url = "https://www.apache.org/licenses/LICENSE-2.0.txt"
66 | distribution = "https://www.apache.org/licenses/LICENSE-2.0.txt"
67 | }
68 | }
69 | developers {
70 | developer {
71 | id = "leavesCZY"
72 | name = "leavesCZY"
73 | email = mavenCentralEmail
74 | }
75 | }
76 | scm {
77 | url = "https://github.com/leavesCZY/KVHolder"
78 | connection = "https://github.com/leavesCZY/KVHolder"
79 | developerConnection = "https://github.com/leavesCZY/KVHolder"
80 | }
81 | }
82 | }
83 | }
84 | repositories {
85 | maven {
86 | setUrl("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
87 | credentials {
88 | username = mavenCentralUserName
89 | password = mavenCentralPassword
90 | }
91 | }
92 | }
93 | }
94 | signing {
95 | sign(publishing.publications["release"])
96 | }
97 | }
--------------------------------------------------------------------------------
/app/src/main/java/github/leavesczy/kvholder/samples/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package github.leavesczy.kvholder.samples
2 |
3 | import android.os.Bundle
4 | import android.util.Log
5 | import android.view.View
6 | import android.widget.TextView
7 | import androidx.appcompat.app.AppCompatActivity
8 | import kotlin.random.Random
9 |
10 | /**
11 | * @Author: leavesCZY
12 | * @Date: 2023/12/5 14:20
13 | * @Desc:
14 | */
15 | class MainActivity : AppCompatActivity() {
16 |
17 | private val btnSetUserGroup by lazy {
18 | findViewById(R.id.btnSetUserGroup)
19 | }
20 |
21 | private val btnPrintUserGroup by lazy {
22 | findViewById(R.id.btnPrintUserGroup)
23 | }
24 |
25 | private val btnCleanUserGroup by lazy {
26 | findViewById(R.id.btnCleanUserGroup)
27 | }
28 |
29 | private val btnSetPreferenceGroup by lazy {
30 | findViewById(R.id.btnSetPreferenceGroup)
31 | }
32 |
33 | private val btnPrintPreferenceGroup by lazy {
34 | findViewById(R.id.btnPrintPreferenceGroup)
35 | }
36 |
37 | private val btnCleanPreferenceGroup by lazy {
38 | findViewById(R.id.btnCleanPreferenceGroup)
39 | }
40 |
41 | private val btnClearLog by lazy {
42 | findViewById(R.id.btnClearLog)
43 | }
44 |
45 | private val tvLog by lazy {
46 | findViewById(R.id.tvLog)
47 | }
48 |
49 | override fun onCreate(savedInstanceState: Bundle?) {
50 | super.onCreate(savedInstanceState)
51 | setContentView(R.layout.activity_main)
52 | btnSetUserGroup.setOnClickListener {
53 | UserKV.intValue = randomInt()
54 | UserKV.longValue = randomInt().toLong()
55 | UserKV.floatValue = randomInt().toFloat()
56 | UserKV.doubleValue = randomInt().toDouble()
57 | UserKV.booleanValue = randomBoolean()
58 | UserKV.stringValue = randomWord()
59 | UserKV.parcelableValue = ParcelizeModel(
60 | name = randomWord(),
61 | age = randomInt()
62 | )
63 | UserKV.parcelableValueNullEnabled = if (randomBoolean()) {
64 | ParcelizeModel(
65 | name = randomWord(),
66 | age = randomInt()
67 | )
68 | } else {
69 | null
70 | }
71 | UserKV.jsonValue = JsonModel(
72 | name = randomWord(),
73 | age = randomInt()
74 | )
75 | UserKV.jsonValueNullEnabled = if (randomBoolean()) {
76 | JsonModel(
77 | name = randomWord(),
78 | age = randomInt()
79 | )
80 | } else {
81 | null
82 | }
83 | }
84 | btnPrintUserGroup.setOnClickListener {
85 | val string = buildString {
86 | append("intValue: " + UserKV.intValue)
87 | append("\n")
88 | append("\n")
89 | append("longValue: " + UserKV.longValue)
90 | append("\n")
91 | append("\n")
92 | append("floatValue: " + UserKV.floatValue)
93 | append("\n")
94 | append("\n")
95 | append("doubleValue: " + UserKV.doubleValue)
96 | append("\n")
97 | append("\n")
98 | append("booleanValue: " + UserKV.booleanValue)
99 | append("\n")
100 | append("\n")
101 | append("stringValue: " + UserKV.stringValue)
102 | append("\n")
103 | append("\n")
104 | append("parcelableValue: " + UserKV.parcelableValue)
105 | append("\n")
106 | append("\n")
107 | append("parcelableValueNullEnabled: " + UserKV.parcelableValueNullEnabled)
108 | append("\n")
109 | append("\n")
110 | append("jsonValue: " + UserKV.jsonValue)
111 | append("\n")
112 | append("\n")
113 | append("jsonValueNullEnabled: " + UserKV.jsonValueNullEnabled)
114 | }
115 | log(log = string)
116 | }
117 | btnCleanUserGroup.setOnClickListener {
118 | UserKV.clear()
119 | }
120 | btnSetPreferenceGroup.setOnClickListener {
121 | PreferenceKV.parcelableValue = ParcelizeModel(
122 | name = randomWord(),
123 | age = randomInt()
124 | )
125 | PreferenceKV.parcelableValueNullEnabled = if (randomBoolean()) {
126 | ParcelizeModel(
127 | name = if (randomBoolean()) {
128 | null
129 | } else {
130 | randomWord()
131 | },
132 | age = randomInt()
133 | )
134 | } else {
135 | null
136 | }
137 | }
138 | btnPrintPreferenceGroup.setOnClickListener {
139 | val string = buildString {
140 | append("parcelableValue: " + PreferenceKV.parcelableValue)
141 | append("\n")
142 | append("\n")
143 | append("parcelableValueNullEnabled: " + PreferenceKV.parcelableValueNullEnabled)
144 | }
145 | log(log = string)
146 | }
147 | btnCleanPreferenceGroup.setOnClickListener {
148 | PreferenceKV.clear()
149 | }
150 | btnClearLog.setOnClickListener {
151 | tvLog.text = ""
152 | }
153 | }
154 |
155 | private fun randomInt(): Int {
156 | return Random.nextInt(1, 500)
157 | }
158 |
159 | private fun randomBoolean(): Boolean {
160 | return Random.nextBoolean()
161 | }
162 |
163 | private fun randomWord(): String {
164 | return "leavesCZY" + "_" + randomInt()
165 | }
166 |
167 | private fun log(log: String) {
168 | Log.e("TAG", log)
169 | tvLog.append(log + "\n\n")
170 | }
171 |
172 | }
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/kvholder/src/main/java/github/leavesczy/kvholder/KVDelegate.kt:
--------------------------------------------------------------------------------
1 | package github.leavesczy.kvholder
2 |
3 | import android.os.Parcelable
4 | import com.google.gson.Gson
5 | import kotlin.properties.ReadWriteProperty
6 | import kotlin.reflect.KProperty
7 |
8 | /**
9 | * @Author: leavesCZY
10 | * @Date: 2023/12/5 14:20
11 | * @Desc:
12 | */
13 | abstract class KVDelegate(
14 | protected val kvHolder: IKVHolder,
15 | protected val defaultValue: T
16 | ) : ReadWriteProperty {
17 |
18 | override fun getValue(thisRef: IKVHolder, property: KProperty<*>): T {
19 | return getValue(key = buildKey(property = property), defaultValue = defaultValue)
20 | }
21 |
22 | override fun setValue(thisRef: IKVHolder, property: KProperty<*>, value: T) {
23 | setValue(key = buildKey(property = property), value = value)
24 | }
25 |
26 | abstract fun getValue(key: String, defaultValue: T): T
27 |
28 | abstract fun setValue(key: String, value: T)
29 |
30 | private fun buildKey(property: KProperty<*>): String {
31 | return property.name
32 | }
33 |
34 | }
35 |
36 | abstract class KVNullEnabledDelegate(kvHolder: IKVHolder) :
37 | KVDelegate(kvHolder = kvHolder, defaultValue = null)
38 |
39 | class IntDelegate(kvHolder: IKVHolder, defaultValue: Int) :
40 | KVDelegate(kvHolder = kvHolder, defaultValue = defaultValue) {
41 |
42 | override fun getValue(key: String, defaultValue: Int): Int {
43 | return kvHolder.get(key = key, defaultValue = defaultValue)
44 | }
45 |
46 | override fun setValue(key: String, value: Int) {
47 | kvHolder.set(key = key, value = value)
48 | }
49 |
50 | }
51 |
52 | class LongDelegate(kvHolder: IKVHolder, defaultValue: Long) :
53 | KVDelegate(kvHolder = kvHolder, defaultValue = defaultValue) {
54 |
55 | override fun getValue(key: String, defaultValue: Long): Long {
56 | return kvHolder.get(key = key, defaultValue = defaultValue)
57 | }
58 |
59 | override fun setValue(key: String, value: Long) {
60 | kvHolder.set(key = key, value = value)
61 | }
62 |
63 | }
64 |
65 | class FloatDelegate(kvHolder: IKVHolder, defaultValue: Float) :
66 | KVDelegate(kvHolder = kvHolder, defaultValue = defaultValue) {
67 |
68 | override fun getValue(key: String, defaultValue: Float): Float {
69 | return kvHolder.get(key = key, defaultValue = defaultValue)
70 | }
71 |
72 | override fun setValue(key: String, value: Float) {
73 | kvHolder.set(key = key, value = value)
74 | }
75 |
76 | }
77 |
78 | class DoubleDelegate(kvHolder: IKVHolder, defaultValue: Double) :
79 | KVDelegate(kvHolder = kvHolder, defaultValue = defaultValue) {
80 |
81 | override fun getValue(key: String, defaultValue: Double): Double {
82 | return kvHolder.get(key = key, defaultValue = defaultValue)
83 | }
84 |
85 | override fun setValue(key: String, value: Double) {
86 | kvHolder.set(key = key, value = value)
87 | }
88 |
89 | }
90 |
91 | class BooleanDelegate(kvHolder: IKVHolder, defaultValue: Boolean) :
92 | KVDelegate(kvHolder = kvHolder, defaultValue = defaultValue) {
93 |
94 | override fun getValue(key: String, defaultValue: Boolean): Boolean {
95 | return kvHolder.get(key = key, defaultValue = defaultValue)
96 | }
97 |
98 | override fun setValue(key: String, value: Boolean) {
99 | kvHolder.set(key = key, value = value)
100 | }
101 |
102 | }
103 |
104 | class StringDelegate(kvHolder: IKVHolder, defaultValue: String) :
105 | KVDelegate(kvHolder = kvHolder, defaultValue = defaultValue) {
106 |
107 | override fun getValue(key: String, defaultValue: String): String {
108 | return kvHolder.get(key = key, defaultValue = defaultValue)
109 | }
110 |
111 | override fun setValue(key: String, value: String) {
112 | kvHolder.set(key = key, value = value)
113 | }
114 |
115 | }
116 |
117 | class ParcelableDelegate(
118 | kvHolder: IKVHolder,
119 | private val clazz: Class,
120 | defaultValue: T
121 | ) : KVDelegate(kvHolder = kvHolder, defaultValue = defaultValue) {
122 |
123 | override fun getValue(key: String, defaultValue: T): T {
124 | return kvHolder.get(key = key, clazz = clazz) ?: defaultValue
125 | }
126 |
127 | override fun setValue(key: String, value: T) {
128 | kvHolder.set(key = key, value = value)
129 | }
130 |
131 | }
132 |
133 | class ParcelableNullEnabledDelegate(
134 | kvHolder: IKVHolder,
135 | private val clazz: Class
136 | ) : KVNullEnabledDelegate(kvHolder = kvHolder) {
137 |
138 | override fun getValue(key: String, defaultValue: T?): T? {
139 | return kvHolder.get(key = key, clazz = clazz)
140 | }
141 |
142 | override fun setValue(key: String, value: T?) {
143 | kvHolder.set(key = key, value = value)
144 | }
145 |
146 | }
147 |
148 | class JsonDelegate(
149 | kvHolder: IKVHolder,
150 | private val clazz: Class,
151 | defaultValue: T
152 | ) : KVDelegate(kvHolder = kvHolder, defaultValue = defaultValue) {
153 |
154 | override fun getValue(key: String, defaultValue: T): T {
155 | val json = kvHolder.get(key = key, defaultValue = "")
156 | if (json.isBlank()) {
157 | return defaultValue
158 | }
159 | return try {
160 | Gson().fromJson(json, clazz)
161 | } catch (e: Throwable) {
162 | null
163 | } ?: defaultValue
164 | }
165 |
166 | override fun setValue(key: String, value: T) {
167 | if (value == null) {
168 | kvHolder.removeKey(key)
169 | } else {
170 | val json = Gson().toJson(value)
171 | kvHolder.set(key = key, value = json)
172 | }
173 | }
174 |
175 | }
176 |
177 | class JsonNullEnabledDelegate(
178 | kvHolder: IKVHolder,
179 | private val clazz: Class
180 | ) : KVNullEnabledDelegate(kvHolder = kvHolder) {
181 |
182 | override fun getValue(key: String, defaultValue: T?): T? {
183 | val json = kvHolder.get(key = key, defaultValue = "")
184 | if (json.isBlank()) {
185 | return defaultValue
186 | }
187 | return try {
188 | Gson().fromJson(json, clazz)
189 | } catch (e: Throwable) {
190 | null
191 | } ?: defaultValue
192 | }
193 |
194 | override fun setValue(key: String, value: T?) {
195 | if (value == null) {
196 | kvHolder.removeKey(key)
197 | } else {
198 | val json = Gson().toJson(value)
199 | kvHolder.set(key = key, value = json)
200 | }
201 | }
202 |
203 | }
--------------------------------------------------------------------------------