Skip to content

Commit

Permalink
Update to Kotlin 2.1.0 (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes authored Dec 23, 2024
1 parent aab6063 commit cb9db3d
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ android.defaults.buildfeatures.shaders=false
android.defaults.buildFeatures.buildConfig=false

kotlin.mpp.androidSourceSetLayoutVersion=2
kotlin.mpp.androidGradlePluginCompatibility.nowarn=true

org.jetbrains.compose.experimental.uikit.enabled=true
org.jetbrains.compose.experimental.jscanvas.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ class MetalavaConventionPlugin : Plugin<Project> {

metalava {
filename.set("api/api.txt")
sourcePaths.setFrom(
target.kotlin.sourceSets
.filterNot { it.name.contains("test", ignoreCase = true) }
.flatMap { it.kotlin.sourceDirectories },

excludedSourceSets.setFrom(
target.kotlin
.sourceSets
.filter { it.name.contains("test", ignoreCase = true) }
.flatMap { it.kotlin.sourceDirectories }
)
}

Expand Down
15 changes: 8 additions & 7 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ androidx-benchmark = "1.3.3"
androidx-media3 = "1.4.1"
androidx-test-ext-junit = "1.2.1"
assertk = "0.28.1"
cache4k = "0.13.0"
coil = "3.0.4"
compose-multiplatform = "1.7.1"
kotlinx-coroutines-swing = "1.10.1"
kotlinx-datetime = "0.6.1"
ktlint = "1.0.1"
kotlin = "2.0.21"
ktor = "3.0.2"
kotlin = "2.1.0"
ktor = "3.0.3"
jetpack-compose = "1.7.6"
metalava = "0.3.5"
metalava = "0.4.0-alpha03"
robolectric = "4.14.1"
roborazzi = "1.38.0"
spotless = "6.25.0"
Expand All @@ -28,8 +28,8 @@ compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "
compose-multiplatform = { id = "org.jetbrains.compose", version.ref = "compose-multiplatform" }
dokka = { id = "org.jetbrains.dokka", version = "2.0.0" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
metalava = { id = "me.tylerbwong.gradle.metalava", version = "0.3.5" }
poko = { id = "dev.drewhamilton.poko", version = "0.17.2" }
metalava = { id = "me.tylerbwong.gradle.metalava", version.ref = "metalava" }
poko = { id = "dev.drewhamilton.poko", version = "0.18.1" }
roborazzi = { id = "io.github.takahirom.roborazzi", version.ref = "roborazzi" }
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
mavenpublish = { id = "com.vanniktech.maven.publish", version = "0.30.0" }
Expand All @@ -50,11 +50,12 @@ androidx-compose-foundation = { module = "androidx.compose.foundation:foundation

assertk = { module = "com.willowtreeapps.assertk:assertk", version.ref = "assertk" }

cache4k = { module = "io.github.reactivecircus.cache4k:cache4k", version.ref = "cache4k" }
coil-compose = { module = "io.coil-kt.coil3:coil-compose", version.ref = "coil" }
coil-ktor = { module = "io.coil-kt.coil3:coil-network-ktor3", version.ref = "coil" }

kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines-swing" }
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" }

ktor-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }

Expand Down
2 changes: 1 addition & 1 deletion haze/api/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ package dev.chrisbanes.haze {
method public float component3-D9Ej5fM();
method public float component4();
method public dev.chrisbanes.haze.HazeTint component5();
method public dev.chrisbanes.haze.HazeStyle copy-cq6XJ1M(long backgroundColor, java.util.List<dev.chrisbanes.haze.HazeTint> tints, float blurRadius, float noiseFactor, dev.chrisbanes.haze.HazeTint fallbackTint);
method public dev.chrisbanes.haze.HazeStyle copy-cq6XJ1M(long backgroundColor, java.util.List<dev.chrisbanes.haze.HazeTint!> tints, float blurRadius, float noiseFactor, dev.chrisbanes.haze.HazeTint fallbackTint);
method public long getBackgroundColor();
method public float getBlurRadius();
method public dev.chrisbanes.haze.HazeTint getFallbackTint();
Expand Down
5 changes: 4 additions & 1 deletion haze/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ kotlin {
dependencies {
api(compose.ui)
implementation(compose.foundation)
implementation(libs.cache4k)
}
}

Expand Down Expand Up @@ -68,6 +67,10 @@ kotlin {

named("wasmJsMain") {
dependsOn(skikoMain)

dependencies {
implementation(libs.kotlinx.datetime)
}
}

named("jsMain") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import androidx.compose.ui.node.currentValueOf
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import io.github.reactivecircus.cache4k.Cache
import kotlin.concurrent.getOrSet
import kotlin.math.roundToInt

Expand Down Expand Up @@ -84,23 +83,19 @@ internal actual fun DrawScope.canUseGraphicLayers(): Boolean {
return Build.VERSION.SDK_INT >= 31 && drawContext.canvas.nativeCanvas.isHardwareAccelerated
}

private val noiseTextureCache by unsynchronizedLazy {
Cache.Builder<Int, Bitmap>()
.maximumCacheSize(3)
.build()
}
private val noiseTextureCache by unsynchronizedLazy { SimpleLruCache<Int, Bitmap>(3) }

private fun Context.getNoiseTexture(noiseFactor: Float): Bitmap {
val noiseAlphaInt = (noiseFactor * 255).roundToInt().coerceIn(0, 255)
val cached = noiseTextureCache.get(noiseAlphaInt)
val cached = noiseTextureCache[noiseAlphaInt]
if (cached != null && !cached.isRecycled) {
return cached
}

// We draw the noise with the given opacity
return BitmapFactory.decodeResource(resources, R.drawable.haze_noise)
.transform(alpha = noiseAlphaInt)
.also { noiseTextureCache.put(noiseAlphaInt, it) }
.also { noiseTextureCache[noiseAlphaInt] = it }
}

@RequiresApi(31)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2024, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

internal actual fun epochTimeMillis(): Long = System.currentTimeMillis()
9 changes: 9 additions & 0 deletions haze/src/appleMain/kotlin/dev/chrisbanes/haze/Time.apple.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2024, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

import platform.Foundation.NSDate
import platform.Foundation.timeIntervalSince1970

internal actual fun epochTimeMillis(): Long = (NSDate().timeIntervalSince1970 * 1000).toLong()
11 changes: 3 additions & 8 deletions haze/src/commonMain/kotlin/dev/chrisbanes/haze/HazeEffectNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import androidx.compose.ui.unit.roundToIntSize
import androidx.compose.ui.unit.takeOrElse
import androidx.compose.ui.unit.toSize
import dev.drewhamilton.poko.Poko
import io.github.reactivecircus.cache4k.Cache

internal val ModifierLocalCurrentHazeZIndex = modifierLocalOf<Float?> { null }

Expand Down Expand Up @@ -599,11 +598,7 @@ sealed interface HazeProgressive {
}
}

private val renderEffectCache by unsynchronizedLazy {
Cache.Builder<RenderEffectParams, RenderEffect>()
.maximumCacheSize(10)
.build()
}
private val renderEffectCache by unsynchronizedLazy { SimpleLruCache<RenderEffectParams, RenderEffect>(10) }

@Poko
internal class RenderEffectParams(
Expand Down Expand Up @@ -663,15 +658,15 @@ internal fun HazeEffectNode.getOrCreateRenderEffect(

internal fun CompositionLocalConsumerModifierNode.getOrCreateRenderEffect(params: RenderEffectParams): RenderEffect? {
log(HazeEffectNode.TAG) { "getOrCreateRenderEffect: $params" }
val cached = renderEffectCache.get(params)
val cached = renderEffectCache[params]
if (cached != null) {
log(HazeEffectNode.TAG) { "getOrCreateRenderEffect. Returning cached: $params" }
return cached
}

log(HazeEffectNode.TAG) { "getOrCreateRenderEffect. Creating: $params" }
return createRenderEffect(params)
?.also { renderEffectCache.put(params, it) }
?.also { renderEffectCache[params] = it }
}

internal expect fun CompositionLocalConsumerModifierNode.createRenderEffect(params: RenderEffectParams): RenderEffect?
Expand Down
35 changes: 35 additions & 0 deletions haze/src/commonMain/kotlin/dev/chrisbanes/haze/SimpleLruCache.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2024, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

/**
* An extremely simple LruCache that wraps a mutable map underneath. It is not thread-safe.
*/
internal class SimpleLruCache<K, V>(private val limit: Int) {

private val map = mutableMapOf<K, CacheEntry<V>>()

operator fun get(key: K): V? = map[key]?.also { it.updateAccessTime() }?.value

operator fun set(key: K, value: V) {
map[key] = CacheEntry(value)

// Now remove the oldest items until we're below our limit again
while (map.size > limit) {
map.minByOrNull { it.value.lastAccessTime }?.also { map.remove(it.key) }
}
}

fun clear() {
map.clear()
}
}

private class CacheEntry<V>(val value: V) {
var lastAccessTime: Long = epochTimeMillis()

fun updateAccessTime() {
lastAccessTime = epochTimeMillis()
}
}
6 changes: 6 additions & 0 deletions haze/src/commonMain/kotlin/dev/chrisbanes/haze/Time.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2024, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

internal expect fun epochTimeMillis(): Long
2 changes: 2 additions & 0 deletions haze/src/commonMain/kotlin/dev/chrisbanes/haze/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2024, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

@file:Suppress("NOTHING_TO_INLINE")

package dev.chrisbanes.haze

import androidx.compose.ui.geometry.Offset
Expand Down
8 changes: 8 additions & 0 deletions haze/src/jsMain/kotlin/dev/chrisbanes/haze/Time.js.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2024, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

import kotlin.js.Date

internal actual fun epochTimeMillis(): Long = Date.now().toLong()
6 changes: 6 additions & 0 deletions haze/src/jvmMain/kotlin/dev/chrisbanes/haze/Time.jvm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2024, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

internal actual fun epochTimeMillis(): Long = System.currentTimeMillis()
8 changes: 8 additions & 0 deletions haze/src/wasmJsMain/kotlin/dev/chrisbanes/haze/Time.wasmJs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2024, Christopher Banes and the Haze project contributors
// SPDX-License-Identifier: Apache-2.0

package dev.chrisbanes.haze

import kotlinx.datetime.Clock

internal actual fun epochTimeMillis(): Long = Clock.System.now().toEpochMilliseconds()
Loading

0 comments on commit cb9db3d

Please sign in to comment.