Jetpack Compose Smart Recomposition

To have smart recomposition scopes play a pivotal role. You can check Vinay Gaba’s What is “donut-hole skipping” in Jetpack Compose? article. Leland Richardson explains in this tweet as The part that is “donut hole skipping” is the fact that a new lambda being passed into a composable (ie Button) can recompose without recompiling the … Read more

JaCoCo returning 0% Coverage with Kotlin and Android 3.0

You can get line-by-line coverage for both Java and Kotlin code by defining the two different directories for generated .class files: def debugTree = fileTree(dir: “${buildDir}/intermediates/classes/debug”, excludes: fileFilter) def kotlinDebugTree = fileTree(dir: “${buildDir}/tmp/kotlin-classes/debug”, excludes: fileFilter) Then, simply include both fileTrees in your classDirectories: classDirectories.from = files([debugTree], [kotlinDebugTree])

How to save enum field in the database room?

You can make a convert to each enum, like this: class Converters { @TypeConverter fun toHealth(value: String) = enumValueOf<Health>(value) @TypeConverter fun fromHealth(value: Health) = value.name } Or if you prefer store it as SQL integer, you can use ordinal too: class Converters { @TypeConverter fun toHealth(value: Int) = enumValues<Health>()[value] @TypeConverter fun fromHealth(value: Health) = value.ordinal … Read more

How to implement timer with Kotlin coroutines

Edit: note that the API suggested in the original answer is now marked @ObsoleteCoroutineApi: Ticker channels are not currently integrated with structured concurrency and their api will change in the future. You can now use the Flow API to create your own ticker flow: import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds import kotlinx.coroutines.* import kotlinx.coroutines.flow.* fun tickerFlow(period: Duration, … Read more

Kotlin Android debounce

I’ve created a gist with three debounce operators inspired by this elegant solution from Patrick where I added two more similar cases: throttleFirst and throttleLatest. Both of these are very similar to their RxJava analogues (throttleFirst, throttleLatest). throttleLatest works similar to debounce but it operates on time intervals and returns the latest data for each … Read more

Kotlin ‘when’ statement vs Java ‘switch’

Simple but wordy solution is: if (oldVersion <= 1) upgradeFromV1() if (oldVersion <= 2) upgradeFromV2() if (oldVersion <= 3) upgradeFromV3() Another possible solution with function references: fun upgradeFromV0() {} fun upgradeFromV1() {} fun upgradeFromV2() {} fun upgradeFromV3() {} val upgrades = arrayOf(::upgradeFromV0, ::upgradeFromV1, ::upgradeFromV2, ::upgradeFromV3) fun upgradeFrom(oldVersion: Int) { for (i in oldVersion..upgrades.lastIndex) { upgrades[i]() … Read more

Using return inside a lambda?

Just use the qualified return syntax: return@fetchUpcomingTrips. In Kotlin, return inside a lambda means return from the innermost nesting fun (ignoring lambdas), and it is not allowed in lambdas that are not inlined. The return@label syntax is used to specify the scope to return from. You can use the name of the function the lambda … Read more