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}") { animatedComposable(route = "${RouteName.READING}/{articleId}") {
ReadingPage(navController = navController) ReadingPage(navController = navController, homeViewModel = homeViewModel)
} }
// Settings // Settings

View File

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

View File

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

View File

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

View File

@ -4,6 +4,8 @@ import android.util.Log
import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.LazyListState
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import androidx.navigation.NavController
import androidx.paging.ItemSnapshotList
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@ -11,9 +13,12 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch 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.model.article.ArticleWithFeed
import me.ash.reader.data.repository.RssHelper import me.ash.reader.data.repository.RssHelper
import me.ash.reader.data.repository.RssRepository 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 import javax.inject.Inject
@HiltViewModel @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() { private fun showLoading() {
_readingUiState.update { _readingUiState.update {
it.copy(isLoading = true) it.copy(isLoading = true)
@ -125,6 +137,30 @@ class ReadingViewModel @Inject constructor(
it.copy(isLoading = false) 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( data class ReadingUiState(