Skip to content

Commit

Permalink
Here lies "currentTab was used multiple times"
Browse files Browse the repository at this point in the history
Fixes #282
  • Loading branch information
AntsyLich committed Oct 29, 2024
1 parent 38d5fc9 commit 371c143
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ 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
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
Expand All @@ -33,20 +33,13 @@ import tachiyomi.presentation.core.i18n.stringResource
fun TabbedScreen(
titleRes: StringResource,
tabs: ImmutableList<TabContent>,
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]
Expand Down
35 changes: 26 additions & 9 deletions app/src/main/java/eu/kanade/tachiyomi/ui/browse/BrowseTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -45,6 +48,12 @@ data class BrowseTab(
navigator.push(GlobalSearchScreen())
}

private val switchToExtensionTabChannel = Channel<Unit>(1, BufferOverflow.DROP_OLDEST)

fun showExtension() {
switchToExtensionTabChannel.trySend(Unit)
}

@Composable
override fun Content() {
val context = LocalContext.current
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
15 changes: 10 additions & 5 deletions app/src/main/java/eu/kanade/tachiyomi/ui/home/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -90,7 +90,7 @@ object HomeScreen : Screen() {
startBar = {
if (isTabletUi()) {
NavigationRail {
tabs.fastForEach {
TABS.fastForEach {
NavigationRailItem(it)
}
}
Expand All @@ -107,7 +107,7 @@ object HomeScreen : Screen() {
exit = shrinkVertically(),
) {
NavigationBar {
tabs.fastForEach {
TABS.fastForEach {
NavigationBarItem(it)
}
}
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/eu/kanade/tachiyomi/ui/more/MoreTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 371c143

Please sign in to comment.