diff --git a/app/src/main/java/me/ash/reader/MainActivity.kt b/app/src/main/java/me/ash/reader/MainActivity.kt index 9873d80..cab6743 100644 --- a/app/src/main/java/me/ash/reader/MainActivity.kt +++ b/app/src/main/java/me/ash/reader/MainActivity.kt @@ -17,9 +17,7 @@ class MainActivity : ComponentActivity() { WindowCompat.setDecorFitsSystemWindows(window, false) Log.i("RLog", "onCreate: ${ProfileInstallerInitializer().create(this)}") setContent { - HomeEntry(intent.extras).also { - intent.replaceExtras(null) - } + HomeEntry() } } } \ No newline at end of file diff --git a/app/src/main/java/me/ash/reader/ui/page/common/HomeEntry.kt b/app/src/main/java/me/ash/reader/ui/page/common/HomeEntry.kt index d1f0ca4..47781b6 100644 --- a/app/src/main/java/me/ash/reader/ui/page/common/HomeEntry.kt +++ b/app/src/main/java/me/ash/reader/ui/page/common/HomeEntry.kt @@ -1,6 +1,5 @@ package me.ash.reader.ui.page.common -import android.os.Bundle import androidx.compose.animation.* import androidx.compose.animation.core.Spring import androidx.compose.animation.core.spring @@ -28,9 +27,7 @@ import me.ash.reader.ui.theme.AppTheme @OptIn(ExperimentalAnimationApi::class, androidx.compose.material.ExperimentalMaterialApi::class) @Composable -fun HomeEntry( - extras: Bundle? = null, -) { +fun HomeEntry() { val navController = rememberAnimatedNavController() AppTheme { @@ -89,7 +86,7 @@ fun HomeEntry( ) + fadeOut(animationSpec = tween(220, delayMillis = 90)) }, ) { - HomePage(navController, extras?.get(ExtraName.ARTICLE_ID)) + HomePage(navController) } composable( route = RouteName.SETTINGS, diff --git a/app/src/main/java/me/ash/reader/ui/page/home/HomePage.kt b/app/src/main/java/me/ash/reader/ui/page/home/HomePage.kt index 7d89de0..e2c8546 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/HomePage.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/HomePage.kt @@ -1,16 +1,18 @@ package me.ash.reader.ui.page.home -import android.util.Log import androidx.activity.compose.BackHandler import androidx.compose.foundation.layout.Column -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.* +import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.hilt.navigation.compose.hiltViewModel import androidx.navigation.NavHostController import com.google.accompanist.pager.ExperimentalPagerApi +import kotlinx.coroutines.launch import me.ash.reader.ui.extension.collectAsStateValue +import me.ash.reader.ui.extension.findActivity +import me.ash.reader.ui.page.common.ExtraName import me.ash.reader.ui.page.home.drawer.feed.FeedOptionDrawer import me.ash.reader.ui.page.home.drawer.feed.FeedOptionViewAction import me.ash.reader.ui.page.home.drawer.feed.FeedOptionViewModel @@ -25,17 +27,44 @@ import me.ash.reader.ui.widget.ViewPager @Composable fun HomePage( navController: NavHostController, - extrasArticleId: Any? = null, homeViewModel: HomeViewModel = hiltViewModel(), readViewModel: ReadViewModel = hiltViewModel(), feedOptionViewModel: FeedOptionViewModel = hiltViewModel(), ) { + val context = LocalContext.current + val intent = remember { context.findActivity()?.intent } val scope = rememberCoroutineScope() val viewState = homeViewModel.viewState.collectAsStateValue() val filterState = homeViewModel.filterState.collectAsStateValue() val syncState = homeViewModel.syncState.collectAsStateValue() - OpenArticleByExtras(extrasArticleId) + var openArticleId by rememberSaveable { + mutableStateOf(intent?.extras?.get(ExtraName.ARTICLE_ID)?.toString() ?: "") + }.also { + intent?.replaceExtras(null) + } + + LaunchedEffect(openArticleId) { + if (openArticleId.isNotEmpty()) { + readViewModel.dispatch(ReadViewAction.ScrollToItem(2)) + launch { + val article = readViewModel + .rssRepository.get() + .findArticleById(openArticleId) ?: return@launch + readViewModel.dispatch(ReadViewAction.InitData(article)) + if (article.feed.isFullContent) readViewModel.dispatch(ReadViewAction.RenderFullContent) + else readViewModel.dispatch(ReadViewAction.RenderDescriptionContent) + readViewModel.dispatch(ReadViewAction.RenderDescriptionContent) + homeViewModel.dispatch( + HomeViewAction.ScrollToPage( + scope = scope, + targetPage = 2, + ) + ) + openArticleId = "" + } + } + } BackHandler(true) { val currentPage = viewState.pagerState.currentPage @@ -58,15 +87,6 @@ fun HomePage( ) } - LaunchedEffect(homeViewModel.viewState) { - homeViewModel.viewState.collect { - Log.i( - "RLog", - "HomePage: ${it.pagerState.currentPage}, ${it.pagerState.targetPage}, ${it.pagerState.currentPageOffset}" - ) - } - } - Column { ViewPager( modifier = Modifier.weight(1f), diff --git a/app/src/main/java/me/ash/reader/ui/page/home/OpenArticleByExtras.kt b/app/src/main/java/me/ash/reader/ui/page/home/OpenArticleByExtras.kt deleted file mode 100644 index a217df5..0000000 --- a/app/src/main/java/me/ash/reader/ui/page/home/OpenArticleByExtras.kt +++ /dev/null @@ -1,38 +0,0 @@ -package me.ash.reader.ui.page.home - -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.rememberCoroutineScope -import androidx.hilt.navigation.compose.hiltViewModel -import kotlinx.coroutines.launch -import me.ash.reader.ui.page.home.read.ReadViewAction -import me.ash.reader.ui.page.home.read.ReadViewModel - -@Composable -fun OpenArticleByExtras( - extrasArticleId: Any? = null, - homeViewModel: HomeViewModel = hiltViewModel(), - readViewModel: ReadViewModel = hiltViewModel(), -) { - val scope = rememberCoroutineScope() - LaunchedEffect(extrasArticleId) { - extrasArticleId?.let { - readViewModel.dispatch(ReadViewAction.ScrollToItem(2)) - this.launch { - val article = readViewModel - .rssRepository.get() - .findArticleById(it.toString()) ?: return@launch - readViewModel.dispatch(ReadViewAction.InitData(article)) - if (article.feed.isFullContent) readViewModel.dispatch(ReadViewAction.RenderFullContent) - else readViewModel.dispatch(ReadViewAction.RenderDescriptionContent) - readViewModel.dispatch(ReadViewAction.RenderDescriptionContent) - homeViewModel.dispatch( - HomeViewAction.ScrollToPage( - scope = scope, - targetPage = 2, - ) - ) - } - } - } -} \ No newline at end of file