From e012f220d887473a7f0b7ec045d19e03bfa313e0 Mon Sep 17 00:00:00 2001 From: jmir1 Date: Thu, 2 Nov 2023 23:19:54 +0100 Subject: [PATCH] fix(storage stats): Don't render arc for categories with 0 size + calculate local source sizes --- .../more/storage/CumulativeStorage.kt | 6 ++++- .../download/anime/AnimeDownloadManager.kt | 24 +++++++++++++++---- .../download/manga/MangaDownloadManager.kt | 24 +++++++++++++++---- .../ui/storage/CommonStorageScreenModel.kt | 4 +--- .../storage/anime/AnimeStorageScreenModel.kt | 1 - .../storage/manga/MangaStorageScreenModel.kt | 1 - 6 files changed, 46 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/more/storage/CumulativeStorage.kt b/app/src/main/java/eu/kanade/presentation/more/storage/CumulativeStorage.kt index a68112ee7..40f51001f 100644 --- a/app/src/main/java/eu/kanade/presentation/more/storage/CumulativeStorage.kt +++ b/app/src/main/java/eu/kanade/presentation/more/storage/CumulativeStorage.kt @@ -35,7 +35,11 @@ fun CumulativeStorage( var currentAngle = 0f rotate(180f) { for (item in items) { - val itemAngle = (item.size / totalSize) * totalAngle + val itemAngle = if (totalSize > 0f) { + (item.size / totalSize) * totalAngle + } else { + 0f + } drawArc( color = item.color, startAngle = currentAngle, diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/anime/AnimeDownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/anime/AnimeDownloadManager.kt index 915c43bcf..dc35e58d8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/anime/AnimeDownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/anime/AnimeDownloadManager.kt @@ -1,10 +1,12 @@ package eu.kanade.tachiyomi.data.download.anime import android.content.Context +import com.hippo.unifile.UniFile import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.animesource.AnimeSource import eu.kanade.tachiyomi.animesource.model.Video import eu.kanade.tachiyomi.data.download.anime.model.AnimeDownload +import eu.kanade.tachiyomi.util.size import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.drop @@ -23,6 +25,9 @@ import tachiyomi.domain.download.service.DownloadPreferences import tachiyomi.domain.entries.anime.model.Anime import tachiyomi.domain.items.episode.model.Episode import tachiyomi.domain.source.anime.service.AnimeSourceManager +import tachiyomi.source.local.entries.anime.LocalAnimeSource +import tachiyomi.source.local.io.ArchiveAnime +import tachiyomi.source.local.io.anime.LocalAnimeSourceFileSystem import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -214,12 +219,18 @@ class AnimeDownloadManager( } /** - * Returns the amount of downloaded episodes for an anime. + * Returns the amount of downloaded/local episodes for an anime. * * @param anime the anime to check. */ fun getDownloadCount(anime: Anime): Int { - return cache.getDownloadCount(anime) + return if (anime.source == LocalAnimeSource.ID) { + LocalAnimeSourceFileSystem(context).getFilesInAnimeDirectory(anime.url) + .filter { ArchiveAnime.isSupported(it) } + .count() + } else { + cache.getDownloadCount(anime) + } } /** @@ -230,12 +241,17 @@ class AnimeDownloadManager( } /** - * Returns the size of downloaded episodes for an anime. + * Returns the size of downloaded/local episodes for an anime. * * @param anime the anime to check. */ fun getDownloadSize(anime: Anime): Long { - return cache.getDownloadSize(anime) + return if (anime.source == LocalAnimeSource.ID) { + LocalAnimeSourceFileSystem(context).getAnimeDirectory(anime.url) + .let { UniFile.fromFile(it) }?.size() ?: 0L + } else { + cache.getDownloadSize(anime) + } } fun cancelQueuedDownloads(downloads: List) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/manga/MangaDownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/manga/MangaDownloadManager.kt index daaff8bcf..b5eb1e3cf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/manga/MangaDownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/manga/MangaDownloadManager.kt @@ -1,10 +1,12 @@ package eu.kanade.tachiyomi.data.download.manga import android.content.Context +import com.hippo.unifile.UniFile import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.download.manga.model.MangaDownload import eu.kanade.tachiyomi.source.MangaSource import eu.kanade.tachiyomi.source.model.Page +import eu.kanade.tachiyomi.util.size import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.drop @@ -22,6 +24,9 @@ import tachiyomi.domain.download.service.DownloadPreferences import tachiyomi.domain.entries.manga.model.Manga import tachiyomi.domain.items.chapter.model.Chapter import tachiyomi.domain.source.manga.service.MangaSourceManager +import tachiyomi.source.local.entries.manga.LocalMangaSource +import tachiyomi.source.local.io.ArchiveManga +import tachiyomi.source.local.io.manga.LocalMangaSourceFileSystem import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -193,12 +198,18 @@ class MangaDownloadManager( } /** - * Returns the amount of downloaded chapters for a manga. + * Returns the amount of downloaded/local chapters for a manga. * * @param manga the manga to check. */ fun getDownloadCount(manga: Manga): Int { - return cache.getDownloadCount(manga) + return if (manga.source == LocalMangaSource.ID) { + LocalMangaSourceFileSystem(context).getFilesInMangaDirectory(manga.url) + .filter { it.isDirectory || ArchiveManga.isSupported(it) } + .count() + } else { + cache.getDownloadCount(manga) + } } /** @@ -209,12 +220,17 @@ class MangaDownloadManager( } /** - * Returns the size of downloaded chapters for a manga. + * Returns the size of downloaded/local episodes for an manga. * * @param manga the manga to check. */ fun getDownloadSize(manga: Manga): Long { - return cache.getDownloadSize(manga) + return if (manga.source == LocalMangaSource.ID) { + LocalMangaSourceFileSystem(context).getMangaDirectory(manga.url) + .let { UniFile.fromFile(it) }?.size() ?: 0L + } else { + cache.getDownloadSize(manga) + } } fun cancelQueuedDownloads(downloads: List) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/storage/CommonStorageScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/storage/CommonStorageScreenModel.kt index cdccee98f..e6c3af31d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/storage/CommonStorageScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/storage/CommonStorageScreenModel.kt @@ -22,7 +22,6 @@ abstract class CommonStorageScreenModel( private val downloadCacheIsInitializing: StateFlow, private val libraries: Flow>, private val categories: Flow>, - private val getTotalDownloadSize: () -> Long, private val getDownloadSize: T.() -> Long, private val getDownloadCount: T.() -> Int, private val getId: T.() -> Long, @@ -47,14 +46,13 @@ abstract class CommonStorageScreenModel( }.filter { selectedCategory == AllCategory || it.getCategoryId() == selectedCategory.id } - val size = getTotalDownloadSize() - val random = Random(size + distinctLibraries.size) mutableState.update { StorageScreenState.Success( selectedCategory = selectedCategory, categories = listOf(AllCategory, *categories.toTypedArray()), items = distinctLibraries.map { + val random = Random(it.getId()) StorageItem( id = it.getId(), title = it.getTitle(), diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/storage/anime/AnimeStorageScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/storage/anime/AnimeStorageScreenModel.kt index 32bb03e48..80ab6206e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/storage/anime/AnimeStorageScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/storage/anime/AnimeStorageScreenModel.kt @@ -23,7 +23,6 @@ class AnimeStorageScreenModel( downloadCacheIsInitializing = downloadCache.isInitializing, libraries = getLibraries.subscribe(), categories = getVisibleCategories.subscribe(), - getTotalDownloadSize = { downloadManager.getDownloadSize() }, getDownloadSize = { downloadManager.getDownloadSize(anime) }, getDownloadCount = { downloadManager.getDownloadCount(anime) }, getId = { id }, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/storage/manga/MangaStorageScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/storage/manga/MangaStorageScreenModel.kt index f9358e7eb..4f97095b3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/storage/manga/MangaStorageScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/storage/manga/MangaStorageScreenModel.kt @@ -23,7 +23,6 @@ class MangaStorageScreenModel( downloadCacheIsInitializing = downloadCache.isInitializing, libraries = getLibraries.subscribe(), categories = getVisibleCategories.subscribe(), - getTotalDownloadSize = { downloadManager.getDownloadSize() }, getDownloadSize = { downloadManager.getDownloadSize(manga) }, getDownloadCount = { downloadManager.getDownloadCount(manga) }, getId = { id },