Skip to content

Instantly share code, notes, and snippets.

View SeongUgJung's full-sized avatar

Steve SeongUg Jung SeongUgJung

View GitHub Profile
@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 }
@Composable
fun ContentView() {
val age by viewModel.age.observeAsState() // viewModel.age: ObservableInt
// omitting...
}
@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()
android {
buildTypes {
getByName("debug") {
sourceSets {
getByName("main") {
java.srcDir(File("build/generated/ksp/debug/kotlin"))
}
}
}
getByName("release") {
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 {
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()
}
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
// 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 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) =
@SeongUgJung
SeongUgJung / RetrofitModule.kt
Last active February 20, 2021 05:10
RetrofitBuilder
fun retrofit(moshi: Moshi) = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
fun moshi() = Moshi.Builder()
.add(PersonAdapterFactory())
.add(PhoneAdapterFactory())
.build()