Skip to content

read Svg preview off the main Thread#21344

Merged
lukstbit merged 1 commit into
ankidroid:mainfrom
Phoenix1808:fix/svg_preview_IO
Jul 14, 2026
Merged

read Svg preview off the main Thread#21344
lukstbit merged 1 commit into
ankidroid:mainfrom
Phoenix1808:fix/svg_preview_IO

Conversation

@Phoenix1808

Copy link
Copy Markdown
Contributor

Purpose / Description

When previewing a selected SVG in the multimedia image editor, the
SVG file is read on the main thread (loadSvgFromUri -> openInputStream
-> convertToString) before it's passed to the WebView. For a large SVG
or a slow document provider, this read can briefly block the UI.

This moves the read to Dispatchers.IO and keeps the WebView update on
the main thread. Behaviour is unchanged.

Fixes #21276

How Has This Been Tested?

Manually previewed SVG and non-SVG images in the multimedia image
editor; the preview renders as before. :AnkiDroid:compilePlayDebugKotlin
builds cleanly.

I didn't add an automated test: this is a threading change with no
observable behaviour difference, and there isn't existing infrastructure
to assert main-thread usage here. Happy to add one if you'd prefer a
specific approach.

@sanjaysargam sanjaysargam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing @Phoenix1808 🚀

Timber.w("Failed to load SVG from URI")
showErrorInWebView()
val webView = this
lifecycleScope.launch {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use viewLifecycleOwner.lifecycleScope
This ensures coroutine is cancelled if view is destroyed, preventing potential crashes

showErrorInWebView()
val webView = this
lifecycleScope.launch {
val svgData = withContext(Dispatchers.IO) { loadSvgFromUri(imageUri) }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try/catch would be even more better

@Phoenix1808

Phoenix1808 commented Jul 12, 2026

Copy link
Copy Markdown
Contributor Author

Done, Please have a look! @sanjaysargam

@sanjaysargam sanjaysargam added Needs Second Approval Has one approval, one more approval to merge and removed Needs Review labels Jul 12, 2026

@lukstbit lukstbit left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, pending on the comment below. Thanks!

Comment thread AnkiDroid/src/main/java/com/ichi2/anki/multimedia/MultimediaImageFragment.kt Outdated
@lukstbit lukstbit added Needs Author Reply Waiting for a reply from the original author Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) squash-merge A squash & force push is required. The PR author may do this to speed up the merge process. and removed Needs Second Approval Has one approval, one more approval to merge labels Jul 13, 2026
@sanjaysargam

Copy link
Copy Markdown
Member

@Phoenix1808 Could you please squash commits into one

@lukstbit

Copy link
Copy Markdown
Member

Great!

@lukstbit lukstbit removed Needs Author Reply Waiting for a reply from the original author squash-merge A squash & force push is required. The PR author may do this to speed up the merge process. labels Jul 14, 2026
@lukstbit
lukstbit added this pull request to the merge queue Jul 14, 2026
Merged via the queue into ankidroid:main with commit 1d2b211 Jul 14, 2026
20 checks passed
@github-actions github-actions Bot removed the Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) label Jul 14, 2026
@github-actions github-actions Bot added this to the 2.25 release milestone Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential main-thread SVG stream read during multimedia image preview

3 participants