Skip to content

Commit

Permalink
feat: support expiration and encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
chachako committed Aug 4, 2024
1 parent ec19d3c commit c4bc1fb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
11 changes: 6 additions & 5 deletions compiler/src/main/kotlin/codegen/CodegenStep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ abstract class CodegenStep {
else -> value.takeIf { it.isNotEmpty() }
}

fun KSAnnotation?.findIntArgument(name: String) =
when (val value = findArgument(name)?.value as? Int) {
null -> null
else -> value
}

fun FileSpec.write(originatingDeclarations: Iterable<KSDeclaration>) =
write(originatingDeclarations.mapNotNull { it.containingFile }.toSet())

Expand Down Expand Up @@ -188,7 +194,6 @@ abstract class CodegenStep {
) {
val factoryClassName = className("PreferencesFactory")
val factoryImplClassName = className("PreferencesFactoryImpl")
val objectTypeConverters = typeConverters.filter { it.classKind == ClassKind.OBJECT }
val classTypeConverters = typeConverters.filter { it.classKind != ClassKind.OBJECT }

val classTypeConvertersParams = classTypeConverters.map {
Expand All @@ -198,10 +203,6 @@ abstract class CodegenStep {
)
}

// If there are non-singleton TypeConverters, the user needs to manually inject
// them.
val needInjectTypeConverters = objectTypeConverters.isNotEmpty()

fun mutableClassName(raw: KSClassDeclaration) =
className("Mutable" + raw.simpleName.asString())

Expand Down
17 changes: 16 additions & 1 deletion compiler/src/main/kotlin/codegen/PreferencesImplClasses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com.squareup.kotlinpoet.INT
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.LONG
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.MemberName.Companion.member
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.SET
Expand All @@ -70,10 +71,24 @@ class PreferencesImplClasses : CodegenStep() {
val mutableImplClassName = context.mutableImplClassName(preferences)
val annotation = requireNotNull(preferences.findAnnotation(Preferences))
val id = annotation.findStringArgument("id") ?: preferences.simpleName.asString()
val expires = annotation.findIntArgument("expires") ?: -1
val cryptKey = annotation.findStringArgument("cryptKey") ?: ""

val mmkvPropertySpec = PropertySpec.builder("mmkv", MMKV)
.addModifiers(KModifier.OVERRIDE)
.initializer("%T.mmkvWithID(%S)", MMKV, id)
.initializer(buildCodeBlock {
add("%T.mmkvWithID(%S", MMKV, id)
if (cryptKey.isNotEmpty()) {
add(", %M, %S)", MMKV.member("SINGLE_PROCESS_MODE"), cryptKey)
} else {
add(")")
}
if (expires > -1) {
beginControlFlow(".apply·{")
addStatement("enableAutoKeyExpire(%L)", expires)
endControlFlow()
}
})
.build()

val defaultPropertySpec = PropertySpec.builder("default", dataClassName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.meowool.mmkv.ktx.PersistDefaultValue
import com.meowool.mmkv.ktx.Preferences
import java.util.Date

@Preferences
@Preferences(expires = 100, cryptKey = "abc")
data class CustomData(
@PersistDefaultValue
val date: Date = Date(),
Expand Down
14 changes: 14 additions & 0 deletions runtime/src/main/kotlin/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,18 @@ annotation class Preferences(
* For example: `GeneralSettings` -> `generalSettings`.
*/
val name: String = "",

/**
* The expiration time (in milliseconds) for the preferences.
* If set to a value greater than -1, the preferences will expire after the specified time.
* If set to -1 (the default value), the preferences will never expire.
*/
val expires: Int = -1,

/**
* The encryption key to use for the preferences.
* If set to a non-empty string, the preferences will be encrypted using the specified key.
* If left empty (the default value), the preferences will not be encrypted.
*/
val cryptKey: String = "",
)

0 comments on commit c4bc1fb

Please sign in to comment.