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)]