Skip to content

Commit

Permalink
feat(ui): scroll to the currently reading article
Browse files Browse the repository at this point in the history
  • Loading branch information
JunkFood02 committed Sep 26, 2024
1 parent aca2028 commit fea521d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.ash.reader.ui.component.webview

import android.util.Log
import android.view.View
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/me/ash/reader/ui/page/common/HomeEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,8 @@ fun HomeEntry(

}
animatedComposable(route = "${RouteName.READING}/{articleId}") {
val articleId = it.arguments?.getString("articleId")
ReadingPage(
navController = navController,
articleId = articleId,
homeViewModel = homeViewModel
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffold
import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffoldRole
import androidx.compose.material3.adaptive.navigation.rememberListDetailPaneScaffoldNavigator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import kotlinx.parcelize.Parcelize
import me.ash.reader.ui.ext.collectAsStateValue
import me.ash.reader.ui.page.common.RouteName
import me.ash.reader.ui.page.home.HomeViewModel
import me.ash.reader.ui.page.home.flow.FlowPage
Expand All @@ -35,7 +37,11 @@ fun ArticleListReaderPage(
BackHandler(navigator.canNavigateBack()) {
navigator.navigateBack()
}
val currentArticle = navigator.currentDestination?.content
val readerState = readingViewModel.readerStateStateFlow.collectAsStateValue()

LaunchedEffect(navigator.currentDestination?.content) {
navigator.currentDestination?.content?.id?.let { readingViewModel.initData(it) }
}

ListDetailPaneScaffold(
modifier = modifier,
Expand All @@ -46,7 +52,7 @@ fun ArticleListReaderPage(
FlowPage(
homeViewModel = homeViewModel,
flowViewModel = flowViewModel,
readingArticleId = currentArticle?.id,
readingArticleId = readerState.articleId,
onNavigateToFeeds = {
if (navController.previousBackStackEntry == null) {
navController.navigate(RouteName.FEEDS) {
Expand All @@ -63,14 +69,11 @@ fun ArticleListReaderPage(
},
detailPane = {
AnimatedPane {
navigator.currentDestination?.content?.let {
ReadingPage(
navController = navController,
articleId = it.id,
homeViewModel = homeViewModel,
readingViewModel = readingViewModel
)
}
ReadingPage(
navController = navController,
homeViewModel = homeViewModel,
readingViewModel = readingViewModel
)
}
}
)
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.work.WorkInfo
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.ash.reader.R
import me.ash.reader.domain.model.article.ArticleFlowItem
import me.ash.reader.domain.model.article.ArticleWithFeed
import me.ash.reader.domain.model.general.Filter
import me.ash.reader.domain.model.general.MarkAsReadConditions
Expand Down Expand Up @@ -177,6 +178,20 @@ fun FlowPage(
}
}

LaunchedEffect(readingArticleId) {
if (readingArticleId != null) {
val item =
listState.layoutInfo.visibleItemsInfo.firstOrNull { it.key == readingArticleId }

val index = item?.index
?: pagingItems.itemSnapshotList.indexOfFirst { it is ArticleFlowItem.Article && it.articleWithFeed.article.id == readingArticleId }

if (index != -1) {
listState.animateScrollToItem(index, scrollOffset = -100)
}
}
}

BackHandler(onSearch) {
onSearch = false
}
Expand Down
21 changes: 10 additions & 11 deletions app/src/main/java/me/ash/reader/ui/page/home/reading/ReadingPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.isSpecified
import androidx.compose.ui.unit.sp
Expand Down Expand Up @@ -52,7 +54,6 @@ private const val DOWNWARD = -1
@Composable
fun ReadingPage(
navController: NavHostController,
articleId: String?,
homeViewModel: HomeViewModel,
readingViewModel: ReadingViewModel = hiltViewModel(),
) {
Expand All @@ -76,17 +77,13 @@ fun ReadingPage(

val pagingItems = homeUiState.pagingData.collectAsLazyPagingItems().itemSnapshotList

LaunchedEffect(articleId) {
if (articleId == null) {
navController.currentBackStackEntryFlow.collect {
it.arguments?.getString("articleId")?.let { articleId ->
if (readerState.articleId != articleId) {
readingViewModel.initData(articleId)
}
LaunchedEffect(Unit) {
navController.currentBackStackEntryFlow.collect {
it.arguments?.getString("articleId")?.let { articleId ->
if (readerState.articleId != articleId) {
readingViewModel.initData(articleId)
}
}
} else {
readingViewModel.initData(articleId)
}
}

Expand Down Expand Up @@ -177,7 +174,9 @@ fun ReadingPage(
}
) {
Box(
modifier = Modifier.fillMaxSize(),
modifier = Modifier
.fillMaxSize()
.nestedScroll(rememberNestedScrollInteropConnection()),
contentAlignment = Alignment.Center
) {
Content(
Expand Down

0 comments on commit fea521d

Please sign in to comment.