From 0cefce17b583f526f42883ae11dc3372973c6bc9 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 11 Mar 2020 21:16:07 +0200 Subject: [PATCH] Update settings --- .../org/koitharu/kotatsu/core/local/Cache.kt | 1 + .../ui/search/MangaSuggestionsProvider.kt | 20 ++++++++++++-- .../ui/settings/HistorySettingsFragment.kt | 26 ++++++++++++++++++- .../ui/settings/ReaderSettingsFragment.kt | 2 +- .../ui/settings/utils/MultiSummaryProvider.kt | 8 +++--- app/src/main/res/values-ru/plurals.xml | 13 ++++++++++ app/src/main/res/values-ru/strings.xml | 5 ++++ app/src/main/res/values/constants.xml | 3 +++ app/src/main/res/values/plurals.xml | 4 +++ app/src/main/res/values/strings.xml | 5 ++++ app/src/main/res/xml/pref_history.xml | 12 +++++++++ 11 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/values-ru/plurals.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 index d86c629fc..1a16d23fd 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/local/Cache.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/local/Cache.kt @@ -2,5 +2,6 @@ package org.koitharu.kotatsu.core.local enum class Cache(val dir: String) { + THUMBS("image_cache"), PAGES("pages"); } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/search/MangaSuggestionsProvider.kt b/app/src/main/java/org/koitharu/kotatsu/ui/search/MangaSuggestionsProvider.kt index 5d1bd3ca9..ffa8f0445 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/search/MangaSuggestionsProvider.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/search/MangaSuggestionsProvider.kt @@ -50,13 +50,17 @@ class MangaSuggestionsProvider : SearchRecentSuggestionsProvider() { private const val AUTHORITY = "${BuildConfig.APPLICATION_ID}.MangaSuggestionsProvider" private const val MODE = DATABASE_MODE_QUERIES + @JvmStatic private val uri = Uri.Builder() .scheme(ContentResolver.SCHEME_CONTENT) .authority(AUTHORITY) .appendPath(SearchManager.SUGGEST_URI_PATH_QUERY) .build() - private val projection = arrayOf("_id", SearchManager.SUGGEST_COLUMN_QUERY) + @JvmStatic + private val projection = arrayOf("_id", SearchManager.SUGGEST_COLUMN_QUERY) + + @JvmStatic fun saveQuery(context: Context, query: String) { SearchRecentSuggestions( context, @@ -65,6 +69,7 @@ class MangaSuggestionsProvider : SearchRecentSuggestionsProvider() { ).saveRecentQuery(query, null) } + @JvmStatic fun clearHistory(context: Context) { SearchRecentSuggestions( context, @@ -73,16 +78,27 @@ class MangaSuggestionsProvider : SearchRecentSuggestionsProvider() { ).clearHistory() } + @JvmStatic + fun getItemsCount(context: Context) = getCursor(context)?.count ?: 0 + + @JvmStatic private fun getCursor(context: Context): Cursor? { return context.contentResolver?.query(uri, projection, null, arrayOf(""), null) } + @JvmStatic fun getSuggestionAdapter(context: Context): CursorAdapter? = getCursor( context )?.let { cursor -> SearchSuggestionAdapter(context, cursor).also { it.setFilterQueryProvider { q -> - context.contentResolver?.query(uri, projection, " ?", arrayOf(q.toString()), null) + context.contentResolver?.query( + uri, + projection, + " ?", + arrayOf(q.toString()), + null + ) } } } diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/settings/HistorySettingsFragment.kt b/app/src/main/java/org/koitharu/kotatsu/ui/settings/HistorySettingsFragment.kt index 3150d0acd..c1f436edc 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/settings/HistorySettingsFragment.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/settings/HistorySettingsFragment.kt @@ -3,12 +3,14 @@ package org.koitharu.kotatsu.ui.settings import android.os.Bundle import androidx.lifecycle.lifecycleScope import androidx.preference.Preference +import com.google.android.material.snackbar.Snackbar import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.local.Cache import org.koitharu.kotatsu.ui.common.BasePreferenceFragment +import org.koitharu.kotatsu.ui.search.MangaSuggestionsProvider import org.koitharu.kotatsu.utils.CacheUtils import org.koitharu.kotatsu.utils.FileSizeUtils import org.koitharu.kotatsu.utils.ext.getDisplayMessage @@ -18,13 +20,25 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_history) findPreference(R.string.key_pages_cache_clear)?.let { pref -> - lifecycleScope.launchWhenStarted { + lifecycleScope.launchWhenResumed { val size = withContext(Dispatchers.IO) { CacheUtils.computeCacheSize(pref.context, Cache.PAGES.dir) } pref.summary = FileSizeUtils.formatBytes(pref.context, size) } } + findPreference(R.string.key_thumbs_cache_clear)?.let { pref -> + lifecycleScope.launchWhenResumed { + val size = withContext(Dispatchers.IO) { + CacheUtils.computeCacheSize(pref.context, Cache.THUMBS.dir) + } + pref.summary = FileSizeUtils.formatBytes(pref.context, size) + } + } + findPreference(R.string.key_search_history_clear)?.let { p -> + val items = MangaSuggestionsProvider.getItemsCount(p.context) + p.summary = p.context.resources.getQuantityString(R.plurals.items, items, items) + } } override fun onPreferenceTreeClick(preference: Preference): Boolean { @@ -33,6 +47,16 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach clearCache(preference, Cache.PAGES) true } + getString(R.string.key_thumbs_cache_clear) -> { + clearCache(preference, Cache.THUMBS) + true + } + getString(R.string.key_search_history_clear) -> { + MangaSuggestionsProvider.clearHistory(preference.context) + preference.context.resources.getQuantityString(R.plurals.items, 0, 0) + Snackbar.make(view ?: return true, R.string.search_history_cleared, Snackbar.LENGTH_SHORT).show() + true + } else -> super.onPreferenceTreeClick(preference) } } diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/settings/ReaderSettingsFragment.kt b/app/src/main/java/org/koitharu/kotatsu/ui/settings/ReaderSettingsFragment.kt index db8a7a58c..a08633075 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/settings/ReaderSettingsFragment.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/settings/ReaderSettingsFragment.kt @@ -11,7 +11,7 @@ class ReaderSettingsFragment : BasePreferenceFragment(R.string.reader_settings) override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_reader) findPreference(R.string.key_reader_switchers)?.let { - it.summaryProvider = MultiSummaryProvider() + it.summaryProvider = MultiSummaryProvider(R.string.gestures_only) } } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/settings/utils/MultiSummaryProvider.kt b/app/src/main/java/org/koitharu/kotatsu/ui/settings/utils/MultiSummaryProvider.kt index 02f23f312..0705820e7 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/settings/utils/MultiSummaryProvider.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/settings/utils/MultiSummaryProvider.kt @@ -1,16 +1,16 @@ package org.koitharu.kotatsu.ui.settings.utils -import android.annotation.SuppressLint +import androidx.annotation.StringRes import androidx.preference.MultiSelectListPreference import androidx.preference.Preference -class MultiSummaryProvider : Preference.SummaryProvider { +class MultiSummaryProvider(@StringRes private val emptySummaryId: Int) : + Preference.SummaryProvider { - @SuppressLint("PrivateResource") override fun provideSummary(preference: MultiSelectListPreference): CharSequence { val values = preference.values return if (values.isEmpty()) { - return preference.context.getString(androidx.preference.R.string.not_set) + return preference.context.getString(emptySummaryId) } else { values.joinToString(", ") { preference.entries[preference.findIndexOfValue(it)] diff --git a/app/src/main/res/values-ru/plurals.xml b/app/src/main/res/values-ru/plurals.xml new file mode 100644 index 000000000..54caaddb6 --- /dev/null +++ b/app/src/main/res/values-ru/plurals.xml @@ -0,0 +1,13 @@ + + + + Всего %1$d страница + Всего %1$d страницы + Всего %1$d страниц + + + %1$d элемент + %1$d элемента + %1$d элементов + + \ No newline at end of file diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 8aff71eac..cca0155f7 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -95,4 +95,9 @@ Больше не спрашивать Отмена… Ошибка + Ничего + Очистить кэш миниатюр + Очистить историю поиска + История поиска очищена + Только жесты \ 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 4da0270d3..25bce7faa 100644 --- a/app/src/main/res/values/constants.xml +++ b/app/src/main/res/values/constants.xml @@ -5,6 +5,9 @@ sources_order traffic_warning pages_cache_clear + thumbs_cache_clear + search_history_clear + reading_history_clear grid_size reader_switchers diff --git a/app/src/main/res/values/plurals.xml b/app/src/main/res/values/plurals.xml index b4a4af465..4ae01551a 100644 --- a/app/src/main/res/values/plurals.xml +++ b/app/src/main/res/values/plurals.xml @@ -4,4 +4,8 @@ Total %1$d page Total %1$d pages + + %1$d item + %1$d items + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7c6114985..fea03487f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -96,4 +96,9 @@ Don`t ask again Cancelling… Error + Nothing + Clear thumbnails cache + Clear search history + Search history cleared + Gestures only \ 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 index fa239b026..f26371f0c 100644 --- a/app/src/main/res/xml/pref_history.xml +++ b/app/src/main/res/xml/pref_history.xml @@ -3,10 +3,22 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> + + + +