Made settings use material dialog box

Even the list preference in extensions details
...this was a nightmare
This commit is contained in:
Jay 2020-01-14 02:33:47 -08:00
parent fc734a980f
commit 43e801130e
13 changed files with 318 additions and 85 deletions

View File

@ -31,6 +31,10 @@ class PreferencesHelper(val context: Context) {
File(Environment.getExternalStorageDirectory().absolutePath + File.separator +
context.getString(R.string.app_name), "backup"))
fun getInt(key: String, default: Int?) = rxPrefs.getInteger(key, default)
fun getStringPref(key: String, default: String?) = rxPrefs.getString(key, default)
fun getStringSet(key: String, default: Set<String>) = rxPrefs.getStringSet(key, default)
fun startScreen() = prefs.getInt(Keys.startScreen, 1)
fun clear() = prefs.edit().clear().apply()

View File

@ -1,6 +1,7 @@
package eu.kanade.tachiyomi.ui.extension
import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.TypedValue
@ -31,6 +32,7 @@ import eu.kanade.tachiyomi.ui.base.controller.NucleusController
import eu.kanade.tachiyomi.ui.setting.preferenceCategory
import eu.kanade.tachiyomi.util.LocaleHelper
import eu.kanade.tachiyomi.util.RecyclerWindowInsetsListener
import eu.kanade.tachiyomi.widget.preference.ListMatPreference
import eu.kanade.tachiyomi.widget.preference.LoginPreference
import eu.kanade.tachiyomi.widget.preference.SourceLoginDialog
import kotlinx.android.synthetic.main.extension_detail_controller.*
@ -154,6 +156,7 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
while (newScreen.preferenceCount != 0) {
val pref = newScreen.getPreference(0)
pref.preferenceDataStore = dataStore
pref.fragment = "source_${source.id}"
pref.order = Int.MAX_VALUE // reset to default order
newScreen.removePreference(pref)
@ -177,6 +180,18 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
screen.getPreference(it) === preference
}
if (preference is ListPreference) {
ListMatPreference(preference.context).apply {
key = preference.key
sharedPref = preference.fragment
otherPref = preference
preferenceDataStore = preference.preferenceDataStore
entries = preference.entries.mapNotNull { it.toString() }
entryValues = preference.entryValues.mapNotNull { it.toString() }
}.dialog().show()
return
}
val f = when (preference) {
is EditTextPreference -> EditTextPreferenceDialogController
.newInstance(preference.getKey())

View File

@ -3,7 +3,18 @@ package eu.kanade.tachiyomi.ui.setting
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.preference.*
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItems
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.widget.preference.IntListMatPreference
import eu.kanade.tachiyomi.widget.preference.IntListPreference
import eu.kanade.tachiyomi.widget.preference.ListMatPreference
import eu.kanade.tachiyomi.widget.preference.MultiListMatPreference
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
@DslMarker
@Target(AnnotationTarget.TYPE)
@ -29,16 +40,19 @@ inline fun PreferenceGroup.editTextPreference(block: (@DSL EditTextPreference).(
return initThenAdd(EditTextPreference(context), block).also(::initDialog)
}
inline fun PreferenceGroup.listPreference(block: (@DSL ListPreference).() -> Unit): ListPreference {
return initThenAdd(ListPreference(context), block).also(::initDialog)
inline fun PreferenceGroup.listPreference(block: (@DSL ListMatPreference).() -> Unit):
ListMatPreference {
return initThenAdd(ListMatPreference(context), block)
}
inline fun PreferenceGroup.intListPreference(block: (@DSL IntListPreference).() -> Unit): IntListPreference {
return initThenAdd(IntListPreference(context), block).also(::initDialog)
inline fun PreferenceGroup.intListPreference(block: (@DSL IntListMatPreference).() -> Unit):
IntListMatPreference {
return initThenAdd(IntListMatPreference(context), block)
}
inline fun PreferenceGroup.multiSelectListPreference(block: (@DSL MultiSelectListPreference).() -> Unit): MultiSelectListPreference {
return initThenAdd(MultiSelectListPreference(context), block).also(::initDialog)
inline fun PreferenceGroup.multiSelectListPreferenceMat(block: (@DSL MultiListMatPreference).()
-> Unit): MultiListMatPreference {
return initThenAdd(MultiListMatPreference(context), block)
}
inline fun PreferenceScreen.preferenceCategory(block: (@DSL PreferenceCategory).() -> Unit): PreferenceCategory {
@ -62,7 +76,7 @@ fun initDialog(dialogPreference: DialogPreference) {
inline fun <P : Preference> PreferenceGroup.initThenAdd(p: P, block: P.() -> Unit): P {
return p.apply {
block()
this.isIconSpaceReserved = false
this.isIconSpaceReserved = false
addPreference(this)
}
}
@ -102,11 +116,3 @@ var Preference.summaryRes: Int
var Preference.iconTint: Int
get() = 0 // set only
set(value) { DrawableCompat.setTint(icon, value) }
var ListPreference.entriesRes: Array<Int>
get() = emptyArray() // set only
set(value) { entries = value.map { context.getString(it) }.toTypedArray() }
var MultiSelectListPreference.entriesRes: Array<Int>
get() = emptyArray() // set only
set(value) { entries = value.map { context.getString(it) }.toTypedArray() }

View File

@ -22,8 +22,6 @@ import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.ui.base.controller.popControllerWithTag
import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe
import eu.kanade.tachiyomi.util.*
import java.io.File
import java.util.concurrent.TimeUnit
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
class SettingsBackupController : SettingsController() {
@ -84,15 +82,14 @@ class SettingsBackupController : SettingsController() {
entriesRes = arrayOf(R.string.update_never, R.string.update_6hour,
R.string.update_12hour, R.string.update_24hour,
R.string.update_48hour, R.string.update_weekly)
entryValues = arrayOf("0", "6", "12", "24", "48", "168")
defaultValue = "0"
summary = "%s"
entryValues = listOf(0, 6, 12, 24, 48, 168)
defaultValue = 0
onChange { newValue ->
// Always cancel the previous task, it seems that sometimes they are not updated
BackupCreatorJob.cancelTask()
val interval = (newValue as String).toInt()
val interval = newValue as Int
if (interval > 0) {
BackupCreatorJob.setupTask(interval)
}
@ -123,10 +120,9 @@ class SettingsBackupController : SettingsController() {
val backupNumber = intListPreference {
key = Keys.numberOfBackups
titleRes = R.string.pref_backup_slots
entries = arrayOf("1", "2", "3", "4", "5")
entryValues = entries
defaultValue = "1"
summary = "%s"
entries = listOf("1", "2", "3", "4", "5")
entryRange = 1..5
defaultValue = 1
}
preferences.backupInterval().asObservable()

View File

@ -65,9 +65,8 @@ class SettingsDownloadController : SettingsController() {
entriesRes = arrayOf(R.string.disabled, R.string.last_read_chapter,
R.string.second_to_last, R.string.third_to_last, R.string.fourth_to_last,
R.string.fifth_to_last)
entryValues = arrayOf("-1", "0", "1", "2", "3", "4")
defaultValue = "-1"
summary = "%s"
entryRange = -1..4
defaultValue = -1
}
}
@ -81,11 +80,12 @@ class SettingsDownloadController : SettingsController() {
titleRes = R.string.pref_download_new
defaultValue = false
}
multiSelectListPreference {
multiSelectListPreferenceMat {
key = Keys.downloadNewCategories
titleRes = R.string.pref_download_new_categories
entries = dbCategories.map { it.name }.toTypedArray()
entryValues = dbCategories.map { it.id.toString() }.toTypedArray()
entries = dbCategories.map { it.name }
entryValues = dbCategories.map { it.id.toString() }
allSelectionRes = R.string.all
preferences.downloadNew().asObservable()
.subscribeUntilDestroy { isVisible = it }
@ -96,7 +96,7 @@ class SettingsDownloadController : SettingsController() {
.mapNotNull { id -> dbCategories.find { it.id == id.toInt() } }
.sortedBy { it.order }
summary = if (selectedCategories.isEmpty())
customSummary = if (selectedCategories.isEmpty())
resources?.getString(R.string.all)
else
selectedCategories.joinToString { it.name }

View File

@ -7,7 +7,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.updater.UpdaterJob
import eu.kanade.tachiyomi.util.LocaleHelper
import eu.kanade.tachiyomi.widget.preference.IntListPreference
import eu.kanade.tachiyomi.widget.preference.IntListMatPreference
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
class SettingsGeneralController : SettingsController() {
@ -20,14 +20,14 @@ class SettingsGeneralController : SettingsController() {
listPreference {
key = Keys.lang
titleRes = R.string.pref_language
entryValues = arrayOf("", "ar", "bg", "bn", "ca", "cs", "de", "el", "en-US", "en-GB",
entryValues = listOf("", "ar", "bg", "bn", "ca", "cs", "de", "el", "en-US", "en-GB",
"es", "fr", "hi", "hu", "in", "it", "ja", "ko", "lv", "ms", "nb-rNO", "nl", "pl", "pt",
"pt-BR", "ro", "ru", "sc", "sr", "sv", "th", "tl", "tr", "uk", "vi", "zh-rCN")
entries = entryValues.map { value ->
val locale = LocaleHelper.getLocaleFromString(value.toString())
locale?.getDisplayName(locale)?.capitalize() ?:
context.getString(R.string.system_default)
}.toTypedArray()
}
defaultValue = ""
summary = "%s"
@ -41,6 +41,29 @@ class SettingsGeneralController : SettingsController() {
}
}
intListPreference {
key = Keys.theme
titleRes = R.string.pref_theme
entriesRes = arrayOf(R.string.light_theme, R.string.dark_theme,
R.string.amoled_theme, R.string.darkblue_theme,
R.string.system_theme, R.string.system_amoled_theme, R.string.system_darkblue_theme)
entryRange = 1..7
defaultValue = 5
onChange {
activity?.recreate()
true
}
}
intListPreference {
key = Keys.startScreen
titleRes = R.string.pref_start_screen
entriesRes = arrayOf(R.string.label_library, R.string.label_recent_manga,
R.string.label_recent_updates)
entryRange = 1..3
defaultValue = 1
}
/*intListPreference {
key = Keys.theme
titleRes = R.string.pref_theme
entriesRes = arrayOf(R.string.light_theme, R.string.dark_theme,
@ -63,7 +86,7 @@ class SettingsGeneralController : SettingsController() {
entryValues = arrayOf("1", "2", "3")
defaultValue = "1"
summary = "%s"
}
}*/
switchPreference {
key = Keys.automaticUpdates
titleRes = R.string.pref_enable_automatic_updates
@ -87,7 +110,7 @@ class SettingsGeneralController : SettingsController() {
val biometricManager = BiometricManager.from(context)
if (biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) {
var preference:IntListPreference? = null
var preference:IntListMatPreference? = null
switchPreference {
key = Keys.useBiometrics
titleRes = R.string.lock_with_biometrics
@ -102,18 +125,17 @@ class SettingsGeneralController : SettingsController() {
key = Keys.lockAfter
titleRes = R.string.lock_when_idle
isVisible = preferences.useBiometrics().getOrDefault()
val values = arrayOf("0", "2", "5", "10", "20", "30", "60", "90", "120", "-1")
entries = values.map {
val values = listOf(0, 2, 5, 10, 20, 30, 60, 90, 120, -1)
entries = values.mapNotNull {
when (it) {
"0" -> context.getString(R.string.lock_always)
"-1" -> context.getString(R.string.lock_never)
0 -> context.getString(R.string.lock_always)
-1 -> context.getString(R.string.lock_never)
else -> resources?.getQuantityString(R.plurals.lock_after_mins, it.toInt(),
it)
}
}.toTypedArray()
}
entryValues = values
defaultValue = "0"
summary = "%s"
defaultValue = 0
}
}
}

View File

@ -4,7 +4,6 @@ import android.app.Dialog
import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.biometric.BiometricManager
import androidx.preference.PreferenceScreen
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
@ -15,8 +14,6 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.util.LocaleHelper
import eu.kanade.tachiyomi.widget.preference.IntListPreference
import kotlinx.android.synthetic.main.pref_library_columns.view.*
import rx.Observable
import uy.kohesive.injekt.Injekt
@ -60,27 +57,26 @@ class SettingsLibraryController : SettingsController() {
entriesRes = arrayOf(R.string.update_never, R.string.update_1hour,
R.string.update_2hour, R.string.update_3hour, R.string.update_6hour,
R.string.update_12hour, R.string.update_24hour, R.string.update_48hour)
entryValues = arrayOf("0", "1", "2", "3", "6", "12", "24", "48")
defaultValue = "0"
summary = "%s"
entryValues = listOf(0, 1, 2, 3, 6, 12, 24, 48)
defaultValue = 0
onChange { newValue ->
// Always cancel the previous task, it seems that sometimes they are not updated.
LibraryUpdateJob.cancelTask()
val interval = (newValue as String).toInt()
val interval = newValue as Int
if (interval > 0) {
LibraryUpdateJob.setupTask(interval)
}
true
}
}
multiSelectListPreference {
multiSelectListPreferenceMat {
key = Keys.libraryUpdateRestriction
titleRes = R.string.pref_library_update_restriction
entriesRes = arrayOf(R.string.wifi, R.string.charging)
entryValues = arrayOf("wifi", "ac")
summaryRes = R.string.pref_library_update_restriction_summary
entryValues = listOf("wifi", "ac")
customSummartRes = R.string.pref_library_update_restriction_summary
preferences.libraryUpdateInterval().asObservable()
.subscribeUntilDestroy { isVisible = it > 0 }
@ -99,11 +95,12 @@ class SettingsLibraryController : SettingsController() {
val dbCategories = db.getCategories().executeAsBlocking()
multiSelectListPreference {
multiSelectListPreferenceMat {
key = Keys.libraryUpdateCategories
titleRes = R.string.pref_library_update_categories
entries = dbCategories.map { it.name }.toTypedArray()
entryValues = dbCategories.map { it.id.toString() }.toTypedArray()
entries = dbCategories.map { it.name }
entryValues = dbCategories.map { it.id.toString() }
allSelectionRes = R.string.all
preferences.libraryUpdateCategories().asObservable()
.subscribeUntilDestroy {
@ -111,7 +108,7 @@ class SettingsLibraryController : SettingsController() {
.mapNotNull { id -> dbCategories.find { it.id == id.toInt() } }
.sortedBy { it.order }
summary = if (selectedCategories.isEmpty())
customSummary = if (selectedCategories.isEmpty())
context.getString(R.string.all)
else
selectedCategories.joinToString { it.name }
@ -127,11 +124,8 @@ class SettingsLibraryController : SettingsController() {
R.string.action_sort_alpha,
R.string.action_sort_last_updated
)
entryValues = arrayOf(
"0",
"1"
)
defaultValue = "0"
entryRange = 0..1
defaultValue = 0
summaryRes = R.string.pref_library_update_prioritization_summary
}
intListPreference {
@ -139,16 +133,16 @@ class SettingsLibraryController : SettingsController() {
titleRes = R.string.default_category
val categories = listOf(Category.createDefault()) + dbCategories
entries = arrayOf(context.getString(R.string.default_category_summary)) +
entries = listOf(context.getString(R.string.default_category_summary)) +
categories.map { it.name }.toTypedArray()
entryValues = arrayOf("-1") + categories.map { it.id.toString() }.toTypedArray()
entryValues = listOf(-1) + categories.mapNotNull { it.id }.toList()
defaultValue = "-1"
val selectedCategory = categories.find { it.id == preferences.defaultCategory() }
summary = selectedCategory?.name ?: context.getString(R.string.default_category_summary)
onChange { newValue ->
summary = categories.find {
it.id == (newValue as String).toInt()
it.id == newValue as Int
}?.name ?: context.getString(R.string.default_category_summary)
true
}

View File

@ -15,9 +15,8 @@ class SettingsReaderController : SettingsController() {
titleRes = R.string.pref_viewer_type
entriesRes = arrayOf(R.string.left_to_right_viewer, R.string.right_to_left_viewer,
R.string.vertical_viewer, R.string.webtoon_viewer)
entryValues = arrayOf("1", "2", "3", "4")
defaultValue = "1"
summary = "%s"
entryRange = 1..4
defaultValue = 1
}
intListPreference {
key = Keys.imageScaleType
@ -25,44 +24,41 @@ class SettingsReaderController : SettingsController() {
entriesRes = arrayOf(R.string.scale_type_fit_screen, R.string.scale_type_stretch,
R.string.scale_type_fit_width, R.string.scale_type_fit_height,
R.string.scale_type_original_size, R.string.scale_type_smart_fit)
entryValues = arrayOf("1", "2", "3", "4", "5", "6")
defaultValue = "1"
summary = "%s"
entryRange = 1..6
defaultValue = 1
}
intListPreference {
key = Keys.zoomStart
titleRes = R.string.pref_zoom_start
entriesRes = arrayOf(R.string.zoom_start_automatic, R.string.zoom_start_left,
R.string.zoom_start_right, R.string.zoom_start_center)
entryValues = arrayOf("1", "2", "3", "4")
defaultValue = "1"
summary = "%s"
entryRange = 1..4
defaultValue = 1
}
intListPreference {
key = Keys.rotation
titleRes = R.string.pref_rotation_type
entriesRes = arrayOf(R.string.rotation_free, R.string.rotation_lock,
R.string.rotation_force_portrait, R.string.rotation_force_landscape)
entryValues = arrayOf("1", "2", "3", "4")
defaultValue = "1"
summary = "%s"
entryRange = 1..4
defaultValue = 1
}
intListPreference {
key = Keys.readerTheme
titleRes = R.string.pref_reader_theme
entriesRes = arrayOf(R.string.white_background, R.string.black_background, R.string
.reader_theme_smart, R.string.reader_theme_smart_theme)
entryValues = arrayOf("0", "1", "2", "3")
defaultValue = "0"
summary = "%s"
entryRange = 0..3
defaultValue = 2
}
intListPreference {
key = Keys.doubleTapAnimationSpeed
titleRes = R.string.pref_double_tap_anim_speed
entries = arrayOf(context.getString(R.string.double_tap_anim_speed_0), context.getString(R.string.double_tap_anim_speed_fast), context.getString(R.string.double_tap_anim_speed_normal))
entryValues = arrayOf("1", "250", "500") // using a value of 0 breaks the image viewer, so min is 1
defaultValue = "500"
summary = "%s"
entries = listOf(context.getString(R.string.double_tap_anim_speed_0), context.getString(R
.string.double_tap_anim_speed_fast), context.getString(R.string.double_tap_anim_speed_normal))
entryValues = listOf(1, 250, 500) // using a value of 0 breaks the image viewer, so
// min is 1
defaultValue = 500
}
switchPreference {
key = Keys.skipRead

View File

@ -0,0 +1,60 @@
package eu.kanade.tachiyomi.widget.preference
import android.content.Context
import android.util.AttributeSet
import androidx.preference.Preference
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.ui.setting.defaultValue
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
class IntListMatPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? =
null) :
Preference(context, attrs) {
private val prefs: PreferencesHelper = Injekt.get()
var entryValues:List<Int> = emptyList()
var entryRange:IntRange
get() = 0..0
set(value) { entryValues = value.toList() }
var entriesRes:Array<Int>
get() = emptyArray()
set(value) { entries = value.map { context.getString(it) } }
private var defValue:Int = 0
var entries:List<String> = emptyList()
override fun onSetInitialValue(defaultValue: Any?) {
super.onSetInitialValue(defaultValue)
defValue = defaultValue as? Int ?: defValue
}
override fun getSummary(): CharSequence {
val index = entryValues.indexOf(prefs.getInt(key, defValue).getOrDefault())
return if (entries.isEmpty() || index == -1) ""
else entries[index]
}
override fun onClick() {
dialog().show()
}
fun dialog(): MaterialDialog {
return MaterialDialog(context).apply {
title(text = title.toString())
negativeButton(android.R.string.cancel)
val default = entryValues.indexOf(prefs.getInt(key, defValue).getOrDefault())
listItemsSingleChoice(items = entries,
waitForPositiveButton = false,
initialSelection = default) {
_, pos, _ ->
val value = entryValues[pos]
prefs.getInt(key, defValue).set(value)
callChangeListener(value)
this@IntListMatPreference.summary = this@IntListMatPreference.summary
dismiss()
}
}
}
}

View File

@ -0,0 +1,79 @@
package eu.kanade.tachiyomi.widget.preference
import android.content.Context
import android.content.SharedPreferences
import android.util.AttributeSet
import androidx.preference.Preference
import androidx.preference.PreferenceManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.ui.setting.defaultValue
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
open class ListMatPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? =
null) :
Preference(context, attrs) {
protected val prefs: PreferencesHelper = Injekt.get()
var sharedPref:String? = null
var otherPref:Preference? = null
var entryValues:List<String> = emptyList()
var entriesRes:Array<Int>
get() = emptyArray()
set(value) { entries = value.map { context.getString(it) } }
protected var defValue:String = ""
var entries:List<String> = emptyList()
override fun onSetInitialValue(defaultValue: Any?) {
super.onSetInitialValue(defaultValue)
defValue = defaultValue as? String ?: defValue
}
override fun getSummary(): CharSequence {
val index = entryValues.indexOf(prefs.getStringPref(key, defValue).getOrDefault())
return if (entries.isEmpty() || index == -1) ""
else entries[index]
}
override fun onClick() {
dialog().show()
}
open fun dialog(): MaterialDialog {
return MaterialDialog(context).apply {
if (this@ListMatPreference.title != null)
title(text = this@ListMatPreference.title.toString())
negativeButton(android.R.string.cancel)
val default = entryValues.indexOf(if (sharedPref != null) {
val settings = context.getSharedPreferences(sharedPref, Context.MODE_PRIVATE)
settings.getString(key, "")
}
else prefs.getStringPref(key, defValue).getOrDefault())
listItemsSingleChoice(items = entries,
waitForPositiveButton = false,
initialSelection = default) { _, pos, _ ->
val value = entryValues[pos]
if (sharedPref != null) {
val oldDef = if (default > -1) entries[default] else ""
val settings = context.getSharedPreferences(sharedPref, Context.MODE_PRIVATE)
val edit = settings.edit()
edit.putString(key, value)
edit.apply()
otherPref?.callChangeListener(value)
if (oldDef == otherPref?.summary || otherPref?.summary.isNullOrEmpty()) otherPref?.summary =
entries[pos]
else otherPref?.summary = otherPref?.summary?.toString()?.replace(oldDef,
entries[pos]
) ?: entries[pos]
}
else {
prefs.getStringPref(key, defValue).set(value)
this@ListMatPreference.summary = this@ListMatPreference.summary
}
dismiss()
}
}
}
}

View File

@ -0,0 +1,59 @@
package eu.kanade.tachiyomi.widget.preference
import android.content.Context
import android.util.AttributeSet
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.checkItem
import com.afollestad.materialdialogs.list.isItemChecked
import com.afollestad.materialdialogs.list.listItemsMultiChoice
import com.afollestad.materialdialogs.list.uncheckItem
import eu.kanade.tachiyomi.data.preference.getOrDefault
class MultiListMatPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? =
null) :
ListMatPreference(context, attrs) {
var allSelectionRes:Int? = null
var customSummartRes:Int
get() = 0
set(value) { customSummary = context.getString(value) }
var customSummary:String? = null
var positions:IntArray? = null
override fun getSummary(): CharSequence {
return customSummary ?: super.getSummary()
}
override fun dialog():MaterialDialog {
return MaterialDialog(context).apply {
title(text = title.toString())
negativeButton(android.R.string.cancel)
positiveButton(android.R.string.ok) {
var value = positions?.map {
entryValues[it - if (allSelectionRes != null) 1 else 0] }?.toSet() ?: emptySet()
if (allSelectionRes != null && isItemChecked(0)) value = emptySet()
prefs.getStringSet(key, emptySet()).set(value)
callChangeListener(value)
this@MultiListMatPreference.summary = this@MultiListMatPreference.summary
}
val set = prefs.getStringSet(key, emptySet()).getOrDefault()
var default = set.map {
entryValues.indexOf(it) + if (allSelectionRes != null) 1 else 0 }
.toIntArray()
if (allSelectionRes != null && default.isEmpty()) default = intArrayOf(0)
val items = if (allSelectionRes != null)
(listOf(context.getString(allSelectionRes!!)) + entries) else entries
listItemsMultiChoice(items = items,
allowEmptySelection = true,
disabledIndices = if (allSelectionRes != null) intArrayOf(0) else null,
waitForPositiveButton = false,
initialSelection = default) { _, pos, _ ->
positions = pos
if (allSelectionRes != null) {
if (pos.isEmpty()) checkItem(0)
else uncheckItem(0)
}
}
}
}
}

View File

@ -3,5 +3,6 @@
<style name="Theme.Tachiyomi" parent="Theme.Base">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="md_button_casing">literal</item>
</style>
</resources>

View File

@ -5,5 +5,6 @@
<style name="Theme.Tachiyomi" parent="Theme.Base">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="md_button_casing">literal</item>
</style>
</resources>