diff --git a/CHANGELOG.md b/CHANGELOG.md index 21c6c583f..bd7d28912 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ The format is a modified version of [Keep a Changelog](https://keepachangelog.co - `Other` - for technical stuff. ## [Unreleased] +### Fixed +- Fixed "currentTab was used multiple times" ## [v0.17.0] - 2024-10-26 ### Added diff --git a/app/src/main/java/eu/kanade/presentation/components/TabbedScreen.kt b/app/src/main/java/eu/kanade/presentation/components/TabbedScreen.kt index efb1e2e04..31cacae13 100644 --- a/app/src/main/java/eu/kanade/presentation/components/TabbedScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/components/TabbedScreen.kt @@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.calculateStartPadding import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.pager.HorizontalPager +import androidx.compose.foundation.pager.PagerState import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.material3.MaterialTheme import androidx.compose.material3.PrimaryTabRow @@ -14,7 +15,6 @@ import androidx.compose.material3.SnackbarHost import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.Tab import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment @@ -33,20 +33,13 @@ import tachiyomi.presentation.core.i18n.stringResource fun TabbedScreen( titleRes: StringResource, tabs: ImmutableList, - startIndex: Int? = null, + state: PagerState = rememberPagerState { tabs.size }, searchQuery: String? = null, onChangeSearchQuery: (String?) -> Unit = {}, ) { val scope = rememberCoroutineScope() - val state = rememberPagerState { tabs.size } val snackbarHostState = remember { SnackbarHostState() } - LaunchedEffect(startIndex) { - if (startIndex != null) { - state.scrollToPage(startIndex) - } - } - Scaffold( topBar = { val tab = tabs[state.currentPage] diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/BrowseTab.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/BrowseTab.kt index 2ce4685bb..166ad2ca5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/BrowseTab.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/BrowseTab.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.ui.browse import androidx.compose.animation.graphics.res.animatedVectorResource import androidx.compose.animation.graphics.res.rememberAnimatedVectorPainter import androidx.compose.animation.graphics.vector.AnimatedImageVector +import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState @@ -22,12 +23,14 @@ import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchScreen import eu.kanade.tachiyomi.ui.browse.source.sourcesTab import eu.kanade.tachiyomi.ui.main.MainActivity import kotlinx.collections.immutable.persistentListOf +import kotlinx.coroutines.channels.BufferOverflow +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.receiveAsFlow import tachiyomi.i18n.MR import tachiyomi.presentation.core.i18n.stringResource -data class BrowseTab( - private val toExtensions: Boolean = false, -) : Tab { +data object BrowseTab : Tab { override val options: TabOptions @Composable @@ -45,6 +48,12 @@ data class BrowseTab( navigator.push(GlobalSearchScreen()) } + private val switchToExtensionTabChannel = Channel(1, BufferOverflow.DROP_OLDEST) + + fun showExtension() { + switchToExtensionTabChannel.trySend(Unit) + } + @Composable override fun Content() { val context = LocalContext.current @@ -53,17 +62,25 @@ data class BrowseTab( val extensionsScreenModel = rememberScreenModel { ExtensionsScreenModel() } val extensionsState by extensionsScreenModel.state.collectAsState() + val tabs = persistentListOf( + sourcesTab(), + extensionsTab(extensionsScreenModel), + migrateSourceTab(), + ) + + val state = rememberPagerState { tabs.size } + TabbedScreen( titleRes = MR.strings.browse, - tabs = persistentListOf( - sourcesTab(), - extensionsTab(extensionsScreenModel), - migrateSourceTab(), - ), - startIndex = 1.takeIf { toExtensions }, + tabs = tabs, + state = state, searchQuery = extensionsState.searchQuery, onChangeSearchQuery = extensionsScreenModel::search, ) + LaunchedEffect(Unit) { + switchToExtensionTabChannel.receiveAsFlow() + .collectLatest { state.scrollToPage(1) } + } LaunchedEffect(Unit) { (context as? MainActivity)?.ready = true diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/history/HistoryTab.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/history/HistoryTab.kt index 9f91706f1..575f586f9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/history/HistoryTab.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/history/HistoryTab.kt @@ -32,7 +32,7 @@ import tachiyomi.domain.chapter.model.Chapter import tachiyomi.i18n.MR import tachiyomi.presentation.core.i18n.stringResource -object HistoryTab : Tab { +data object HistoryTab : Tab { private val snackbarHostState = SnackbarHostState() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/home/HomeScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/home/HomeScreen.kt index ac708ec2f..f07015eb1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/home/HomeScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/home/HomeScreen.kt @@ -69,11 +69,11 @@ object HomeScreen : Screen() { private const val TAB_FADE_DURATION = 200 private const val TAB_NAVIGATOR_KEY = "HomeTabs" - private val tabs = listOf( + private val TABS = listOf( LibraryTab, UpdatesTab, HistoryTab, - BrowseTab(), + BrowseTab, MoreTab, ) @@ -90,7 +90,7 @@ object HomeScreen : Screen() { startBar = { if (isTabletUi()) { NavigationRail { - tabs.fastForEach { + TABS.fastForEach { NavigationRailItem(it) } } @@ -107,7 +107,7 @@ object HomeScreen : Screen() { exit = shrinkVertically(), ) { NavigationBar { - tabs.fastForEach { + TABS.fastForEach { NavigationBarItem(it) } } @@ -159,7 +159,12 @@ object HomeScreen : Screen() { is Tab.Library -> LibraryTab Tab.Updates -> UpdatesTab Tab.History -> HistoryTab - is Tab.Browse -> BrowseTab(it.toExtensions) + is Tab.Browse -> { + if (it.toExtensions) { + BrowseTab.showExtension() + } + BrowseTab + } is Tab.More -> MoreTab } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryTab.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryTab.kt index 7acdb51f7..a42a9bac0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryTab.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryTab.kt @@ -61,7 +61,7 @@ import tachiyomi.presentation.core.screens.EmptyScreenAction import tachiyomi.presentation.core.screens.LoadingScreen import tachiyomi.source.local.isLocal -object LibraryTab : Tab { +data object LibraryTab : Tab { override val options: TabOptions @Composable diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/more/MoreTab.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/more/MoreTab.kt index 0279a6975..0201ddc44 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/more/MoreTab.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/more/MoreTab.kt @@ -38,7 +38,7 @@ import tachiyomi.presentation.core.i18n.stringResource import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get -object MoreTab : Tab { +data object MoreTab : Tab { override val options: TabOptions @Composable diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesTab.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesTab.kt index 8064d123a..7233ca818 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesTab.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/updates/UpdatesTab.kt @@ -31,7 +31,7 @@ import tachiyomi.core.common.i18n.stringResource import tachiyomi.i18n.MR import tachiyomi.presentation.core.i18n.stringResource -object UpdatesTab : Tab { +data object UpdatesTab : Tab { override val options: TabOptions @Composable