Fix local manga size

pull/112/head
Koitharu 4 years ago
parent ed4c470bdc
commit 3c0c4ce9c0
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -2,6 +2,7 @@ package org.koitharu.kotatsu.core.ui
import coil.ComponentRegistry import coil.ComponentRegistry
import coil.ImageLoader import coil.ImageLoader
import coil.util.CoilUtils
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import org.koin.android.ext.koin.androidContext import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module import org.koin.dsl.module
@ -11,8 +12,11 @@ import org.koitharu.kotatsu.local.data.CbzFetcher
val uiModule val uiModule
get() = module { get() = module {
single { single {
val httpClient = get<OkHttpClient>().newBuilder()
.cache(CoilUtils.createDefaultCache(androidContext()))
.build()
ImageLoader.Builder(androidContext()) ImageLoader.Builder(androidContext())
.okHttpClient(get<OkHttpClient>()) .okHttpClient(httpClient)
.componentRegistry( .componentRegistry(
ComponentRegistry.Builder() ComponentRegistry.Builder()
.add(CbzFetcher()) .add(CbzFetcher())

@ -6,6 +6,7 @@ import android.os.StatFs
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runInterruptible import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.withContext
import okhttp3.Cache import okhttp3.Cache
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.utils.ext.computeSize import org.koitharu.kotatsu.utils.ext.computeSize
@ -32,7 +33,7 @@ class LocalStorageManager(
return Cache(directory, maxSize) return Cache(directory, maxSize)
} }
suspend fun computeCacheSize(cache: CacheDir) = runInterruptible(Dispatchers.IO) { suspend fun computeCacheSize(cache: CacheDir) = withContext(Dispatchers.IO) {
getCacheDirs(cache.dir).sumOf { it.computeSize() } getCacheDirs(cache.dir).sumOf { it.computeSize() }
} }
@ -86,7 +87,9 @@ class LocalStorageManager(
private fun getCacheDirs(subDir: String): MutableSet<File> { private fun getCacheDirs(subDir: String): MutableSet<File> {
val result = LinkedHashSet<File>() val result = LinkedHashSet<File>()
result += File(context.cacheDir, subDir) result += File(context.cacheDir, subDir)
result += context.getExternalFilesDirs(subDir) context.externalCacheDirs.mapTo(result) {
File(it, subDir)
}
return result return result
} }

@ -32,18 +32,8 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
findPreference<Preference>(AppSettings.KEY_PAGES_CACHE_CLEAR)?.let { pref -> findPreference<Preference>(AppSettings.KEY_PAGES_CACHE_CLEAR)?.bindSummaryToCacheSize(CacheDir.PAGES)
viewLifecycleScope.launchWhenResumed { findPreference<Preference>(AppSettings.KEY_THUMBS_CACHE_CLEAR)?.bindSummaryToCacheSize(CacheDir.THUMBS)
val size = storageManager.computeCacheSize(CacheDir.PAGES)
pref.summary = FileSize.BYTES.format(pref.context, size)
}
}
findPreference<Preference>(AppSettings.KEY_THUMBS_CACHE_CLEAR)?.let { pref ->
viewLifecycleScope.launchWhenResumed {
val size = storageManager.computeCacheSize(CacheDir.THUMBS)
pref.summary = FileSize.BYTES.format(pref.context, size)
}
}
findPreference<Preference>(AppSettings.KEY_SEARCH_HISTORY_CLEAR)?.let { pref -> findPreference<Preference>(AppSettings.KEY_SEARCH_HISTORY_CLEAR)?.let { pref ->
viewLifecycleScope.launchWhenResumed { viewLifecycleScope.launchWhenResumed {
val items = searchRepository.getSearchHistoryCount() val items = searchRepository.getSearchHistoryCount()
@ -111,6 +101,11 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
} }
} }
private fun Preference.bindSummaryToCacheSize(dir: CacheDir) = viewLifecycleScope.launch {
val size = storageManager.computeCacheSize(dir)
summary = FileSize.BYTES.format(context, size)
}
private fun clearSearchHistory(preference: Preference) { private fun clearSearchHistory(preference: Preference) {
MaterialAlertDialogBuilder(context ?: return) MaterialAlertDialogBuilder(context ?: return)
.setTitle(R.string.clear_search_history) .setTitle(R.string.clear_search_history)

@ -7,8 +7,10 @@ import android.os.Build
import android.os.Environment import android.os.Environment
import android.os.storage.StorageManager import android.os.storage.StorageManager
import android.provider.OpenableColumns import android.provider.OpenableColumns
import androidx.annotation.WorkerThread
import androidx.core.database.getStringOrNull import androidx.core.database.getStringOrNull
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import java.io.File import java.io.File
@ -25,14 +27,6 @@ fun ZipFile.readText(entry: ZipEntry) = getInputStream(entry).bufferedReader().u
it.readText() it.readText()
} }
fun File.computeSize(): Long = listFiles()?.sumOf { x ->
if (x.isDirectory) {
x.computeSize()
} else {
x.length()
}
} ?: 0L
fun File.getStorageName(context: Context): String = runCatching { fun File.getStorageName(context: Context): String = runCatching {
val manager = context.getSystemService(Context.STORAGE_SERVICE) as StorageManager val manager = context.getSystemService(Context.STORAGE_SERVICE) as StorageManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@ -66,4 +60,18 @@ fun ContentResolver.resolveName(uri: Uri): String? {
} }
} }
return fallback return fallback
}
suspend fun File.computeSize(): Long = runInterruptible(Dispatchers.IO) {
computeSizeInternal(this)
}
@WorkerThread
private fun computeSizeInternal(file: File): Long {
if (file.isDirectory) {
val files = file.listFiles() ?: return 0L
return files.sumOf { computeSizeInternal(it) }
} else {
return file.length()
}
} }

@ -16,6 +16,7 @@
<string name="settings">Settings</string> <string name="settings">Settings</string>
<string name="remote_sources">Remote sources</string> <string name="remote_sources">Remote sources</string>
<string name="loading_">Loading…</string> <string name="loading_">Loading…</string>
<string name="computing_">Computing…</string>
<string name="chapter_d_of_d">Chapter %1$d of %2$d</string> <string name="chapter_d_of_d">Chapter %1$d of %2$d</string>
<string name="close">Close</string> <string name="close">Close</string>
<string name="try_again">Try again</string> <string name="try_again">Try again</string>

@ -6,12 +6,14 @@
<Preference <Preference
android:key="search_history_clear" android:key="search_history_clear"
android:persistent="false" android:persistent="false"
android:summary="@string/loading_"
android:title="@string/clear_search_history" android:title="@string/clear_search_history"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<Preference <Preference
android:key="updates_feed_clear" android:key="updates_feed_clear"
android:persistent="false" android:persistent="false"
android:summary="@string/loading_"
android:title="@string/clear_updates_feed" android:title="@string/clear_updates_feed"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
@ -27,12 +29,14 @@
<Preference <Preference
android:key="thumbs_cache_clear" android:key="thumbs_cache_clear"
android:persistent="false" android:persistent="false"
android:summary="@string/computing_"
android:title="@string/clear_thumbs_cache" android:title="@string/clear_thumbs_cache"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<Preference <Preference
android:key="pages_cache_clear" android:key="pages_cache_clear"
android:persistent="false" android:persistent="false"
android:summary="@string/computing_"
android:title="@string/clear_pages_cache" android:title="@string/clear_pages_cache"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />

Loading…
Cancel
Save