From 6865c21c75b67708b26cc78d68747945ea206641 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Wed, 14 Sep 2022 22:36:13 +0700 Subject: [PATCH] Fix derivedStateOf errors (#8008) --- .../presentation/library/LibraryScreen.kt | 3 ++- .../library/components/LibraryContent.kt | 4 ++-- .../library/components/LibraryPager.kt | 9 ++++---- .../tachiyomi/ui/library/LibraryPresenter.kt | 21 +++++-------------- .../tachiyomi/ui/manga/MangaController.kt | 10 +-------- 5 files changed, 14 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt b/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt index c45aacd1aa..54f6d7cdaa 100644 --- a/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt @@ -11,6 +11,7 @@ import eu.kanade.presentation.library.components.LibraryContent import eu.kanade.presentation.library.components.LibraryToolbar import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.ui.library.LibraryPresenter +import eu.kanade.tachiyomi.ui.library.setting.display @Composable fun LibraryScreen( @@ -72,7 +73,7 @@ fun LibraryScreen( onRefresh = onClickRefresh, onGlobalSearchClicked = onGlobalSearchClicked, getNumberOfMangaForCategory = { presenter.getMangaCountForCategory(it) }, - getDisplayModeForPage = { presenter.getDisplayMode(index = it) }, + getDisplayModeForPage = { presenter.categories[it].display }, getColumnsForOrientation = { presenter.getColumnsPreferenceForCurrentOrientation(it) }, getLibraryForPage = { presenter.getMangaForCategory(page = it) }, isIncognitoMode = presenter.isIncognitoMode, diff --git a/app/src/main/java/eu/kanade/presentation/library/components/LibraryContent.kt b/app/src/main/java/eu/kanade/presentation/library/components/LibraryContent.kt index 3e73aa8f84..440214fc8b 100644 --- a/app/src/main/java/eu/kanade/presentation/library/components/LibraryContent.kt +++ b/app/src/main/java/eu/kanade/presentation/library/components/LibraryContent.kt @@ -45,9 +45,9 @@ fun LibraryContent( onRefresh: (Category?) -> Boolean, onGlobalSearchClicked: () -> Unit, getNumberOfMangaForCategory: @Composable (Long) -> State, - getDisplayModeForPage: @Composable (Int) -> State, + getDisplayModeForPage: @Composable (Int) -> LibraryDisplayMode, getColumnsForOrientation: (Boolean) -> PreferenceMutableState, - getLibraryForPage: @Composable (Int) -> State>, + getLibraryForPage: @Composable (Int) -> List, ) { Column( modifier = Modifier.padding(contentPadding), diff --git a/app/src/main/java/eu/kanade/presentation/library/components/LibraryPager.kt b/app/src/main/java/eu/kanade/presentation/library/components/LibraryPager.kt index 648bcf72a6..a8d15c385b 100644 --- a/app/src/main/java/eu/kanade/presentation/library/components/LibraryPager.kt +++ b/app/src/main/java/eu/kanade/presentation/library/components/LibraryPager.kt @@ -3,7 +3,6 @@ package eu.kanade.presentation.library.components import android.content.res.Configuration import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable -import androidx.compose.runtime.State import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -24,9 +23,9 @@ fun LibraryPager( selectedManga: List, searchQuery: String?, onGlobalSearchClicked: () -> Unit, - getDisplayModeForPage: @Composable (Int) -> State, + getDisplayModeForPage: @Composable (Int) -> LibraryDisplayMode, getColumnsForOrientation: (Boolean) -> PreferenceMutableState, - getLibraryForPage: @Composable (Int) -> State>, + getLibraryForPage: @Composable (Int) -> List, onClickManga: (LibraryManga) -> Unit, onLongClickManga: (LibraryManga) -> Unit, ) { @@ -40,8 +39,8 @@ fun LibraryPager( // To make sure only one offscreen page is being composed return@HorizontalPager } - val library by getLibraryForPage(page) - val displayMode by getDisplayModeForPage(page) + val library = getLibraryForPage(page) + val displayMode = getDisplayModeForPage(page) val columns by if (displayMode != LibraryDisplayMode.List) { val configuration = LocalConfiguration.current val isLandscape = configuration.orientation == Configuration.ORIENTATION_LANDSCAPE diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index 2ab3cc804f..4a0b2222e9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -43,9 +43,7 @@ import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter -import eu.kanade.tachiyomi.ui.library.setting.LibraryDisplayMode import eu.kanade.tachiyomi.ui.library.setting.LibrarySort -import eu.kanade.tachiyomi.ui.library.setting.display import eu.kanade.tachiyomi.ui.library.setting.sort import eu.kanade.tachiyomi.util.lang.combineLatest import eu.kanade.tachiyomi.util.lang.launchIO @@ -642,13 +640,12 @@ class LibraryPresenter( } @Composable - fun getMangaForCategory(page: Int): androidx.compose.runtime.State> { - val categoryId = remember(categories) { - categories.getOrNull(page)?.id ?: -1 + fun getMangaForCategory(page: Int): List { + val unfiltered = remember(categories, loadedManga) { + val categoryId = categories.getOrNull(page)?.id ?: -1 + loadedManga[categoryId] ?: emptyList() } - val unfiltered = loadedManga[categoryId] ?: emptyList() - - return derivedStateOf { + return remember(unfiltered) { val query = searchQuery if (query.isNullOrBlank().not()) { unfiltered.filter { @@ -660,14 +657,6 @@ class LibraryPresenter( } } - @Composable - fun getDisplayMode(index: Int): androidx.compose.runtime.State { - val category = categories[index] - return derivedStateOf { - category.display - } - } - fun clearSelection() { state.selection = emptyList() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index 3693a5e2f8..b7cf7c2a3b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -11,7 +11,6 @@ import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.SnackbarResult import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState -import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope @@ -111,13 +110,6 @@ class MangaController : FullComposeController { return } - val dialog by derivedStateOf { - when (val state = state) { - MangaScreenState.Loading -> null - is MangaScreenState.Success -> state.dialog - } - } - val successState = state as MangaScreenState.Success val isHttpSource = remember { successState.source is HttpSource } val scope = rememberCoroutineScope() @@ -152,7 +144,7 @@ class MangaController : FullComposeController { ) val onDismissRequest = { presenter.dismissDialog() } - when (val dialog = dialog) { + when (val dialog = (state as? MangaScreenState.Success)?.dialog) { is Dialog.ChangeCategory -> { ChangeCategoryDialog( initialSelection = dialog.initialSelection,