-
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use internal modifier's coroutine scope instead of using runblocking #48
Merged
+25
−18
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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), | ||
|
@@ -121,24 +122,26 @@ private class CloudyModifierNode( | |
[email protected]() | ||
} | ||
|
||
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)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsolicited review:
coroutineScope { }
is redundant. You are not launching any other coroutines there.suspend
is enough.Maybe if you have launched each blur region in separate coroutine then it would have been justified. Like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing out of it! I was testing on the bitmap transformation and forgetting to remove the scope before merging it. Will fix it soon later 👍