Просмотр исходного кода

订阅源在未开启预加载时无法主动加载的问题

HapeLee 6 месяцев назад
Родитель
Сommit
c6cbfab0fa

+ 3 - 7
app/src/main/java/io/legado/app/ui/rss/article/RssArticlesCompose.kt

@@ -120,11 +120,7 @@ fun RssArticlesPage(
     val articles by articleFlow.collectAsStateWithLifecycle(initialValue = emptyList())
 
     LaunchedEffect(rssSource?.sourceUrl, sortName, sortUrl) {
-        rssSource?.let { source ->
-            if (isPreload) {
-                viewModel.loadArticles(source)
-            }
-        }
+        rssSource?.let(viewModel::loadArticles)
     }
 
     LaunchedEffect(loadState.errorMessage) {
@@ -308,7 +304,7 @@ private fun LoadMoreFooter(
     onRetry: () -> Unit
 ) {
     val text = when {
-        state.isLoadingMore -> "加载中..."
+        state.isRefreshing || state.isLoadingMore -> "加载中..."
         !state.hasMore -> "没有更多了"
         state.errorMessage != null -> "加载失败,点击重试"
         else -> "上拉加载更多"
@@ -326,7 +322,7 @@ private fun LoadMoreFooter(
     ) {
         EmptyMessage(
             message = text,
-            isLoading = state.isLoadingMore,
+            isLoading = state.isRefreshing || state.isLoadingMore,
             modifier = contentModifier
         )
     }

+ 0 - 1
app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt

@@ -90,7 +90,6 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
 
         val pageUrl = nextPageUrl
         if (pageUrl.isNullOrEmpty()) {
-            _loadState.value = currentState.copy(hasMore = false)
             return
         }
 

+ 13 - 12
app/src/main/java/io/legado/app/ui/rss/article/RssSortRouteScreen.kt

@@ -6,6 +6,7 @@ import android.app.Activity
 import androidx.activity.compose.LocalActivity
 import androidx.activity.compose.rememberLauncherForActivityResult
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
 import androidx.compose.runtime.getValue
 import androidx.compose.runtime.mutableIntStateOf
 import androidx.compose.runtime.mutableStateOf
@@ -52,26 +53,25 @@ fun RssSortRouteScreen(
     var showReadRecordSheet by remember { mutableStateOf(false) }
     var readRecords by remember { mutableStateOf<List<RssReadRecord>>(emptyList()) }
 
-    fun reloadSourceState() {
-        viewModel.initData(sourceUrl) {
-            scope.launch {
-                sortList = viewModel.loadSorts()
-                articleStyle = viewModel.currentArticleStyle()
-                screenTitle = viewModel.rssSource?.sourceName.orEmpty()
-                redirectPolicy = RedirectPolicy.fromString(viewModel.rssSource?.redirectPolicy)
-            }
+    suspend fun reloadSourceState() {
+        withContext(Dispatchers.IO) {
+            viewModel.initDataSource(sourceUrl)
         }
+        sortList = viewModel.loadSorts()
+        articleStyle = viewModel.currentArticleStyle()
+        screenTitle = viewModel.rssSource?.sourceName.orEmpty()
+        redirectPolicy = RedirectPolicy.fromString(viewModel.rssSource?.redirectPolicy)
     }
 
     val editSourceResult = rememberLauncherForActivityResult(
         StartActivityContract(RssSourceEditActivity::class.java)
     ) {
         if (it.resultCode == Activity.RESULT_OK) {
-            reloadSourceState()
+            scope.launch { reloadSourceState() }
         }
     }
 
-    androidx.compose.runtime.LaunchedEffect(sourceUrl) {
+    LaunchedEffect(sourceUrl) {
         reloadSourceState()
     }
 
@@ -91,8 +91,9 @@ fun RssSortRouteScreen(
             }
         },
         onRefreshSort = {
-            viewModel.clearSortCache {
-                scope.launch { sortList = viewModel.loadSorts() }
+            scope.launch {
+                viewModel.clearSortCache()
+                sortList = viewModel.loadSorts()
             }
         },
         onSetSourceVariable = {

+ 10 - 23
app/src/main/java/io/legado/app/ui/rss/article/RssSortViewModel.kt

@@ -1,7 +1,6 @@
 package io.legado.app.ui.rss.article
 
 import android.app.Application
-import android.content.Intent
 import androidx.lifecycle.MutableLiveData
 import io.legado.app.base.BaseViewModel
 import io.legado.app.data.appDb
@@ -21,23 +20,15 @@ class RssSortViewModel(application: Application) : BaseViewModel(application) {
     val isGridLayout get() = rssSource?.articleStyle == 2
     val isWaterLayout get() = rssSource?.articleStyle == 3
 
-    fun initData(intent: Intent, finally: () -> Unit) {
-        initData(intent.getStringExtra("url") ?: intent.getStringExtra("sourceUrl"), finally)
-    }
-
-    fun initData(sourceUrl: String?, finally: () -> Unit) {
-        execute {
-            url = sourceUrl
-            url?.let { sourceUrl ->
-                rssSource = appDb.rssSourceDao.getByKey(sourceUrl)
-                rssSource?.let {
-                    titleLiveData.postValue(it.sourceName)
-                } ?: let {
-                    rssSource = RssSource(sourceUrl = sourceUrl)
-                }
+    fun initDataSource(sourceUrl: String?) {
+        url = sourceUrl
+        url?.let { src ->
+            rssSource = appDb.rssSourceDao.getByKey(src)
+            rssSource?.let {
+                titleLiveData.postValue(it.sourceName)
+            } ?: run {
+                rssSource = RssSource(sourceUrl = src)
             }
-        }.onFinally {
-            finally()
         }
     }
 
@@ -80,12 +71,8 @@ class RssSortViewModel(application: Application) : BaseViewModel(application) {
         }
     }
 
-    fun clearSortCache(onFinally: () -> Unit) {
-        execute {
-            rssSource?.removeSortCache()
-        }.onFinally {
-            onFinally.invoke()
-        }
+    suspend fun clearSortCache() {
+        rssSource?.removeSortCache()
     }
 
     fun getRecords(): List<RssReadRecord> {

+ 5 - 1
app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesScreen.kt

@@ -31,6 +31,7 @@ import androidx.compose.ui.unit.dp
 import androidx.lifecycle.viewmodel.compose.viewModel
 import io.legado.app.R
 import io.legado.app.data.entities.RssStar
+import io.legado.app.ui.theme.adaptiveContentPadding
 import io.legado.app.ui.widget.components.ActionItem
 import io.legado.app.ui.widget.components.EmptyMessage
 import io.legado.app.ui.widget.components.SourceIcon
@@ -191,7 +192,10 @@ fun RssFavoritesScreen(
         } else {
             LazyColumn(
                 modifier = Modifier.fillMaxSize(),
-                contentPadding = paddingValues,
+                contentPadding = adaptiveContentPadding(
+                    top = paddingValues.calculateTopPadding(),
+                    bottom = paddingValues.calculateBottomPadding() + 120.dp
+                ),
                 verticalArrangement = Arrangement.spacedBy(8.dp)
             ) {
                 items(state.items, key = { "${it.origin}|${it.link}" }) { rssStar ->