From a2a0e62b471e2ef38fb537f246544d4e890e1783 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 24 Feb 2020 18:48:37 +0200 Subject: [PATCH] Misc fixes --- .../org/koitharu/kotatsu/core/local/Cache.kt | 6 ++ .../koitharu/kotatsu/core/local/PagesCache.kt | 2 +- .../kotatsu/ui/main/list/MangaGridHolder.kt | 1 - .../ui/main/list/MangaListDetailsHolder.kt | 2 +- .../kotatsu/ui/main/list/MangaListHolder.kt | 1 - .../ui/settings/HistorySettingsFragment.kt | 57 +++++++++++++++++++ .../sources/SourcesSettingsFragment.kt | 12 ++++ .../org/koitharu/kotatsu/utils/CacheUtils.kt | 27 +++++++++ .../koitharu/kotatsu/utils/FileSizeUtils.kt | 25 ++++++++ .../kotatsu/utils/ext/CollectionExt.kt | 16 ++++++ .../org/koitharu/kotatsu/utils/ext/FileExt.kt | 10 +++- app/src/main/res/drawable/ic_placeholder.xml | 24 ++++---- .../main/res/drawable/ic_placeholder_raw.xml | 14 ----- app/src/main/res/drawable/ic_web.xml | 12 ++++ .../res/layout/fragment_settings_sources.xml | 6 ++ app/src/main/res/values/constants.xml | 1 + app/src/main/res/values/strings.xml | 4 ++ app/src/main/res/xml/pref_headers.xml | 10 ++++ app/src/main/res/xml/pref_history.xml | 18 ++++++ 19 files changed, 218 insertions(+), 30 deletions(-) create mode 100644 app/src/main/java/org/koitharu/kotatsu/core/local/Cache.kt create mode 100644 app/src/main/java/org/koitharu/kotatsu/ui/settings/HistorySettingsFragment.kt create mode 100644 app/src/main/java/org/koitharu/kotatsu/ui/settings/sources/SourcesSettingsFragment.kt create mode 100644 app/src/main/java/org/koitharu/kotatsu/utils/CacheUtils.kt delete mode 100644 app/src/main/res/drawable/ic_placeholder_raw.xml create mode 100644 app/src/main/res/drawable/ic_web.xml create mode 100644 app/src/main/res/layout/fragment_settings_sources.xml create mode 100644 app/src/main/res/xml/pref_history.xml diff --git a/app/src/main/java/org/koitharu/kotatsu/core/local/Cache.kt b/app/src/main/java/org/koitharu/kotatsu/core/local/Cache.kt new file mode 100644 index 000000000..d86c629fc --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/core/local/Cache.kt @@ -0,0 +1,6 @@ +package org.koitharu.kotatsu.core.local + +enum class Cache(val dir: String) { + + PAGES("pages"); +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/local/PagesCache.kt b/app/src/main/java/org/koitharu/kotatsu/core/local/PagesCache.kt index 0c3f510ed..63c842d2c 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/local/PagesCache.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/local/PagesCache.kt @@ -12,7 +12,7 @@ import java.io.OutputStream class PagesCache(context: Context) { private val cacheDir = context.externalCacheDir ?: context.cacheDir - private val lruCache = DiskLruCache.create(cacheDir.sub("pages"), FileSizeUtils.mbToBytes(200)) + private val lruCache = DiskLruCache.create(cacheDir.sub(Cache.PAGES.dir), FileSizeUtils.mbToBytes(200)) operator fun get(url: String): File? { return lruCache.get(url)?.takeIfReadable() diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/main/list/MangaGridHolder.kt b/app/src/main/java/org/koitharu/kotatsu/ui/main/list/MangaGridHolder.kt index 2f6bb7ba0..70856910d 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/main/list/MangaGridHolder.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/main/list/MangaGridHolder.kt @@ -20,7 +20,6 @@ class MangaGridHolder(parent: ViewGroup) : BaseViewHolder( placeholder(R.drawable.ic_placeholder) fallback(R.drawable.ic_placeholder) error(R.drawable.ic_placeholder) - crossfade(true) } } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/main/list/MangaListDetailsHolder.kt b/app/src/main/java/org/koitharu/kotatsu/ui/main/list/MangaListDetailsHolder.kt index 7e52d594c..f81e9fd3d 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/main/list/MangaListDetailsHolder.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/main/list/MangaListDetailsHolder.kt @@ -5,6 +5,7 @@ import android.view.ViewGroup import androidx.core.view.isVisible import coil.api.load import coil.request.RequestDisposable +import coil.size.Scale import kotlinx.android.synthetic.main.item_manga_list_details.* import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.model.Manga @@ -26,7 +27,6 @@ class MangaListDetailsHolder(parent: ViewGroup) : BaseViewHolder(R.string.key_pages_cache_clear)?.let { pref -> + lifecycleScope.launchWhenStarted { + val size = withContext(Dispatchers.IO) { + CacheUtils.computeCacheSize(pref.context, Cache.PAGES.dir) + } + pref.summary = FileSizeUtils.formatBytes(pref.context, size) + } + } + } + + override fun onPreferenceTreeClick(preference: Preference): Boolean { + return when(preference.key) { + getString(R.string.key_pages_cache_clear) -> { + clearCache(preference, Cache.PAGES) + true + } + else -> super.onPreferenceTreeClick(preference) + } + } + + private fun clearCache(preference: Preference, cache: Cache) { + val ctx = preference.context.applicationContext + lifecycleScope.launch { + try { + preference.isEnabled = false + val size = withContext(Dispatchers.IO) { + CacheUtils.clearCache(ctx, cache.dir) + CacheUtils.computeCacheSize(ctx, cache.dir) + } + preference.summary = FileSizeUtils.formatBytes(ctx, size) + } catch (e: Exception) { + preference.summary = e.getDisplayMessage(ctx.resources) + } finally { + preference.isEnabled = true + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/settings/sources/SourcesSettingsFragment.kt b/app/src/main/java/org/koitharu/kotatsu/ui/settings/sources/SourcesSettingsFragment.kt new file mode 100644 index 000000000..4ad9c7918 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/ui/settings/sources/SourcesSettingsFragment.kt @@ -0,0 +1,12 @@ +package org.koitharu.kotatsu.ui.settings.sources + +import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.ui.common.BaseFragment + +class SourcesSettingsFragment : BaseFragment(R.layout.fragment_settings_sources) { + + override fun onResume() { + super.onResume() + activity?.setTitle(R.string.remote_sources) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/CacheUtils.kt b/app/src/main/java/org/koitharu/kotatsu/utils/CacheUtils.kt new file mode 100644 index 000000000..840c7758c --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/utils/CacheUtils.kt @@ -0,0 +1,27 @@ +package org.koitharu.kotatsu.utils + +import android.content.Context +import androidx.annotation.WorkerThread +import org.koitharu.kotatsu.utils.ext.computeSize +import org.koitharu.kotatsu.utils.ext.sub +import org.koitharu.kotatsu.utils.ext.sumByLong + +object CacheUtils { + + @JvmStatic + fun getCacheDirs(context: Context) = (context.externalCacheDirs + context.cacheDir) + .filterNotNull() + .distinctBy { it.absolutePath } + + @JvmStatic + @WorkerThread + fun computeCacheSize(context: Context, name: String) = getCacheDirs(context) + .map { it.sub(name) } + .sumByLong { x -> x.computeSize() } + + @JvmStatic + @WorkerThread + fun clearCache(context: Context, name: String) = getCacheDirs(context) + .map { it.sub(name) } + .forEach { it.deleteRecursively() } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/FileSizeUtils.kt b/app/src/main/java/org/koitharu/kotatsu/utils/FileSizeUtils.kt index 9b73c486e..0f3dabfc7 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/FileSizeUtils.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/FileSizeUtils.kt @@ -1,5 +1,12 @@ package org.koitharu.kotatsu.utils +import android.content.Context +import org.koitharu.kotatsu.R +import java.text.DecimalFormat +import kotlin.math.log10 +import kotlin.math.pow + + object FileSizeUtils { @JvmStatic @@ -7,4 +14,22 @@ object FileSizeUtils { @JvmStatic fun kbToBytes(kb: Int) = 1024L * kb + + @JvmStatic + fun formatBytes(context: Context, bytes: Long): String { + val units = context.getString(R.string.text_file_sizes).split('|') + if (bytes <= 0) { + return "0 ${units.first()}" + } + val digitGroups = (log10(bytes.toDouble()) / log10(1024.0)).toInt() + return buildString { + append( + DecimalFormat("#,##0.#").format( + bytes / 1024.0.pow(digitGroups.toDouble()) + ).toString() + ) + append(' ') + append(units.getOrNull(digitGroups).orEmpty()) + } + } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ext/CollectionExt.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ext/CollectionExt.kt index cd6fd5c10..d52571cf1 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ext/CollectionExt.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/ext/CollectionExt.kt @@ -3,4 +3,20 @@ package org.koitharu.kotatsu.utils.ext fun MutableCollection.replaceWith(subject: Iterable) { clear() addAll(subject) +} + +inline fun Array.sumByLong(selector: (T) -> Long): Long { + var sum = 0L + for (element in this) { + sum += selector(element) + } + return sum +} + +inline fun Iterable.sumByLong(selector: (T) -> Long): Long { + var sum = 0L + for (element in this) { + sum += selector(element) + } + return sum } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ext/FileExt.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ext/FileExt.kt index 00c8daf6a..444737bba 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ext/FileExt.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/ext/FileExt.kt @@ -10,4 +10,12 @@ fun File.takeIfReadable() = takeIf { it.exists() && it.canRead() } fun ZipFile.readText(entry: ZipEntry) = getInputStream(entry).bufferedReader().use { it.readText() -} \ No newline at end of file +} + +fun File.computeSize(): Long = listFiles()?.sumByLong { x -> + if (x.isDirectory) { + x.computeSize() + } else { + x.length() + } +} ?: 0L \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_placeholder.xml b/app/src/main/res/drawable/ic_placeholder.xml index b6c98c64a..c7eb74389 100644 --- a/app/src/main/res/drawable/ic_placeholder.xml +++ b/app/src/main/res/drawable/ic_placeholder.xml @@ -1,11 +1,13 @@ - - - - \ No newline at end of file + + + + diff --git a/app/src/main/res/drawable/ic_placeholder_raw.xml b/app/src/main/res/drawable/ic_placeholder_raw.xml deleted file mode 100644 index c3652f8c0..000000000 --- a/app/src/main/res/drawable/ic_placeholder_raw.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - diff --git a/app/src/main/res/drawable/ic_web.xml b/app/src/main/res/drawable/ic_web.xml new file mode 100644 index 000000000..1a770a30c --- /dev/null +++ b/app/src/main/res/drawable/ic_web.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_settings_sources.xml b/app/src/main/res/layout/fragment_settings_sources.xml new file mode 100644 index 000000000..4b6b4a9b5 --- /dev/null +++ b/app/src/main/res/layout/fragment_settings_sources.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/constants.xml b/app/src/main/res/values/constants.xml index 7629a618e..bbddded3c 100644 --- a/app/src/main/res/values/constants.xml +++ b/app/src/main/res/values/constants.xml @@ -2,6 +2,7 @@ list_mode theme + pages_cache_clear -1 1 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 359619dea..64f0e1da6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -75,4 +75,8 @@ This operation is not supported Invalid file. Only ZIP and CBZ are supported. No description + History and cache + Clear pages cache + Cache + B|kB|MB|GB|TB \ No newline at end of file diff --git a/app/src/main/res/xml/pref_headers.xml b/app/src/main/res/xml/pref_headers.xml index 15ba1b36a..46933b843 100644 --- a/app/src/main/res/xml/pref_headers.xml +++ b/app/src/main/res/xml/pref_headers.xml @@ -8,4 +8,14 @@ android:icon="@drawable/ic_palette" android:title="@string/appearance" /> + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/pref_history.xml b/app/src/main/res/xml/pref_history.xml new file mode 100644 index 000000000..fa239b026 --- /dev/null +++ b/app/src/main/res/xml/pref_history.xml @@ -0,0 +1,18 @@ + + + + + + + + + + \ No newline at end of file