From bb9dfa205bcb6f05ffc1c6217f64d928b2a20429 Mon Sep 17 00:00:00 2001 From: Shamicen Date: Mon, 26 Feb 2024 12:31:58 +0100 Subject: [PATCH] Added support for updating manga while the app is running --- .../tachiyomi/source/local/LocalSource.kt | 45 +++++++++++++++---- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt b/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt index 9ad90bad5..247174eea 100644 --- a/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt +++ b/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt @@ -75,9 +75,9 @@ actual class LocalSource( private val mangaRepository: MangaRepository by injectLazy() - private var localManga: List = emptyList() + private var localManga: MutableList> = mutableListOf() - private val mangaChunks: List> by lazy { + private var mangaChunks: List> = fileSystem.getFilesInBaseDirectory() // Filter out files that are hidden and is not a folder .asSequence() @@ -87,7 +87,6 @@ actual class LocalSource( .toList() .chunked(MANGA_LOADING_CHUNK_SIZE) .toList() - } private var loadedPages = 0 private var currentlyLoadingPage: Int? = null @@ -108,6 +107,33 @@ actual class LocalSource( override val supportsLatest: Boolean = true + private fun checkForNewManga() { + if (!allMangaLoaded || currentlyLoadingPage != null) return + + val newManga = fileSystem.getFilesInBaseDirectory() + // Filter out files that are hidden and is not a folder + .asSequence() + .filter { it.isDirectory && it.name?.startsWith('.') == false } + .filterNot { mangaDir -> mangaChunks.flatten().map { it.name }.contains(mangaDir.name) } + .distinctBy { it.name } + .sortedBy { it.lastModified() } + .toList() + + if (newManga.isNotEmpty()) { + mangaChunks = mangaChunks + .flatten() + .plus(newManga) + .distinctBy { it.name } + .chunked(MANGA_LOADING_CHUNK_SIZE) + + allMangaLoaded = false + if (localManga.last().size % MANGA_LOADING_CHUNK_SIZE != 0) { + localManga = localManga.dropLast(1).toMutableList() + loadedPages-- + } + } + } + private fun loadMangaForPage(page: Int) { if (page != loadedPages + 1) return if (page == currentlyLoadingPage) return @@ -158,7 +184,7 @@ actual class LocalSource( } }.toList() - localManga = localManga.plus(mangaPage) + localManga.add(mangaPage) loadedPages++ currentlyLoadingPage = null } @@ -180,6 +206,7 @@ actual class LocalSource( } override suspend fun getSearchManga(page: Int, query: String, filters: FilterList): MangasPage = withIOContext { + if (page == 1) checkForNewManga() loadMangaForPage(page) while (page == currentlyLoadingPage) { @@ -297,7 +324,7 @@ actual class LocalSource( } } - includedManga = localManga.filter { manga -> + includedManga = localManga.flatten().filter { manga -> (manga.title.contains(query, ignoreCase = true) || File(manga.url).name.contains(query, ignoreCase = true)) && areAllElementsInMangaEntry(includedGenres, manga.genre) && areAllElementsInMangaEntry(includedAuthors, manga.author) && @@ -311,7 +338,7 @@ actual class LocalSource( includedArtists.isEmpty() && includedStatuses.isEmpty() ) { - includedManga = localManga.toMutableList() + includedManga = localManga.flatten().toMutableList() isFilteredSearch = false } else { isFilteredSearch = true @@ -625,13 +652,13 @@ actual class LocalSource( // Filters override fun getFilterList(): FilterList { - val genres = localManga.mapNotNull { it.genre?.split(",") } + val genres = localManga.flatten().mapNotNull { it.genre?.split(",") } .flatMap { it.map { genre -> genre.trim() } }.toSet() - val authors = localManga.mapNotNull { it.author?.split(",") } + val authors = localManga.flatten().mapNotNull { it.author?.split(",") } .flatMap { it.map { author -> author.trim() } }.toSet() - val artists = localManga.mapNotNull { it.artist?.split(",") } + val artists = localManga.flatten().mapNotNull { it.artist?.split(",") } .flatMap { it.map { artist -> artist.trim() } }.toSet() val filters = try {