Skip to content

Commit

Permalink
fix(storage stats): Don't render arc for categories with 0 size + cal…
Browse files Browse the repository at this point in the history
…culate local source sizes
  • Loading branch information
jmir1 committed Nov 2, 2023
1 parent efd4213 commit e012f22
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
}
}

/**
Expand All @@ -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<AnimeDownload>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
}
}

/**
Expand All @@ -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<MangaDownload>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ abstract class CommonStorageScreenModel<T>(
private val downloadCacheIsInitializing: StateFlow<Boolean>,
private val libraries: Flow<List<T>>,
private val categories: Flow<List<Category>>,
private val getTotalDownloadSize: () -> Long,
private val getDownloadSize: T.() -> Long,
private val getDownloadCount: T.() -> Int,
private val getId: T.() -> Long,
Expand All @@ -47,14 +46,13 @@ abstract class CommonStorageScreenModel<T>(
}.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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit e012f22

Please sign in to comment.