Buff
Add status to beans in Jetpack(jb) Compose, Fields in beans can be directly used as the MutableState<T>
ability
- Convert some fields in beans to MutableState<T> that can be used directly
Capability to be achieved
How to use
Step 1.Root dir, build.gradle.kts add:
buildscript {
repositories {
maven("https://jitpack.io")//this
...
}
}
allprojects {
repositories {
maven("https://jitpack.io")//this
...
}
}
Step 2.Your app dir, build.gradle.kts add:
plugins {
...
id("com.google.devtools.ksp") version "1.7.10-1.0.6"//this, The left 1.7.10 corresponds to your the Kotlin version,more version: https://github.com/google/ksp/releases
}
dependencies {
...
implementation("com.github.ltttttttttttt:Buff:$version")//this, such as 0.0.3
ksp("com.github.ltttttttttttt:Buff:$version")//this, such as 0.0.3
}
Step 3.Use BUff
Add the @Buff to your bean, call the addBuff() transform to the new Any, The attribute (such as name) not in the constructor will be automatically converted to MutableState<T>
@Buff
class BuffBean(
val id: Int? = null,
) {
var name: String? = null
}
Example(reference UseBuff.kt):
val buffBean = BuffBean(0)
val bean = buffBean.addBuff()//Transform to the BuffBeanWithBuff
bean.name//The name's getter and setter have the effect of MutableState<T>
bean.removeBuff()//Fallback to BuffBean(optional)
Step 4.Add ksp dir to the srcDir
Your app dir, build.gradle.kts add:
//If your project is the android, and the productFlavors is not set
android {
buildTypes {
release {
kotlin {
sourceSets.main {
kotlin.srcDir("build/generated/ksp/release/kotlin")
}
}
}
debug {
kotlin {
sourceSets.main {
kotlin.srcDir("build/generated/ksp/debug/kotlin")
}
}
}
}
kotlin {
sourceSets.test {
kotlin.srcDir("build/generated/ksp/test/kotlin")
}
}
}
//If your project is the android, and the productFlavors is set
applicationVariants.all {
outputs.all {
val flavorAndBuildTypeName = name
kotlin {
sourceSets.main {
kotlin.srcDir(
"build/generated/ksp/${
flavorAndBuildTypeName.split("-").let {
it.first() + it.last()[0].toUpperCase() + it.last().substring(1)
}
}/kotlin"
)
}
}
}
}
kotlin {
sourceSets.test {
kotlin.srcDir("build/generated/ksp/test/kotlin")
}
}
//If your project is the jvm or more
kotlin {
sourceSets.main {
kotlin.srcDir("build/generated/ksp/main/kotlin")
}
sourceSets.test {
kotlin.srcDir("build/generated/ksp/test/kotlin")
}
}
Step 5.Config
Serialize of this project uses kotlinx-serialization by default, To modify, Your app dir, build.gradle.kts add:
ksp {
//Set the Annotation of the class, Usually as follows
arg("classSerializeAnnotationWithBuff", "//Not have")
//Set the Annotation of the field to transient, Usually as follows
arg("fieldSerializeTransientAnnotationWithBuff", "@kotlin.jvm.Transient")
}
Add custom code, reference [KspOptions.handlerCustomCode], Your app dir, build.gradle.kts add:
ksp {
arg("customInClassWithBuff", "//Class end")//in class
arg("customInFileWithBuff", "//File end")//in file
}