use formatFileSize and getDirectorySize, compatibility fixes

This commit is contained in:
semenvav 2023-11-01 18:09:41 +02:00
parent 7f777e5c1a
commit abd3d9c1ea
8 changed files with 23 additions and 64 deletions

View File

@ -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)

View File

@ -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,

View File

@ -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

View File

@ -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 {

View File

@ -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(

View File

@ -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
}
}
}

View File

@ -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!!)),
),
)

View File

@ -183,13 +183,6 @@
<string name="disable_stat_graph_grouping">Disable group mode</string>
<string name="start_operation_massage">Start of recording, no information previously available</string>
<!-- Memory units -->
<string name="memory_unit_b">B</string>
<string name="memory_unit_kb">KB</string>
<string name="memory_unit_mb">MB</string>
<string name="memory_unit_gb">GB</string>
<!-- Operations -->
<string name="loading">Loading…</string>
<string name="internal_error">InternalError: Check crash logs for further information</string>