From aedd651cd01ed9458a6f2380c03f07c1da3ccad7 Mon Sep 17 00:00:00 2001 From: "tramline-github[bot]" <98346001+tramline-github[bot]@users.noreply.github.com> Date: Mon, 3 Jun 2024 11:04:52 +0530 Subject: [PATCH] Run reader view parsing in the background thread (#595) Co-authored-by: Sasikanth Miriyampalli --- .../dev/sasikanth/rss/reader/app/App.kt | 8 +++- .../rss/reader/reader/ui/ReaderHTML.kt | 5 +-- .../rss/reader/reader/ui/ReaderScreen.kt | 43 ++++++++++++------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/app/App.kt b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/app/App.kt index ad2483415..4e59cd126 100644 --- a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/app/App.kt +++ b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/app/App.kt @@ -49,6 +49,7 @@ import dev.sasikanth.rss.reader.search.ui.SearchScreen import dev.sasikanth.rss.reader.settings.ui.SettingsScreen import dev.sasikanth.rss.reader.share.LocalShareHandler import dev.sasikanth.rss.reader.share.ShareHandler +import dev.sasikanth.rss.reader.util.DispatchersProvider import dev.sasikanth.rss.reader.utils.LocalWindowSizeClass import me.tatarka.inject.annotations.Inject @@ -62,6 +63,7 @@ fun App( shareHandler: ShareHandler, linkHandler: LinkHandler, imageLoader: ImageLoader, + dispatchersProvider: DispatchersProvider, ) { setSingletonImageLoaderFactory { imageLoader } @@ -109,7 +111,11 @@ fun App( AboutScreen(aboutPresenter = screen.presenter, modifier = fillMaxSizeModifier) } is Screen.Reader -> { - ReaderScreen(presenter = screen.presenter, modifier = fillMaxSizeModifier) + ReaderScreen( + presenter = screen.presenter, + dispatchersProvider = dispatchersProvider, + modifier = fillMaxSizeModifier + ) } is Screen.AddFeed -> { AddFeedScreen(presenter = screen.presenter, modifier = fillMaxSizeModifier) diff --git a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/reader/ui/ReaderHTML.kt b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/reader/ui/ReaderHTML.kt index f8d646a6a..a45494c12 100644 --- a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/reader/ui/ReaderHTML.kt +++ b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/reader/ui/ReaderHTML.kt @@ -16,9 +16,6 @@ package dev.sasikanth.rss.reader.reader.ui -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.IO -import kotlinx.coroutines.withContext import twine.shared.generated.resources.Res object ReaderHTML { @@ -89,6 +86,6 @@ object ReaderHTML { } private suspend fun readFile(fileName: String): String { - return withContext(Dispatchers.IO) { Res.readBytes("files/reader/$fileName").decodeToString() } + return Res.readBytes("files/reader/$fileName").decodeToString() } } diff --git a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/reader/ui/ReaderScreen.kt b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/reader/ui/ReaderScreen.kt index 63e8ee61b..8ca78bf13 100644 --- a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/reader/ui/ReaderScreen.kt +++ b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/reader/ui/ReaderScreen.kt @@ -68,11 +68,17 @@ import dev.sasikanth.rss.reader.resources.icons.TwineIcons import dev.sasikanth.rss.reader.resources.icons.Website import dev.sasikanth.rss.reader.share.LocalShareHandler import dev.sasikanth.rss.reader.ui.AppTheme +import dev.sasikanth.rss.reader.util.DispatchersProvider import dev.sasikanth.rss.reader.utils.asJSString import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @Composable -internal fun ReaderScreen(presenter: ReaderPresenter, modifier: Modifier = Modifier) { +internal fun ReaderScreen( + presenter: ReaderPresenter, + dispatchersProvider: DispatchersProvider, + modifier: Modifier = Modifier +) { val state by presenter.state.collectAsState() val coroutineScope = rememberCoroutineScope() val linkHandler = LocalLinkHandler.current @@ -229,25 +235,30 @@ internal fun ReaderScreen(presenter: ReaderPresenter, modifier: Modifier = Modif } LaunchedEffect(state.content) { - val html = - ReaderHTML.create( - title = state.title!!, - feedName = state.feed!!.name, - feedHomePageLink = state.feed!!.homepageLink, - publishedAt = state.publishedAt!!, - ) + withContext(dispatchersProvider.io) { + val htmlTemplate = + ReaderHTML.create( + title = state.title!!, + feedName = state.feed!!.name, + feedHomePageLink = state.feed!!.homepageLink, + publishedAt = state.publishedAt!!, + ) - navigator.loadHtml(html, state.link) + navigator.loadHtml(htmlTemplate, state.link) + } } LaunchedEffect(webViewState.loadingState) { - if ( - webViewState.loadingState == LoadingState.Finished && !state.content.isNullOrBlank() - ) { - navigator.evaluateJavaScript( - script = - "renderReaderView(${state.link.asJSString}, ${state.content.asJSString}, ${colors.asJSString})" - ) + withContext(dispatchersProvider.io) { + val hasHtmlTemplateLoaded = + webViewState.loadingState == LoadingState.Finished && !state.content.isNullOrBlank() + + if (hasHtmlTemplateLoaded) { + navigator.evaluateJavaScript( + script = + "renderReaderView(${state.link.asJSString}, ${state.content.asJSString}, ${colors.asJSString})" + ) + } } }