HapeLee 6 месяцев назад
Родитель
Сommit
91b9aa5088
1 измененных файлов с 89 добавлено и 32 удалено
  1. 89 32
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfScreen.kt

+ 89 - 32
app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfScreen.kt

@@ -2,7 +2,9 @@ package io.legado.app.ui.main.bookshelf
 
 
 import android.content.ClipData
 import android.content.ClipData
 import android.content.res.Configuration
 import android.content.res.Configuration
+import android.net.Uri
 import androidx.activity.compose.BackHandler
 import androidx.activity.compose.BackHandler
+import androidx.activity.compose.ManagedActivityResultLauncher
 import androidx.activity.compose.rememberLauncherForActivityResult
 import androidx.activity.compose.rememberLauncherForActivityResult
 import androidx.activity.result.contract.ActivityResultContracts
 import androidx.activity.result.contract.ActivityResultContracts
 import androidx.compose.animation.AnimatedContent
 import androidx.compose.animation.AnimatedContent
@@ -58,6 +60,7 @@ import androidx.compose.material3.pulltorefresh.pullToRefresh
 import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
 import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.LaunchedEffect
 import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.derivedStateOf
 import androidx.compose.runtime.getValue
 import androidx.compose.runtime.getValue
 import androidx.compose.runtime.remember
 import androidx.compose.runtime.remember
 import androidx.compose.runtime.rememberCoroutineScope
 import androidx.compose.runtime.rememberCoroutineScope
@@ -226,17 +229,29 @@ fun BookshelfScreen(
             }
             }
     }
     }
 
 
-    val currentTabGroupId =
-        uiState.groups.getOrNull(pagerState.currentPage)?.groupId ?: BookGroup.IdAll
-    val searchGroupExists = uiState.allGroups.any { it.groupId == uiState.selectedGroupId }
-    val currentGroupId = if (uiState.isSearch && searchGroupExists) {
-        uiState.selectedGroupId
-    } else {
-        currentTabGroupId
+    val currentTabGroupId by remember {
+        derivedStateOf {
+            uiState.groups.getOrNull(pagerState.currentPage)?.groupId ?: BookGroup.IdAll
+        }
+    }
+    val searchGroupExists by remember {
+        derivedStateOf { uiState.allGroups.any { it.groupId == uiState.selectedGroupId } }
+    }
+    val currentGroupId by remember {
+        derivedStateOf {
+            if (uiState.isSearch && searchGroupExists) {
+                uiState.selectedGroupId
+            } else {
+                currentTabGroupId
+            }
+        }
+    }
+    val isUsingStandaloneSearchGroup by remember {
+        derivedStateOf {
+            uiState.isSearch && uiState.groups.none { it.groupId == currentGroupId }
+        }
     }
     }
-    val isUsingStandaloneSearchGroup = uiState.isSearch &&
-            uiState.groups.none { it.groupId == currentGroupId }
-    val currentGroupBookCount = uiState.currentGroupBookCount
+    val currentGroupBookCount by remember { derivedStateOf { uiState.currentGroupBookCount } }
 
 
     val clearSelection = {
     val clearSelection = {
         viewModel.clearSelection()
         viewModel.clearSelection()
@@ -263,7 +278,7 @@ fun BookshelfScreen(
         }
         }
     }
     }
 
 
-    val currentGroupName = uiState.currentGroupName
+    val currentGroupName by remember { derivedStateOf { uiState.currentGroupName } }
 
 
     if (bookGroupStyle == 2 && !isInFolderRoot && !isEditMode) {
     if (bookGroupStyle == 2 && !isInFolderRoot && !isEditMode) {
         BackHandler {
         BackHandler {
@@ -273,22 +288,37 @@ fun BookshelfScreen(
 
 
     val configuration = LocalConfiguration.current
     val configuration = LocalConfiguration.current
     val isLandscape = configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
     val isLandscape = configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
-    val bookshelfLayoutMode =
-        if (isLandscape) BookshelfConfig.bookshelfLayoutModeLandscape else BookshelfConfig.bookshelfLayoutModePortrait
-    val bookshelfLayoutGrid =
-        if (isLandscape) BookshelfConfig.bookshelfLayoutGridLandscape else BookshelfConfig.bookshelfLayoutGridPortrait
-    val bookshelfLayoutList =
-        if (isLandscape) BookshelfConfig.bookshelfLayoutListLandscape else BookshelfConfig.bookshelfLayoutListPortrait
-    val currentMenuGroupId = if (uiState.isSearch) uiState.selectedGroupId else currentTabGroupId
-    val editStickySummary = if (isEditMode) {
-        BookshelfEditStickySummary(
-            selectedCount = selectedBookUrls.size,
-            currentGroupTotalCount = currentGroupBookCount,
-            groupName = currentGroupName,
-            showGroupName = bookGroupStyle != 0
-        )
-    } else {
-        null
+    val bookshelfLayoutMode by remember {
+        derivedStateOf {
+            if (isLandscape) BookshelfConfig.bookshelfLayoutModeLandscape else BookshelfConfig.bookshelfLayoutModePortrait
+        }
+    }
+    val bookshelfLayoutGrid by remember {
+        derivedStateOf {
+            if (isLandscape) BookshelfConfig.bookshelfLayoutGridLandscape else BookshelfConfig.bookshelfLayoutGridPortrait
+        }
+    }
+    val bookshelfLayoutList by remember {
+        derivedStateOf {
+            if (isLandscape) BookshelfConfig.bookshelfLayoutListLandscape else BookshelfConfig.bookshelfLayoutListPortrait
+        }
+    }
+    val currentMenuGroupId by remember {
+        derivedStateOf { if (uiState.isSearch) uiState.selectedGroupId else currentTabGroupId }
+    }
+    val editStickySummary by remember {
+        derivedStateOf {
+            if (isEditMode) {
+                BookshelfEditStickySummary(
+                    selectedCount = selectedBookUrls.size,
+                    currentGroupTotalCount = currentGroupBookCount,
+                    groupName = currentGroupName,
+                    showGroupName = bookGroupStyle != 0
+                )
+            } else {
+                null
+            }
+        }
     }
     }
 
 
     ListScaffold(
     ListScaffold(
@@ -604,12 +634,18 @@ fun BookshelfScreen(
         } else null
         } else null
     ) { paddingValues ->
     ) { paddingValues ->
         val pullToRefreshState = rememberPullToRefreshState()
         val pullToRefreshState = rememberPullToRefreshState()
-        val currentGroup = if (uiState.isSearch) {
-            uiState.allGroups.firstOrNull { it.groupId == currentGroupId }
-        } else {
-            uiState.groups.getOrNull(pagerState.currentPage)
+        val currentGroup by remember {
+            derivedStateOf {
+                if (uiState.isSearch) {
+                    uiState.allGroups.firstOrNull { it.groupId == currentGroupId }
+                } else {
+                    uiState.groups.getOrNull(pagerState.currentPage)
+                }
+            }
+        }
+        val pullToRefreshEnabled by remember {
+            derivedStateOf { (currentGroup?.enableRefresh ?: true) && !isEditMode }
         }
         }
-        val pullToRefreshEnabled = (currentGroup?.enableRefresh ?: true) && !isEditMode
 
 
         Box(
         Box(
             modifier = Modifier
             modifier = Modifier
@@ -900,6 +936,27 @@ fun BookshelfScreen(
         }
         }
     }
     }
 
 
+    BookshelfOverlays(
+        activeOverlay = activeOverlay,
+        uiState = uiState,
+        viewModel = viewModel,
+        selectedBookUrls = selectedBookUrls,
+        importLauncher = importLauncher,
+        exportLauncher = exportLauncher,
+        clearSelection = clearSelection
+    )
+}
+
+@Composable
+private fun BookshelfOverlays(
+    activeOverlay: BookshelfOverlay?,
+    uiState: BookshelfUiState,
+    viewModel: BookshelfViewModel,
+    selectedBookUrls: Set<String>,
+    importLauncher: ManagedActivityResultLauncher<Array<String>, Uri?>,
+    exportLauncher: ManagedActivityResultLauncher<String, Uri?>,
+    clearSelection: () -> Unit
+) {
     BookshelfConfigSheet(
     BookshelfConfigSheet(
         show = activeOverlay == BookshelfOverlay.ConfigSheet,
         show = activeOverlay == BookshelfOverlay.ConfigSheet,
         onDismissRequest = { viewModel.dismissOverlay() }
         onDismissRequest = { viewModel.dismissOverlay() }