Skip to content

Commit

Permalink
Merge pull request #48 from skydoves/improve/modifier-coroutinescope
Browse files Browse the repository at this point in the history
Use internal modifier's coroutine scope instead of using runblocking
  • Loading branch information
skydoves authored Dec 30, 2024
2 parents 5fd27ee + e2200d7 commit 20b7d88
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions cloudy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ kotlin {
dependencies {
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.runtime)
implementation(libs.kotlinx.coroutines.android)
}
21 changes: 12 additions & 9 deletions cloudy/src/main/kotlin/com/skydoves/cloudy/CloudyModifierNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.graphics.layer.GraphicsLayer
import androidx.compose.ui.graphics.layer.drawLayer
import androidx.compose.ui.graphics.rememberGraphicsLayer
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasureResult
Expand All @@ -38,7 +39,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.offset
import com.skydoves.cloudy.internals.render.iterativeBlur
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.launch

/**
* `Modifier.cloudy()` is a replacement of the [blur] modifier (compatible with under Android 12),
Expand Down Expand Up @@ -121,24 +122,26 @@ private class CloudyModifierNode(
this@draw.drawContent()
}

drawLayer(graphicsLayer)

onStateChanged.invoke(CloudyState.Loading)

try {
val blurredBitmap: Bitmap = runBlocking(Dispatchers.IO) {
coroutineScope.launch(Dispatchers.Main.immediate) {
try {
val targetBitmap: Bitmap = graphicsLayer.toImageBitmap().asAndroidBitmap()
.copy(Bitmap.Config.ARGB_8888, true)

iterativeBlur(
val blurredBitmap = iterativeBlur(
androidBitmap = targetBitmap,
radius = radius
)?.apply {
drawImage(this.asImageBitmap())
}
} ?: throw RuntimeException("Couldn't capture a bitmap from the composable tree")
} ?: throw RuntimeException("Couldn't capture a bitmap from the composable tree")

onStateChanged.invoke(CloudyState.Success(blurredBitmap))
} catch (e: Exception) {
onStateChanged.invoke(CloudyState.Error(e))
onStateChanged.invoke(CloudyState.Success(blurredBitmap))
} catch (e: Exception) {
onStateChanged.invoke(CloudyState.Error(e))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.skydoves.cloudy.internals.render

import android.graphics.Bitmap
import kotlinx.coroutines.coroutineScope

// This string is used for error messages.
private const val externalName = "RenderScript Toolkit"
Expand Down Expand Up @@ -315,10 +316,10 @@ internal fun vectorSize(bitmap: Bitmap): Int {
}
}

internal fun iterativeBlur(
internal suspend fun iterativeBlur(
androidBitmap: Bitmap,
radius: Int
): Bitmap? {
): Bitmap? = coroutineScope {
val iterate = (radius + 1) / 25
var bitmap: Bitmap? = RenderScriptToolkit.blur(
inputBitmap = androidBitmap,
Expand All @@ -332,5 +333,5 @@ internal fun iterativeBlur(
)
}

return bitmap
bitmap
}
14 changes: 8 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
[versions]
agp = "8.7.2"
kotlin = "2.0.21"
dokka = "1.9.20"
agp = "8.7.3"
kotlin = "2.1.0"
dokka = "2.0.0"
javaRelease = "11"
jvmTarget = "11"
kotlinxCoroutinesAndroid = "1.9.0"
material = "1.12.0"
nexusPlugin = "0.30.0"
binaryCompatibility = "0.16.3"
binaryCompatibility = "0.17.0"
androidxActivity = "1.9.3"
androidxCompose = "1.7.5"
androidxCompose = "1.8.0-alpha07"
androidxComposeConstraintLayout = "1.1.0"
androidxCore = "1.15.0"
androidxTest = "1.6.2"
androidxJunit = "1.2.1"
androidxMacroBenchmark = "1.3.3"
androidxProfileinstaller = "1.4.1"
androidxUiAutomator = "2.3.0"
landscapist = "2.4.2"
landscapist = "2.4.4"
spotless = "6.7.0"

[plugins]
Expand All @@ -30,6 +31,7 @@ spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
kotlinBinaryCompatibilityValidator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binaryCompatibility" }

[libraries]
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinxCoroutinesAndroid" }
material = { module = "com.google.android.material:material", version.ref = "material" }
androidx-compose-ui = { group = "androidx.compose.ui", name = "ui", version.ref = "androidxCompose" }
androidx-compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling", version.ref = "androidxCompose" }
Expand Down

0 comments on commit 20b7d88

Please sign in to comment.