Android Kotlin Coroutines: what is the difference between flow, callbackFlow, channelFlow,… other flow constructors

For callbackFlow: You cannot use emit() as the simple Flow (because it’s a suspend function) inside a callback. Therefore the callbackFlow offers you a synchronized way to do it with the trySend() option. Example: fun observeData() = flow { myAwesomeInterface.addListener{ result -> emit(result) // NOT ALLOWED } } So, coroutines offer you the option of … Read more

How do you make a text clickable in jetpack compose ? I would also like to toggle it to non clickable after selecting once

You can add the clickable modifier to your Text or use ClickableText instead of Text. Here is an example of how to do it with ClickableText: var enabled by remember { mutableStateOf(true)} ClickableText( text = AnnotatedString(text) , onClick = { if (enabled) { enabled = false text = “Disabled” } })

assign variable only if it is null

The shortest way I can think of is indeed using the elvis operator: value = value ?: newValue If you do this often, an alternative is to use a delegated property, which only stores the value if its null: class Once<T> { private var value: T? = null operator fun getValue(thisRef: Any?, property: KProperty<*>): T? … Read more

Is there a way to reference the Java class for a Kotlin top-level function?

Another way I found is to declare a local class or an anonymous object inside a top level function and to get its enclosingClass: val topLevelClass = object{}.javaClass.enclosingClass Note: to work, this declaration should be placed on top level or inside a top-level function. Then you can use the topLevelClass as a Class<out Any>: fun … Read more

What’s the use of Moshi’s Kotlin codegen?

This is sort of three questions Why is code gen useful Code gen is useful as a compile-time alternative to the reflective moshi-kotlin. Both of them are useful because they natively understand Kotlin code and its language features. Without them, Moshi would not be able to understand Kotlin nullability, default values, and much more. There … Read more

techhipbettruvabetnorabahisbahis forumuedusedueduedusedusedueduseduseduedus