Update settings

This commit is contained in:
Koitharu
2020-03-11 21:16:07 +02:00
parent b706e600c7
commit 0cefce17b5
11 changed files with 91 additions and 8 deletions

View File

@@ -2,5 +2,6 @@ package org.koitharu.kotatsu.core.local
enum class Cache(val dir: String) {
THUMBS("image_cache"),
PAGES("pages");
}

View File

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

View File

@@ -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<Preference>(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<Preference>(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<Preference>(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)
}
}

View File

@@ -11,7 +11,7 @@ class ReaderSettingsFragment : BasePreferenceFragment(R.string.reader_settings)
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.pref_reader)
findPreference<MultiSelectListPreference>(R.string.key_reader_switchers)?.let {
it.summaryProvider = MultiSummaryProvider()
it.summaryProvider = MultiSummaryProvider(R.string.gestures_only)
}
}
}

View File

@@ -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<MultiSelectListPreference> {
class MultiSummaryProvider(@StringRes private val emptySummaryId: Int) :
Preference.SummaryProvider<MultiSelectListPreference> {
@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)]

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="pages">
<item quantity="one">Всего %1$d страница</item>
<item quantity="few">Всего %1$d страницы</item>
<item quantity="many">Всего %1$d страниц</item>
</plurals>
<plurals name="items">
<item quantity="one">%1$d элемент</item>
<item quantity="few">%1$d элемента</item>
<item quantity="many">%1$d элементов</item>
</plurals>
</resources>

View File

@@ -95,4 +95,9 @@
<string name="dont_ask_again">Больше не спрашивать</string>
<string name="cancelling_">Отмена…</string>
<string name="error">Ошибка</string>
<string name="nothing">Ничего</string>
<string name="clear_thumbs_cache">Очистить кэш миниатюр</string>
<string name="clear_search_history">Очистить историю поиска</string>
<string name="search_history_cleared">История поиска очищена</string>
<string name="gestures_only">Только жесты</string>
</resources>

View File

@@ -5,6 +5,9 @@
<string name="key_sources_order">sources_order</string>
<string name="key_traffic_warning">traffic_warning</string>
<string name="key_pages_cache_clear">pages_cache_clear</string>
<string name="key_thumbs_cache_clear">thumbs_cache_clear</string>
<string name="key_search_history_clear">search_history_clear</string>
<string name="key_reading_history_clear">reading_history_clear</string>
<string name="key_grid_size">grid_size</string>
<string name="key_reader_switchers">reader_switchers</string>
<string-array name="values_theme">

View File

@@ -4,4 +4,8 @@
<item quantity="one">Total %1$d page</item>
<item quantity="other">Total %1$d pages</item>
</plurals>
<plurals name="items">
<item quantity="one">%1$d item</item>
<item quantity="other">%1$d items</item>
</plurals>
</resources>

View File

@@ -96,4 +96,9 @@
<string name="dont_ask_again">Don`t ask again</string>
<string name="cancelling_">Cancelling…</string>
<string name="error">Error</string>
<string name="nothing">Nothing</string>
<string name="clear_thumbs_cache">Clear thumbnails cache</string>
<string name="clear_search_history">Clear search history</string>
<string name="search_history_cleared">Search history cleared</string>
<string name="gestures_only">Gestures only</string>
</resources>

View File

@@ -3,10 +3,22 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Preference
android:key="@string/key_search_history_clear"
android:persistent="false"
android:title="@string/clear_search_history"
app:iconSpaceReserved="false" />
<PreferenceCategory
app:iconSpaceReserved="false"
android:title="@string/cache">
<Preference
android:key="@string/key_thumbs_cache_clear"
android:persistent="false"
android:title="@string/clear_thumbs_cache"
app:iconSpaceReserved="false" />
<Preference
android:key="@string/key_pages_cache_clear"
android:persistent="false"