NavSection.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/navigation/NavSection.kt
1package eu.friedcutlet.yokan.ui.navigation23import androidx.compose.material.icons.Icons4import androidx.compose.material.icons.filled.AccountTree5import androidx.compose.material.icons.filled.CollectionsBookmark6import androidx.compose.material.icons.filled.Explore7import androidx.compose.material.icons.filled.History8import androidx.compose.material.icons.filled.Home9import androidx.compose.material.icons.filled.LocalLibrary10import androidx.compose.material.icons.filled.MoreHoriz11import androidx.compose.material.icons.filled.NewReleases12import androidx.compose.material.icons.filled.VideoLibrary13import androidx.compose.material.icons.outlined.AccountTree14import androidx.compose.material.icons.outlined.CollectionsBookmark15import androidx.compose.material.icons.outlined.Explore16import androidx.compose.material.icons.outlined.History17import androidx.compose.material.icons.outlined.Home18import androidx.compose.material.icons.outlined.LocalLibrary19import androidx.compose.material.icons.outlined.MoreHoriz20import androidx.compose.material.icons.outlined.NewReleases21import androidx.compose.material.icons.outlined.VideoLibrary22import androidx.compose.runtime.Composable23import androidx.compose.ui.graphics.vector.ImageVector24import eu.friedcutlet.yokan.database.DbSettings25import eu.friedcutlet.yokan.domain.ItemType26import eu.friedcutlet.yokan.services.system.isLocalLibraryManagerSupported27import eu.friedcutlet.yokan.services.system.isTvDevice28import org.jetbrains.compose.resources.stringResource29import yokan.composeapp.generated.resources.Res30import yokan.composeapp.generated.resources.nav_anime_library31import yokan.composeapp.generated.resources.nav_browse32import yokan.composeapp.generated.resources.nav_history33import yokan.composeapp.generated.resources.nav_home34import yokan.composeapp.generated.resources.nav_manga_library35import yokan.composeapp.generated.resources.nav_more36import yokan.composeapp.generated.resources.nav_novel_library37import yokan.composeapp.generated.resources.nav_tracker_library38import yokan.composeapp.generated.resources.nav_updates3940enum class NavSection(val title: String, val itemType: ItemType? = null) {41 Home("Home"),42 MangaLibrary("Manga", ItemType.Manga),43 AnimeLibrary("Anime", ItemType.Anime),44 NovelLibrary("Novel", ItemType.Novel),45 LocalLibrary("Local media"),46 TrackerLibrary("Tracking"),47 Updates("Updates"),48 History("History"),49 Browse("Browse"),50 More("More"),51}5253fun ItemType.librarySection(): NavSection = when (this) {54 ItemType.Manga -> NavSection.MangaLibrary55 ItemType.Anime -> NavSection.AnimeLibrary56 ItemType.Novel -> NavSection.NovelLibrary57}5859private val defaultNavigationOrder = listOf(60 NavSection.Home,61 NavSection.MangaLibrary,62 NavSection.AnimeLibrary,63 NavSection.NovelLibrary,64 NavSection.LocalLibrary,65 NavSection.Updates,66 NavSection.History,67 NavSection.Browse,68 NavSection.More,69 NavSection.TrackerLibrary,70)7172private val previousYokanDefaultNavigationOrder = listOf(73 NavSection.Updates,74 NavSection.MangaLibrary,75 NavSection.AnimeLibrary,76 NavSection.NovelLibrary,77 NavSection.LocalLibrary,78 NavSection.History,79 NavSection.Browse,80 NavSection.More,81 NavSection.TrackerLibrary,82)8384fun navigationSectionForStoredName(name: String): NavSection? = when (name.trim().removePrefix("/")) {85 "Home", "home" -> NavSection.Home86 "MangaLibrary" -> NavSection.MangaLibrary87 "AnimeLibrary" -> NavSection.AnimeLibrary88 "NovelLibrary" -> NavSection.NovelLibrary89 "LocalLibrary", "localLibrary" -> NavSection.LocalLibrary90 "updates", "Updates" -> NavSection.Updates91 "history", "History" -> NavSection.History92 "browse", "Browse" -> NavSection.Browse93 "more", "More" -> NavSection.More94 "trackerLibrary", "TrackerLibrary" -> NavSection.TrackerLibrary95 else -> null96}9798fun hiddenNavigationSections(db: DbSettings): Set<NavSection> {99 val stored = db.hideItems.orEmpty().mapNotNull(::navigationSectionForStoredName).toSet()100 val tvAnimeOnly = isTvDevice() && (db.animeOnlyTvMode ?: true)101 return if (tvAnimeOnly) stored + setOf(NavSection.MangaLibrary, NavSection.NovelLibrary) else stored102}103104fun completeNavigationOrder(db: DbSettings): List<NavSection> =105 (db.navigationOrder.orEmpty().mapNotNull(::navigationSectionForStoredName) + defaultNavigationOrder).distinct()106107/** The visible root sections, in order. */108fun navigationOrder(db: DbSettings): List<NavSection> {109 val hidden = hiddenNavigationSections(db)110 val localHidden = !db.showLocalLibraryNavigation || !isLocalLibraryManagerSupported()111 return completeNavigationOrder(db).filterNot { it in hidden || (it == NavSection.LocalLibrary && localHidden) }112}113114fun DbSettings.withNavSectionMoved(section: NavSection, direction: Int): DbSettings {115 val current = completeNavigationOrder(this).toMutableList()116 val index = current.indexOf(section).takeIf { it >= 0 } ?: return this117 val target = (index + direction).coerceIn(0, current.size - 1)118 if (target == index) return this119 current.removeAt(index)120 current.add(target, section)121 return copy(navigationOrder = current.map(NavSection::name))122}123124fun DbSettings.withNavSectionHiddenToggled(section: NavSection): DbSettings {125 val hidden = hiddenNavigationSections(this).toMutableSet()126 if (!hidden.remove(section)) hidden.add(section)127 return copy(hideItems = hidden.map(NavSection::name))128}129130/** One-time migration from the previous Yokan default order (Updates first) to the Mangayomi one. */131internal fun DbSettings.withNavigationOrderMigrated(): DbSettings {132 val parsed = navigationOrder.orEmpty().mapNotNull(::navigationSectionForStoredName)133 val previousWithoutLocal = previousYokanDefaultNavigationOrder.filterNot { it == NavSection.LocalLibrary }134 return if (parsed == previousYokanDefaultNavigationOrder || parsed == previousWithoutLocal) {135 copy(navigationOrder = defaultNavigationOrder.map(NavSection::name))136 } else {137 this138 }139}140141data class NavIcon(142 val selected: ImageVector,143 val unselected: ImageVector,144)145146@Composable147fun localizedTitle(section: NavSection): String = when (section) {148 NavSection.LocalLibrary -> "Local media"149 else -> stringResource(150 when (section) {151 NavSection.Home -> Res.string.nav_home152 NavSection.MangaLibrary -> Res.string.nav_manga_library153 NavSection.AnimeLibrary -> Res.string.nav_anime_library154 NavSection.NovelLibrary -> Res.string.nav_novel_library155 NavSection.TrackerLibrary -> Res.string.nav_tracker_library156 NavSection.Updates -> Res.string.nav_updates157 NavSection.History -> Res.string.nav_history158 NavSection.Browse -> Res.string.nav_browse159 NavSection.More -> Res.string.nav_more160 NavSection.LocalLibrary -> Res.string.nav_more161 },162 )163}164165fun iconFor(section: NavSection): NavIcon = when (section) {166 NavSection.Home -> NavIcon(Icons.Filled.Home, Icons.Outlined.Home)167 NavSection.MangaLibrary -> NavIcon(Icons.Filled.CollectionsBookmark, Icons.Outlined.CollectionsBookmark)168 NavSection.AnimeLibrary -> NavIcon(Icons.Filled.VideoLibrary, Icons.Outlined.VideoLibrary)169 NavSection.NovelLibrary -> NavIcon(Icons.Filled.LocalLibrary, Icons.Outlined.LocalLibrary)170 NavSection.LocalLibrary -> NavIcon(Icons.Filled.LocalLibrary, Icons.Outlined.LocalLibrary)171 NavSection.TrackerLibrary -> NavIcon(Icons.Filled.AccountTree, Icons.Outlined.AccountTree)172 NavSection.Updates -> NavIcon(Icons.Filled.NewReleases, Icons.Outlined.NewReleases)173 NavSection.History -> NavIcon(Icons.Filled.History, Icons.Outlined.History)174 NavSection.Browse -> NavIcon(Icons.Filled.Explore, Icons.Outlined.Explore)175 NavSection.More -> NavIcon(Icons.Filled.MoreHoriz, Icons.Outlined.MoreHoriz)176}177