From abd3d9c1ea3bfea30607f3a1704d2b3b4c9b5961 Mon Sep 17 00:00:00 2001 From: semenvav Date: Wed, 1 Nov 2023 18:09:41 +0200 Subject: [PATCH] use formatFileSize and getDirectorySize, compatibility fixes --- .../more/download/DownloadStatsScreenModel.kt | 10 +++--- .../download/components/DownloadStatDialog.kt | 6 ++-- .../components/DownloadstatsContent.kt | 13 +++---- .../more/download/tabs/OverAllStatsTab.kt | 8 ++--- .../data/download/DownloadManager.kt | 6 ++-- .../data/download/DownloadProvider.kt | 35 ------------------- .../tachiyomi/data/download/Downloader.kt | 2 +- i18n/src/main/res/values/strings.xml | 7 ---- 8 files changed, 23 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/more/download/DownloadStatsScreenModel.kt b/app/src/main/java/eu/kanade/presentation/more/download/DownloadStatsScreenModel.kt index b55dbf9d87..b9c0fc643d 100644 --- a/app/src/main/java/eu/kanade/presentation/more/download/DownloadStatsScreenModel.kt +++ b/app/src/main/java/eu/kanade/presentation/more/download/DownloadStatsScreenModel.kt @@ -13,11 +13,12 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.DownloadProvider import eu.kanade.tachiyomi.util.lang.toRelativeString -import eu.kanade.tachiyomi.util.preference.toggle +import eu.kanade.tachiyomi.util.storage.DiskUtil import kotlinx.coroutines.flow.update import kotlinx.coroutines.runBlocking import tachiyomi.core.preference.PreferenceStore import tachiyomi.core.preference.getEnum +import tachiyomi.core.preference.toggle import tachiyomi.core.util.lang.launchIO import tachiyomi.core.util.lang.launchNonCancellable import tachiyomi.domain.category.interactor.GetCategories @@ -30,6 +31,7 @@ import tachiyomi.domain.stat.interactor.GetDownloadStatOperations import tachiyomi.domain.stat.model.DownloadStatOperation import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import java.io.File import java.text.SimpleDateFormat import java.util.Date import java.util.Locale @@ -76,7 +78,7 @@ class DownloadStatsScreenModel( DownloadStatManga( libraryManga = libraryManga, source = source, - folderSize = downloadProvider.getFolderSize(path), + folderSize = DiskUtil.getDirectorySize(File(path)), downloadChaptersCount = downloadChaptersCount, category = categories[libraryManga.category]!!, ) @@ -399,7 +401,7 @@ class DownloadStatsScreenModel( coordinate = weight, subLine = Date(i.date).toRelativeString( context = context, - range = 0, + relative = false, ), dialog = Dialog.DownloadStatOperationInfo(i), ), @@ -412,7 +414,7 @@ class DownloadStatsScreenModel( weight += i.size val key = Date(i.date).toRelativeString( context = context, - range = 0, + relative = false, ) if (dateMap.containsKey(key)) { dateMap[key]?.add(i) diff --git a/app/src/main/java/eu/kanade/presentation/more/download/components/DownloadStatDialog.kt b/app/src/main/java/eu/kanade/presentation/more/download/components/DownloadStatDialog.kt index f33fd42d64..ecf3777b31 100644 --- a/app/src/main/java/eu/kanade/presentation/more/download/components/DownloadStatDialog.kt +++ b/app/src/main/java/eu/kanade/presentation/more/download/components/DownloadStatDialog.kt @@ -5,6 +5,7 @@ import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll @@ -31,7 +32,6 @@ import eu.kanade.tachiyomi.R import tachiyomi.domain.manga.model.Manga import tachiyomi.domain.stat.model.DownloadStatOperation import tachiyomi.presentation.core.components.CheckboxItem -import tachiyomi.presentation.core.components.LazyColumn import tachiyomi.presentation.core.components.RadioItem import tachiyomi.presentation.core.components.SortItem import tachiyomi.presentation.core.components.material.TextButton @@ -181,7 +181,7 @@ private fun MangaInfoColumn( stringResource(R.string.download_stat_operation_downloaded), downloadItems.sumOf { it.units }, folderSizeText( - folderSizeBytes = downloadItems.sumOf { it.size }, + folderSize = downloadItems.sumOf { it.size }, ), ), maxLines = 2, @@ -201,7 +201,7 @@ private fun MangaInfoColumn( stringResource(R.string.download_stat_operation_deleted), deleteItems.sumOf { it.units }, folderSizeText( - folderSizeBytes = abs(deleteItems.sumOf { it.size }), + folderSize = abs(deleteItems.sumOf { it.size }), ), ), maxLines = 2, diff --git a/app/src/main/java/eu/kanade/presentation/more/download/components/DownloadstatsContent.kt b/app/src/main/java/eu/kanade/presentation/more/download/components/DownloadstatsContent.kt index 775b33bf41..41ce557b73 100644 --- a/app/src/main/java/eu/kanade/presentation/more/download/components/DownloadstatsContent.kt +++ b/app/src/main/java/eu/kanade/presentation/more/download/components/DownloadstatsContent.kt @@ -1,5 +1,6 @@ package eu.kanade.presentation.more.download.components +import android.text.format.Formatter import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column @@ -34,6 +35,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.hapticfeedback.HapticFeedbackType +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.TextStyle @@ -59,8 +61,6 @@ import tachiyomi.presentation.core.components.material.TextButton import tachiyomi.presentation.core.components.material.padding import tachiyomi.presentation.core.util.selectedBackground import kotlin.math.abs -import kotlin.math.ln -import kotlin.math.pow @Composable fun CategoryList( @@ -413,12 +413,9 @@ fun DownloadedIndicator( } @Composable -fun folderSizeText(folderSizeBytes: Long): String { - val units = arrayOf(R.string.memory_unit_b, R.string.memory_unit_kb, R.string.memory_unit_mb, R.string.memory_unit_gb) - val base = 1024.0 - val exponent = (ln(folderSizeBytes.toDouble()) / ln(base)).toInt() - val size = folderSizeBytes / base.pow(exponent.toDouble()) - return if (exponent > 0) { String.format("%.2f %s", size, stringResource(units[exponent])) } else "0" +fun folderSizeText(folderSize: Long): String { + val context = LocalContext.current + return Formatter.formatFileSize(context, folderSize) } @Composable diff --git a/app/src/main/java/eu/kanade/presentation/more/download/tabs/OverAllStatsTab.kt b/app/src/main/java/eu/kanade/presentation/more/download/tabs/OverAllStatsTab.kt index d01e53fbf0..1218d21c79 100644 --- a/app/src/main/java/eu/kanade/presentation/more/download/tabs/OverAllStatsTab.kt +++ b/app/src/main/java/eu/kanade/presentation/more/download/tabs/OverAllStatsTab.kt @@ -1,6 +1,7 @@ package eu.kanade.presentation.more.download.tabs import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable @@ -16,7 +17,6 @@ import eu.kanade.presentation.more.download.components.DownloadStatsRow import eu.kanade.presentation.more.download.components.graphic.PieChartWithLegend import eu.kanade.presentation.more.download.components.graphic.PointGraph import eu.kanade.tachiyomi.R -import tachiyomi.presentation.core.components.LazyColumn import tachiyomi.presentation.core.components.material.padding @Composable @@ -27,10 +27,10 @@ fun overAllStatsTab( return TabContent( titleRes = R.string.label_download_stats_overall_tab, searchEnabled = false, - content = { PaddingValues, _ -> + content = { paddingValues, _ -> LazyColumn( state = rememberLazyListState(), - contentPadding = PaddingValues, + contentPadding = paddingValues, verticalArrangement = Arrangement.spacedBy(MaterialTheme.padding.small), ) { item { @@ -56,7 +56,7 @@ fun overAllStatsTab( defaultCategoryName = defaultCategoryName, ) }, - contentPadding = PaddingValues, + contentPadding = paddingValues, ) } item { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index 065b35ca25..d1f265cac5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -5,6 +5,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.model.Page +import eu.kanade.tachiyomi.util.storage.DiskUtil import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.drop @@ -26,6 +27,7 @@ import tachiyomi.domain.stat.interactor.AddDownloadStatOperation import tachiyomi.domain.stat.model.DownloadStatOperation import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import java.io.File /** * This class is used to manage chapter downloads in the application. It must be instantiated once @@ -229,7 +231,7 @@ class DownloadManager( addDownloadStatOperation.await( DownloadStatOperation.create().copy( mangaId = manga.id, - size = chapterDirs.sumOf { provider.getFolderSize(it.filePath!!) } * -1, + size = chapterDirs.sumOf { DiskUtil.getDirectorySize(File( it.filePath!!)) } * -1, units = filteredChapters.size.toLong(), ), @@ -257,7 +259,7 @@ class DownloadManager( downloader.removeFromQueue(manga) } val mangaDir = provider.findMangaDir(manga.title, source) - val dirSize = provider.getFolderSize(mangaDir?.filePath!!) + val dirSize = DiskUtil.getDirectorySize(File( mangaDir?.filePath!!)) if (dirSize > 0) { addDownloadStatOperation.await( DownloadStatOperation.create().copy( diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt index bdb7694fa2..2c0b796dfb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt @@ -16,7 +16,6 @@ import tachiyomi.domain.download.service.DownloadPreferences import tachiyomi.domain.manga.model.Manga import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get -import java.io.File /** * This class is used to provide the directories where the downloads should be saved. @@ -189,38 +188,4 @@ class DownloadProvider( } } } - - fun getFolderSize(path: String): Long { - val file = File(path) - var size: Long = 0 - - if (file.exists()) { - if (file.isDirectory) { - val files = file.listFiles() - if (files != null) { - for (childFile in files) { - size += if (childFile.isDirectory) { - getFolderSize(childFile.path) - } else { - getFileSize(childFile) - } - } - } - } else { - size = getFileSize(file) - } - } - - return size - } - - private fun getFileSize(file: File): Long { - return if (file.isDirectory) { - getFolderSize(file.path) - } else if (file.isFile) { - file.length() - } else { - 0 - } - } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index 0a46e91123..5f74b646c8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -407,7 +407,7 @@ class Downloader( addDownloadStatOperation.await( DownloadStatOperation.create().copy( mangaId = download.manga.id, - size = provider.getFolderSize(provider.findChapterDir(download.chapter.name, download.chapter.scanlator, download.manga.title, download.source)?.filePath!!), + size = DiskUtil.getDirectorySize(File(provider.findChapterDir(download.chapter.name, download.chapter.scanlator, download.manga.title, download.source)?.filePath!!)), ), ) diff --git a/i18n/src/main/res/values/strings.xml b/i18n/src/main/res/values/strings.xml index 16dbb29dae..f8ec0e098d 100644 --- a/i18n/src/main/res/values/strings.xml +++ b/i18n/src/main/res/values/strings.xml @@ -183,13 +183,6 @@ Disable group mode Start of recording, no information previously available - - B - KB - MB - GB - - Loading… InternalError: Check crash logs for further information