Selaa lähdekoodia

refactor: 优化 screenState 的初始化方式,使用 lazy 以提高性能

5151561 3 kuukautta sitten
vanhempi
commit
3a6048f966
1 muutettua tiedostoa jossa 19 lisäystä ja 17 poistoa
  1. 19 17
      app/src/main/java/io/legado/app/ui/book/toc/TocViewModel.kt

+ 19 - 17
app/src/main/java/io/legado/app/ui/book/toc/TocViewModel.kt

@@ -293,24 +293,26 @@ class TocViewModel(
                 initialValue = emptyList()
             )
 
-    val screenState: StateFlow<TocUiState> = combine(
-        uiState,
-        bookState,
-        collapsedVolumes,
-        bookmarkUiList,
-    ) { action, book, collapsed, bookmarks ->
-        TocUiState(
-            action = action,
-            book = book,
-            collapsedVolumes = collapsed.toImmutableSet(),
-            bookmarks = bookmarks.toImmutableList(),
-            isSplitLongChapter = book?.getSplitLongChapter() ?: false,
+    val screenState: StateFlow<TocUiState> by lazy {
+        combine(
+            uiState,
+            bookState,
+            collapsedVolumes,
+            bookmarkUiList,
+        ) { action, book, collapsed, bookmarks ->
+            TocUiState(
+                action = action,
+                book = book,
+                collapsedVolumes = collapsed.toImmutableSet(),
+                bookmarks = bookmarks.toImmutableList(),
+                isSplitLongChapter = book?.getSplitLongChapter() ?: false,
+            )
+        }.stateIn(
+            scope = viewModelScope,
+            started = SharingStarted.WhileSubscribed(5_000),
+            initialValue = TocUiState(),
         )
-    }.stateIn(
-        scope = viewModelScope,
-        started = SharingStarted.WhileSubscribed(5_000),
-        initialValue = TocUiState(),
-    )
+    }
 
     private val reverseFlow =
         bookState.map { it?.getReverseToc() ?: false }