Fix local manga size
This commit is contained in:
@@ -2,6 +2,7 @@ package org.koitharu.kotatsu.core.ui
|
||||
|
||||
import coil.ComponentRegistry
|
||||
import coil.ImageLoader
|
||||
import coil.util.CoilUtils
|
||||
import okhttp3.OkHttpClient
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.dsl.module
|
||||
@@ -11,8 +12,11 @@ import org.koitharu.kotatsu.local.data.CbzFetcher
|
||||
val uiModule
|
||||
get() = module {
|
||||
single {
|
||||
val httpClient = get<OkHttpClient>().newBuilder()
|
||||
.cache(CoilUtils.createDefaultCache(androidContext()))
|
||||
.build()
|
||||
ImageLoader.Builder(androidContext())
|
||||
.okHttpClient(get<OkHttpClient>())
|
||||
.okHttpClient(httpClient)
|
||||
.componentRegistry(
|
||||
ComponentRegistry.Builder()
|
||||
.add(CbzFetcher())
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.os.StatFs
|
||||
import androidx.annotation.WorkerThread
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runInterruptible
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.Cache
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.utils.ext.computeSize
|
||||
@@ -32,7 +33,7 @@ class LocalStorageManager(
|
||||
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() }
|
||||
}
|
||||
|
||||
@@ -86,7 +87,9 @@ class LocalStorageManager(
|
||||
private fun getCacheDirs(subDir: String): MutableSet<File> {
|
||||
val result = LinkedHashSet<File>()
|
||||
result += File(context.cacheDir, subDir)
|
||||
result += context.getExternalFilesDirs(subDir)
|
||||
context.externalCacheDirs.mapTo(result) {
|
||||
File(it, subDir)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
@@ -32,18 +32,8 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
findPreference<Preference>(AppSettings.KEY_PAGES_CACHE_CLEAR)?.let { pref ->
|
||||
viewLifecycleScope.launchWhenResumed {
|
||||
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_PAGES_CACHE_CLEAR)?.bindSummaryToCacheSize(CacheDir.PAGES)
|
||||
findPreference<Preference>(AppSettings.KEY_THUMBS_CACHE_CLEAR)?.bindSummaryToCacheSize(CacheDir.THUMBS)
|
||||
findPreference<Preference>(AppSettings.KEY_SEARCH_HISTORY_CLEAR)?.let { pref ->
|
||||
viewLifecycleScope.launchWhenResumed {
|
||||
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) {
|
||||
MaterialAlertDialogBuilder(context ?: return)
|
||||
.setTitle(R.string.clear_search_history)
|
||||
|
||||
@@ -7,8 +7,10 @@ import android.os.Build
|
||||
import android.os.Environment
|
||||
import android.os.storage.StorageManager
|
||||
import android.provider.OpenableColumns
|
||||
import androidx.annotation.WorkerThread
|
||||
import androidx.core.database.getStringOrNull
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runInterruptible
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.koitharu.kotatsu.R
|
||||
import java.io.File
|
||||
@@ -25,14 +27,6 @@ fun ZipFile.readText(entry: ZipEntry) = getInputStream(entry).bufferedReader().u
|
||||
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 {
|
||||
val manager = context.getSystemService(Context.STORAGE_SERVICE) as StorageManager
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
@@ -66,4 +60,18 @@ fun ContentResolver.resolveName(uri: Uri): String? {
|
||||
}
|
||||
}
|
||||
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="remote_sources">Remote sources</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="close">Close</string>
|
||||
<string name="try_again">Try again</string>
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
<Preference
|
||||
android:key="search_history_clear"
|
||||
android:persistent="false"
|
||||
android:summary="@string/loading_"
|
||||
android:title="@string/clear_search_history"
|
||||
app:iconSpaceReserved="false" />
|
||||
|
||||
<Preference
|
||||
android:key="updates_feed_clear"
|
||||
android:persistent="false"
|
||||
android:summary="@string/loading_"
|
||||
android:title="@string/clear_updates_feed"
|
||||
app:iconSpaceReserved="false" />
|
||||
|
||||
@@ -27,12 +29,14 @@
|
||||
<Preference
|
||||
android:key="thumbs_cache_clear"
|
||||
android:persistent="false"
|
||||
android:summary="@string/computing_"
|
||||
android:title="@string/clear_thumbs_cache"
|
||||
app:iconSpaceReserved="false" />
|
||||
|
||||
<Preference
|
||||
android:key="pages_cache_clear"
|
||||
android:persistent="false"
|
||||
android:summary="@string/computing_"
|
||||
android:title="@string/clear_pages_cache"
|
||||
app:iconSpaceReserved="false" />
|
||||
|
||||
|
||||
Reference in New Issue
Block a user