├── 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 |