Skip to content

Commit

Permalink
Run reader view parsing in the background thread (#595)
Browse files Browse the repository at this point in the history
Co-authored-by: Sasikanth Miriyampalli <[email protected]>
  • Loading branch information
tramline-github[bot] and msasikanth authored Jun 3, 2024
1 parent ca81eb3 commit aedd651
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -62,6 +63,7 @@ fun App(
shareHandler: ShareHandler,
linkHandler: LinkHandler,
imageLoader: ImageLoader,
dispatchersProvider: DispatchersProvider,
) {
setSingletonImageLoaderFactory { imageLoader }

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})"
)
}
}
}

Expand Down

0 comments on commit aedd651

Please sign in to comment.