Enable 'Next Article' (#171)

This commit is contained in:
Zhiting Guo 2022-08-08 11:31:11 +08:00 committed by GitHub
parent 5eba7e4ce2
commit f695a2a040
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 2 deletions

View File

@ -132,7 +132,7 @@ fun HomeEntry(
)
}
animatedComposable(route = "${RouteName.READING}/{articleId}") {
ReadingPage(navController = navController)
ReadingPage(navController = navController, homeViewModel = homeViewModel)
}
// Settings

View File

@ -99,4 +99,5 @@ data class FilterState(
data class HomeUiState(
val pagingData: Flow<PagingData<ArticleFlowItem>> = emptyFlow(),
val searchContent: String = "",
var nextArticleId: String = ""
)

View File

@ -33,6 +33,7 @@ fun BottomBar(
isFullContent: Boolean,
onUnread: (isUnread: Boolean) -> Unit = {},
onStarred: (isStarred: Boolean) -> Unit = {},
onNextArticle: () -> Unit = {},
onFullContent: (isFullContent: Boolean) -> Unit = {},
) {
val tonalElevation = LocalReadingPageTonalElevation.current
@ -95,13 +96,14 @@ fun BottomBar(
onStarred(!isStarred)
}
CanBeDisabledIconButton(
disabled = true,
disabled = false,
modifier = Modifier.size(40.dp),
imageVector = Icons.Rounded.ExpandMore,
contentDescription = "Next Article",
tint = MaterialTheme.colorScheme.outline,
) {
view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP)
onNextArticle()
}
CanBeDisabledIconButton(
modifier = Modifier.size(36.dp),

View File

@ -9,25 +9,32 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.paging.compose.collectAsLazyPagingItems
import me.ash.reader.data.model.preference.LocalReadingAutoHideToolbar
import me.ash.reader.data.model.preference.LocalReadingPageTonalElevation
import me.ash.reader.ui.component.base.RYScaffold
import me.ash.reader.ui.ext.collectAsStateValue
import me.ash.reader.ui.ext.isScrollDown
import me.ash.reader.ui.page.home.HomeViewModel
@Composable
fun ReadingPage(
navController: NavHostController,
homeViewModel: HomeViewModel,
readingViewModel: ReadingViewModel = hiltViewModel(),
) {
val tonalElevation = LocalReadingPageTonalElevation.current
val readingUiState = readingViewModel.readingUiState.collectAsStateValue()
val homeUiState = homeViewModel.homeUiState.collectAsStateValue()
val isShowToolBar = if (LocalReadingAutoHideToolbar.current.value) {
readingUiState.articleWithFeed != null && !readingUiState.listState.isScrollDown()
} else {
true
}
val pagingItems = homeUiState.pagingData.collectAsLazyPagingItems().itemSnapshotList
readingViewModel.recorderNextArticle(readingUiState, homeUiState, pagingItems)
LaunchedEffect(Unit) {
navController.currentBackStackEntryFlow.collect {
it.arguments?.getString("articleId")?.let {
@ -90,6 +97,9 @@ fun ReadingPage(
onStarred = {
readingViewModel.markStarred(it)
},
onNextArticle = {
readingViewModel.nextArticle(navController, homeUiState.nextArticleId)
},
onFullContent = {
if (it) readingViewModel.renderFullContent()
else readingViewModel.renderDescriptionContent()

View File

@ -4,6 +4,8 @@ import android.util.Log
import androidx.compose.foundation.lazy.LazyListState
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.NavController
import androidx.paging.ItemSnapshotList
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
@ -11,9 +13,12 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import me.ash.reader.data.model.article.ArticleFlowItem
import me.ash.reader.data.model.article.ArticleWithFeed
import me.ash.reader.data.repository.RssHelper
import me.ash.reader.data.repository.RssRepository
import me.ash.reader.ui.page.common.RouteName
import me.ash.reader.ui.page.home.HomeUiState
import javax.inject.Inject
@HiltViewModel
@ -114,6 +119,13 @@ class ReadingViewModel @Inject constructor(
}
}
fun nextArticle(navController: NavController, nextArticleId: String) {
navController.popBackStack()
if (nextArticleId.isNotBlank()) {
navController.navigate("${RouteName.READING}/${nextArticleId}")
}
}
private fun showLoading() {
_readingUiState.update {
it.copy(isLoading = true)
@ -125,6 +137,30 @@ class ReadingViewModel @Inject constructor(
it.copy(isLoading = false)
}
}
fun recorderNextArticle(
readingUiState: ReadingUiState, homeUiState: HomeUiState, pagingItems:
ItemSnapshotList<ArticleFlowItem>
) {
if (pagingItems.size > 0) {
val cur = readingUiState.articleWithFeed?.article
if (cur != null) {
var found = false
for (item in pagingItems) {
if (item is ArticleFlowItem.Article) {
val itemId = item.articleWithFeed.article.id
if (itemId == cur.id) {
found = true
homeUiState.nextArticleId = ""
} else if (found) {
homeUiState.nextArticleId = itemId
break
}
}
}
}
}
}
}
data class ReadingUiState(