package eu.friedcutlet.yokan.ui.navigation import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.AccountTree import androidx.compose.material.icons.filled.CollectionsBookmark import androidx.compose.material.icons.filled.Explore import androidx.compose.material.icons.filled.History import androidx.compose.material.icons.filled.Home import androidx.compose.material.icons.filled.LocalLibrary import androidx.compose.material.icons.filled.MoreHoriz import androidx.compose.material.icons.filled.NewReleases import androidx.compose.material.icons.filled.VideoLibrary import androidx.compose.material.icons.outlined.AccountTree import androidx.compose.material.icons.outlined.CollectionsBookmark import androidx.compose.material.icons.outlined.Explore import androidx.compose.material.icons.outlined.History import androidx.compose.material.icons.outlined.Home import androidx.compose.material.icons.outlined.LocalLibrary import androidx.compose.material.icons.outlined.MoreHoriz import androidx.compose.material.icons.outlined.NewReleases import androidx.compose.material.icons.outlined.VideoLibrary import androidx.compose.runtime.Composable import androidx.compose.ui.graphics.vector.ImageVector import eu.friedcutlet.yokan.database.DbSettings import eu.friedcutlet.yokan.domain.ItemType import eu.friedcutlet.yokan.services.system.isLocalLibraryManagerSupported import eu.friedcutlet.yokan.services.system.isTvDevice import org.jetbrains.compose.resources.stringResource import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.nav_anime_library import yokan.composeapp.generated.resources.nav_browse import yokan.composeapp.generated.resources.nav_history import yokan.composeapp.generated.resources.nav_home import yokan.composeapp.generated.resources.nav_manga_library import yokan.composeapp.generated.resources.nav_more import yokan.composeapp.generated.resources.nav_novel_library import yokan.composeapp.generated.resources.nav_tracker_library import yokan.composeapp.generated.resources.nav_updates enum class NavSection(val title: String, val itemType: ItemType? = null) { Home("Home"), MangaLibrary("Manga", ItemType.Manga), AnimeLibrary("Anime", ItemType.Anime), NovelLibrary("Novel", ItemType.Novel), LocalLibrary("Local media"), TrackerLibrary("Tracking"), Updates("Updates"), History("History"), Browse("Browse"), More("More"), } fun ItemType.librarySection(): NavSection = when (this) { ItemType.Manga -> NavSection.MangaLibrary ItemType.Anime -> NavSection.AnimeLibrary ItemType.Novel -> NavSection.NovelLibrary } private val defaultNavigationOrder = listOf( NavSection.Home, NavSection.MangaLibrary, NavSection.AnimeLibrary, NavSection.NovelLibrary, NavSection.LocalLibrary, NavSection.Updates, NavSection.History, NavSection.Browse, NavSection.More, NavSection.TrackerLibrary, ) private val previousYokanDefaultNavigationOrder = listOf( NavSection.Updates, NavSection.MangaLibrary, NavSection.AnimeLibrary, NavSection.NovelLibrary, NavSection.LocalLibrary, NavSection.History, NavSection.Browse, NavSection.More, NavSection.TrackerLibrary, ) fun navigationSectionForStoredName(name: String): NavSection? = when (name.trim().removePrefix("/")) { "Home", "home" -> NavSection.Home "MangaLibrary" -> NavSection.MangaLibrary "AnimeLibrary" -> NavSection.AnimeLibrary "NovelLibrary" -> NavSection.NovelLibrary "LocalLibrary", "localLibrary" -> NavSection.LocalLibrary "updates", "Updates" -> NavSection.Updates "history", "History" -> NavSection.History "browse", "Browse" -> NavSection.Browse "more", "More" -> NavSection.More "trackerLibrary", "TrackerLibrary" -> NavSection.TrackerLibrary else -> null } fun hiddenNavigationSections(db: DbSettings): Set { val stored = db.hideItems.orEmpty().mapNotNull(::navigationSectionForStoredName).toSet() val tvAnimeOnly = isTvDevice() && (db.animeOnlyTvMode ?: true) return if (tvAnimeOnly) stored + setOf(NavSection.MangaLibrary, NavSection.NovelLibrary) else stored } fun completeNavigationOrder(db: DbSettings): List = (db.navigationOrder.orEmpty().mapNotNull(::navigationSectionForStoredName) + defaultNavigationOrder).distinct() /** The visible root sections, in order. */ fun navigationOrder(db: DbSettings): List { val hidden = hiddenNavigationSections(db) val localHidden = !db.showLocalLibraryNavigation || !isLocalLibraryManagerSupported() return completeNavigationOrder(db).filterNot { it in hidden || (it == NavSection.LocalLibrary && localHidden) } } fun DbSettings.withNavSectionMoved(section: NavSection, direction: Int): DbSettings { val current = completeNavigationOrder(this).toMutableList() val index = current.indexOf(section).takeIf { it >= 0 } ?: return this val target = (index + direction).coerceIn(0, current.size - 1) if (target == index) return this current.removeAt(index) current.add(target, section) return copy(navigationOrder = current.map(NavSection::name)) } fun DbSettings.withNavSectionHiddenToggled(section: NavSection): DbSettings { val hidden = hiddenNavigationSections(this).toMutableSet() if (!hidden.remove(section)) hidden.add(section) return copy(hideItems = hidden.map(NavSection::name)) } /** One-time migration from the previous Yokan default order (Updates first) to the Mangayomi one. */ internal fun DbSettings.withNavigationOrderMigrated(): DbSettings { val parsed = navigationOrder.orEmpty().mapNotNull(::navigationSectionForStoredName) val previousWithoutLocal = previousYokanDefaultNavigationOrder.filterNot { it == NavSection.LocalLibrary } return if (parsed == previousYokanDefaultNavigationOrder || parsed == previousWithoutLocal) { copy(navigationOrder = defaultNavigationOrder.map(NavSection::name)) } else { this } } data class NavIcon( val selected: ImageVector, val unselected: ImageVector, ) @Composable fun localizedTitle(section: NavSection): String = when (section) { NavSection.LocalLibrary -> "Local media" else -> stringResource( when (section) { NavSection.Home -> Res.string.nav_home NavSection.MangaLibrary -> Res.string.nav_manga_library NavSection.AnimeLibrary -> Res.string.nav_anime_library NavSection.NovelLibrary -> Res.string.nav_novel_library NavSection.TrackerLibrary -> Res.string.nav_tracker_library NavSection.Updates -> Res.string.nav_updates NavSection.History -> Res.string.nav_history NavSection.Browse -> Res.string.nav_browse NavSection.More -> Res.string.nav_more NavSection.LocalLibrary -> Res.string.nav_more }, ) } fun iconFor(section: NavSection): NavIcon = when (section) { NavSection.Home -> NavIcon(Icons.Filled.Home, Icons.Outlined.Home) NavSection.MangaLibrary -> NavIcon(Icons.Filled.CollectionsBookmark, Icons.Outlined.CollectionsBookmark) NavSection.AnimeLibrary -> NavIcon(Icons.Filled.VideoLibrary, Icons.Outlined.VideoLibrary) NavSection.NovelLibrary -> NavIcon(Icons.Filled.LocalLibrary, Icons.Outlined.LocalLibrary) NavSection.LocalLibrary -> NavIcon(Icons.Filled.LocalLibrary, Icons.Outlined.LocalLibrary) NavSection.TrackerLibrary -> NavIcon(Icons.Filled.AccountTree, Icons.Outlined.AccountTree) NavSection.Updates -> NavIcon(Icons.Filled.NewReleases, Icons.Outlined.NewReleases) NavSection.History -> NavIcon(Icons.Filled.History, Icons.Outlined.History) NavSection.Browse -> NavIcon(Icons.Filled.Explore, Icons.Outlined.Explore) NavSection.More -> NavIcon(Icons.Filled.MoreHoriz, Icons.Outlined.MoreHoriz) }