Yokan / rebuild
Course 1 8624561

LocalLibraryProjection.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/library/LocalLibraryProjection.kt

implementation58 lines
1package eu.friedcutlet.yokan.services.library23import eu.friedcutlet.yokan.data.toDomainMangas4import eu.friedcutlet.yokan.database.DbLocalFolder5import eu.friedcutlet.yokan.database.DbManga6import eu.friedcutlet.yokan.database.DbTitleMetadata7import eu.friedcutlet.yokan.database.YokanDatabaseSnapshot8import eu.friedcutlet.yokan.domain.ItemType9import eu.friedcutlet.yokan.domain.Manga10import eu.friedcutlet.yokan.util.PlatformLock1112/** Both local screens share these immutable lists; scan status and folder edits don't rebuild them. */13internal class LocalLibraryProjection(snapshot: YokanDatabaseSnapshot) {14    // Season records are reached from their parent title's detail screen, never listed separately.15    private val local = snapshot.mangas.filter { it.isLocalArchive && it.parentId == null }.toDomainMangas(snapshot.settings)16    private val managed = local.filterNot { it.source.equals("torrent", ignoreCase = true) }17    val titlesByType = ItemType.entries.associateWith { type -> managed.filter { it.itemType == type } }18    val browseTitlesByType = ItemType.entries.associateWith { type ->19        local.filter { it.itemType == type }.sortedBy { it.name.lowercase() }20    }21    val reviewQueue = ItemType.entries.flatMap { titlesByType.getValue(it) }22        .filter { it.metadataServerId == null || it.description.isBlank() }.distinctBy { it.id }23    private val names = snapshot.mangas.associate { it.id to it.name.orEmpty() }2425    fun titleNames(reconciliation: LocalLibraryReconciliationResult?): Map<Long, String> {26        reconciliation ?: return emptyMap()27        val ids = reconciliation.staleTitleCandidates.map { it.id } + reconciliation.duplicateTitleGroups.flatMap { it.ids }28        return ids.distinct().mapNotNull { id -> names[id]?.let { id to it } }.toMap()29    }3031    fun overview(folders: List<DbLocalFolder>, scan: LocalLibraryScanState) = LocalLibraryOverview(32        folders = folders,33        manga = titlesByType.getValue(ItemType.Manga).size,34        anime = titlesByType.getValue(ItemType.Anime).size,35        novels = titlesByType.getValue(ItemType.Novel).size,36        lastScan = scan.lastScan,37        scanRunning = scan.running,38        scanError = scan.error,39        reconciliation = scan.reconciliation,40    )41}4243/** Controller-owned and bounded to one revision; collectors can run on different dispatchers. */44internal class LocalLibraryProjectionCache {45    private val lock = PlatformLock()46    private var mangas: List<DbManga>? = null47    private var metadata: List<DbTitleMetadata>? = null48    private var cached: LocalLibraryProjection? = null4950    fun get(snapshot: YokanDatabaseSnapshot): LocalLibraryProjection = lock.withLock {51        if (cached == null || mangas !== snapshot.mangas || metadata != snapshot.settings.titleMetadata) {52            cached = LocalLibraryProjection(snapshot)53            mangas = snapshot.mangas54            metadata = snapshot.settings.titleMetadata55        }56        requireNotNull(cached)57    }58}59

Search chapters, reference and source filenames.