This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable fun LifecycleObservable.observeLifecycle() { | |
val lifecycleOwner = LocalLifecycleOwner.current | |
LaunchedEffect(lifecycleOwner) { | |
var createdJob: Job? = null | |
var startedJob: Job? = null | |
var resumedJob: Job? = null | |
lifecycleOwner.addObserver { | |
fun onCreated() = createdToDestroy().launchIn(this@LaunchedEffect).let { createdJob = it } | |
fun onStarted() = startedToStop().launchIn(this@LaunchedEffect).let { startedJob = it } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun ContentView() { | |
val age by viewModel.age.observeAsState() // viewModel.age: ObservableInt | |
// omitting... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun ObservableBoolean.observeAsState(): State<Boolean> { | |
val value = get() | |
val state = remember { mutableStateOf(value) } | |
DisposableEffect(state) { | |
val callback: Observable.OnPropertyChangedCallback = object : Observable.OnPropertyChangedCallback() { | |
override fun onPropertyChanged(sender: Observable?, propertyId: Int) { | |
if (sender == null && sender !is ObservableBoolean) return | |
state.value = (sender as ObservableBoolean).get() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
android { | |
buildTypes { | |
getByName("debug") { | |
sourceSets { | |
getByName("main") { | |
java.srcDir(File("build/generated/ksp/debug/kotlin")) | |
} | |
} | |
} | |
getByName("release") { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) { | |
classDeclaration.annotations.firstOrNull { annotation -> | |
// filter only JsonClass | |
annotation.shortName.asString() == "JsonClass" | |
}?.arguments?.firstOrNull { argument -> | |
// check @JsonClass(generateAdapter = true) | |
argument.name?.asString() == "generateAdapter" && argument.value == true | |
} | |
?.let { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val targets = mutableListOf<KSClassDeclaration>() | |
override fun process(resolver: Resolver): List<KSAnnotated> { | |
// read @JsonClass annoted class | |
val symbols = resolver.getSymbolsWithAnnotation("com.squareup.moshi.JsonClass") | |
symbols.filter { ksAnnotated -> | |
// only filter out class. | |
ksAnnotated is KSClassDeclaration && ksAnnotated.validate() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private lateinit var codeGenerator: CodeGenerator | |
private lateinit var logger: KSPLogger | |
override fun init( | |
options: Map<String, String>, | |
kotlinVersion: KotlinVersion, | |
codeGenerator: CodeGenerator, | |
logger: KSPLogger | |
) { | |
// set all these to member field |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// root build.gradle.kts | |
buildscript { | |
repositories { | |
google() | |
} | |
dependencies { | |
classpath("com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:1.4.30-1.0.0-alpha02") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this will generate PersonAdapter.kt | |
@JsonClass(generateAdapter = true) | |
data class Person( | |
val name: String, | |
val age: Int, | |
val phone: Phone | |
) | |
class PersonAdapterFactory : JsonAdapter.Factory { | |
overridee fun create(type: Type, moshi: Moshi) = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun retrofit(moshi: Moshi) = Retrofit.Builder() | |
.addConverterFactory(MoshiConverterFactory.create(moshi)) | |
.build() | |
fun moshi() = Moshi.Builder() | |
.add(PersonAdapterFactory()) | |
.add(PhoneAdapterFactory()) | |
.build() |
NewerOlder