Skip to content
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

Load articles immediately on swipe #379

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
Expand All @@ -21,6 +23,7 @@ import com.capyreader.app.ui.components.WebView
import com.capyreader.app.ui.components.rememberWebViewState
import com.jocmp.capy.Article
import com.jocmp.capy.articles.ArticleRenderer
import kotlinx.coroutines.launch
import my.nanihadesuka.compose.ColumnScrollbar
import my.nanihadesuka.compose.ScrollbarSettings
import org.koin.compose.koinInject
Expand All @@ -29,21 +32,11 @@ import org.koin.compose.koinInject
fun ArticleReader(
article: Article,
scrollState: ScrollState,
renderer: ArticleRenderer = koinInject(),
) {
val scope = rememberCoroutineScope()
val mediaViewer = LocalMediaViewer.current
val colors = articleTemplateColors()
var lastScrollY by rememberSaveable { mutableIntStateOf(0) }
val webViewState = rememberWebViewState()
val byline = article.byline(context = LocalContext.current)

fun render(): String {
return renderer.render(
article,
byline = byline,
colors = colors
)
}

Scrollbar(scrollState = scrollState) {
Column(
Expand All @@ -56,6 +49,11 @@ fun ArticleReader(
onNavigateToMedia = {
mediaViewer.open(it)
},
onPageStarted = {
scope.launch {
scrollState.scrollTo(0)
}
},
onDispose = {
lastScrollY = scrollState.value
},
Expand All @@ -64,8 +62,8 @@ fun ArticleReader(
}
}

LaunchedEffect(article.content) {
webViewState.loadHtml(render())
LaunchedEffect(article.id, article.content) {
webViewState.loadHtml(article)
}

LaunchedEffect(lastScrollY, scrollState.maxValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fun ArticleView(
onRequestArticle: (id: String) -> Unit
) {
val fullContent = LocalFullContent.current
val scrollState = rememberSaveable(article.id, key = article.id, saver = ScrollState.Saver) {
val scrollState = rememberSaveable(key = article.id, saver = ScrollState.Saver) {
ScrollState(initial = 0)
}

Expand Down Expand Up @@ -92,12 +92,10 @@ fun ArticleView(
onRequestPrevious = onRequestPrevious,
articles = articles,
) {
key(article.id) {
ArticleReader(
article = article,
scrollState = scrollState
)
}
ArticleReader(
article = article,
scrollState = scrollState
)
}
}

Expand Down
Loading
Loading