|
|
@ -14,6 +14,7 @@ import androidx.preference.Preference |
|
|
|
import androidx.preference.PreferenceViewHolder |
|
|
|
import androidx.preference.PreferenceViewHolder |
|
|
|
import com.jaredrummler.android.colorpicker.* |
|
|
|
import com.jaredrummler.android.colorpicker.* |
|
|
|
import io.legado.app.lib.theme.ATH |
|
|
|
import io.legado.app.lib.theme.ATH |
|
|
|
|
|
|
|
import io.legado.app.lib.theme.ColorUtils |
|
|
|
|
|
|
|
|
|
|
|
class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs), |
|
|
|
class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs), |
|
|
|
ColorPickerDialogListener { |
|
|
|
ColorPickerDialogListener { |
|
|
@ -22,8 +23,9 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex |
|
|
|
private val sizeLarge = 1 |
|
|
|
private val sizeLarge = 1 |
|
|
|
|
|
|
|
|
|
|
|
private var onShowDialogListener: OnShowDialogListener? = null |
|
|
|
private var onShowDialogListener: OnShowDialogListener? = null |
|
|
|
private var color = Color.BLACK |
|
|
|
private var mColor = Color.BLACK |
|
|
|
private var showDialog: Boolean = false |
|
|
|
private var showDialog: Boolean = false |
|
|
|
|
|
|
|
|
|
|
|
@ColorPickerDialog.DialogType |
|
|
|
@ColorPickerDialog.DialogType |
|
|
|
private var dialogType: Int = 0 |
|
|
|
private var dialogType: Int = 0 |
|
|
|
private var colorShape: Int = 0 |
|
|
|
private var colorShape: Int = 0 |
|
|
@ -42,7 +44,8 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex |
|
|
|
val a = context.obtainStyledAttributes(attrs, R.styleable.ColorPreference) |
|
|
|
val a = context.obtainStyledAttributes(attrs, R.styleable.ColorPreference) |
|
|
|
showDialog = a.getBoolean(R.styleable.ColorPreference_cpv_showDialog, true) |
|
|
|
showDialog = a.getBoolean(R.styleable.ColorPreference_cpv_showDialog, true) |
|
|
|
|
|
|
|
|
|
|
|
dialogType = a.getInt(R.styleable.ColorPreference_cpv_dialogType, ColorPickerDialog.TYPE_PRESETS) |
|
|
|
dialogType = |
|
|
|
|
|
|
|
a.getInt(R.styleable.ColorPreference_cpv_dialogType, ColorPickerDialog.TYPE_PRESETS) |
|
|
|
colorShape = a.getInt(R.styleable.ColorPreference_cpv_colorShape, ColorShape.CIRCLE) |
|
|
|
colorShape = a.getInt(R.styleable.ColorPreference_cpv_colorShape, ColorShape.CIRCLE) |
|
|
|
allowPresets = a.getBoolean(R.styleable.ColorPreference_cpv_allowPresets, true) |
|
|
|
allowPresets = a.getBoolean(R.styleable.ColorPreference_cpv_allowPresets, true) |
|
|
|
allowCustom = a.getBoolean(R.styleable.ColorPreference_cpv_allowCustom, true) |
|
|
|
allowCustom = a.getBoolean(R.styleable.ColorPreference_cpv_allowCustom, true) |
|
|
@ -50,7 +53,8 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex |
|
|
|
showColorShades = a.getBoolean(R.styleable.ColorPreference_cpv_showColorShades, true) |
|
|
|
showColorShades = a.getBoolean(R.styleable.ColorPreference_cpv_showColorShades, true) |
|
|
|
previewSize = a.getInt(R.styleable.ColorPreference_cpv_previewSize, sizeNormal) |
|
|
|
previewSize = a.getInt(R.styleable.ColorPreference_cpv_previewSize, sizeNormal) |
|
|
|
val presetsResId = a.getResourceId(R.styleable.ColorPreference_cpv_colorPresets, 0) |
|
|
|
val presetsResId = a.getResourceId(R.styleable.ColorPreference_cpv_colorPresets, 0) |
|
|
|
dialogTitle = a.getResourceId(R.styleable.ColorPreference_cpv_dialogTitle, R.string.cpv_default_title) |
|
|
|
dialogTitle = |
|
|
|
|
|
|
|
a.getResourceId(R.styleable.ColorPreference_cpv_dialogTitle, R.string.cpv_default_title) |
|
|
|
presets = if (presetsResId != 0) { |
|
|
|
presets = if (presetsResId != 0) { |
|
|
|
context.resources.getIntArray(presetsResId) |
|
|
|
context.resources.getIntArray(presetsResId) |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -67,7 +71,7 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex |
|
|
|
override fun onClick() { |
|
|
|
override fun onClick() { |
|
|
|
super.onClick() |
|
|
|
super.onClick() |
|
|
|
if (onShowDialogListener != null) { |
|
|
|
if (onShowDialogListener != null) { |
|
|
|
onShowDialogListener!!.onShowColorPickerDialog(title as String, color) |
|
|
|
onShowDialogListener!!.onShowColorPickerDialog(title as String, mColor) |
|
|
|
} else if (showDialog) { |
|
|
|
} else if (showDialog) { |
|
|
|
val dialog = ColorPickerDialogCompat.newBuilder() |
|
|
|
val dialog = ColorPickerDialogCompat.newBuilder() |
|
|
|
.setDialogType(dialogType) |
|
|
|
.setDialogType(dialogType) |
|
|
@ -78,7 +82,7 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex |
|
|
|
.setAllowCustom(allowCustom) |
|
|
|
.setAllowCustom(allowCustom) |
|
|
|
.setShowAlphaSlider(showAlphaSlider) |
|
|
|
.setShowAlphaSlider(showAlphaSlider) |
|
|
|
.setShowColorShades(showColorShades) |
|
|
|
.setShowColorShades(showColorShades) |
|
|
|
.setColor(color) |
|
|
|
.setColor(mColor) |
|
|
|
.create() |
|
|
|
.create() |
|
|
|
dialog.setColorPickerDialogListener(this) |
|
|
|
dialog.setColorPickerDialogListener(this) |
|
|
|
getActivity().supportFragmentManager |
|
|
|
getActivity().supportFragmentManager |
|
|
@ -111,10 +115,12 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: PreferenceViewHolder) { |
|
|
|
override fun onBindViewHolder(holder: PreferenceViewHolder) { |
|
|
|
val v = io.legado.app.ui.widget.prefs.Preference.bindView<ColorPanelView>(context, holder, icon, title, summary, widgetLayoutResource, |
|
|
|
val v = io.legado.app.ui.widget.prefs.Preference.bindView<ColorPanelView>( |
|
|
|
io.legado.app.R.id.cpv_preference_preview_color_panel, 30, 30) |
|
|
|
context, holder, icon, title, summary, widgetLayoutResource, |
|
|
|
|
|
|
|
io.legado.app.R.id.cpv_preference_preview_color_panel, 30, 30 |
|
|
|
|
|
|
|
) |
|
|
|
if (v is ColorPanelView) { |
|
|
|
if (v is ColorPanelView) { |
|
|
|
v.color = color |
|
|
|
v.color = mColor |
|
|
|
} |
|
|
|
} |
|
|
|
super.onBindViewHolder(holder) |
|
|
|
super.onBindViewHolder(holder) |
|
|
|
} |
|
|
|
} |
|
|
@ -122,10 +128,10 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex |
|
|
|
override fun onSetInitialValue(defaultValue: Any?) { |
|
|
|
override fun onSetInitialValue(defaultValue: Any?) { |
|
|
|
super.onSetInitialValue(defaultValue) |
|
|
|
super.onSetInitialValue(defaultValue) |
|
|
|
if (defaultValue is Int) { |
|
|
|
if (defaultValue is Int) { |
|
|
|
color = (defaultValue as Int?)!! |
|
|
|
mColor = if (!showAlphaSlider) ColorUtils.withAlpha(defaultValue, 1f) else defaultValue |
|
|
|
persistInt(color) |
|
|
|
persistInt(mColor) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
color = getPersistedInt(-0x1000000) |
|
|
|
mColor = getPersistedInt(-0x1000000) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -147,8 +153,8 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex |
|
|
|
* @param color The newly selected color |
|
|
|
* @param color The newly selected color |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun saveValue(@ColorInt color: Int) { |
|
|
|
fun saveValue(@ColorInt color: Int) { |
|
|
|
this.color = color |
|
|
|
mColor = if (showAlphaSlider) color else ColorUtils.withAlpha(color, 1f) |
|
|
|
persistInt(this.color) |
|
|
|
persistInt(mColor) |
|
|
|
notifyChanged() |
|
|
|
notifyChanged() |
|
|
|
callChangeListener(color) |
|
|
|
callChangeListener(color) |
|
|
|
} |
|
|
|
} |
|
|
@ -231,17 +237,23 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex |
|
|
|
class Builder internal constructor() { |
|
|
|
class Builder internal constructor() { |
|
|
|
|
|
|
|
|
|
|
|
internal var colorPickerDialogListener: ColorPickerDialogListener? = null |
|
|
|
internal var colorPickerDialogListener: ColorPickerDialogListener? = null |
|
|
|
|
|
|
|
|
|
|
|
@StringRes |
|
|
|
@StringRes |
|
|
|
internal var dialogTitle = R.string.cpv_default_title |
|
|
|
internal var dialogTitle = R.string.cpv_default_title |
|
|
|
|
|
|
|
|
|
|
|
@StringRes |
|
|
|
@StringRes |
|
|
|
internal var presetsButtonText = R.string.cpv_presets |
|
|
|
internal var presetsButtonText = R.string.cpv_presets |
|
|
|
|
|
|
|
|
|
|
|
@StringRes |
|
|
|
@StringRes |
|
|
|
internal var customButtonText = R.string.cpv_custom |
|
|
|
internal var customButtonText = R.string.cpv_custom |
|
|
|
|
|
|
|
|
|
|
|
@StringRes |
|
|
|
@StringRes |
|
|
|
internal var selectedButtonText = R.string.cpv_select |
|
|
|
internal var selectedButtonText = R.string.cpv_select |
|
|
|
|
|
|
|
|
|
|
|
@DialogType |
|
|
|
@DialogType |
|
|
|
internal var dialogType = TYPE_PRESETS |
|
|
|
internal var dialogType = TYPE_PRESETS |
|
|
|
internal var presets = MATERIAL_COLORS |
|
|
|
internal var presets = MATERIAL_COLORS |
|
|
|
|
|
|
|
|
|
|
|
@ColorInt |
|
|
|
@ColorInt |
|
|
|
internal var color = Color.BLACK |
|
|
|
internal var color = Color.BLACK |
|
|
|
internal var dialogId = 0 |
|
|
|
internal var dialogId = 0 |
|
|
@ -249,6 +261,7 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex |
|
|
|
internal var allowPresets = true |
|
|
|
internal var allowPresets = true |
|
|
|
internal var allowCustom = true |
|
|
|
internal var allowCustom = true |
|
|
|
internal var showColorShades = true |
|
|
|
internal var showColorShades = true |
|
|
|
|
|
|
|
|
|
|
|
@ColorShape |
|
|
|
@ColorShape |
|
|
|
internal var colorShape = ColorShape.CIRCLE |
|
|
|
internal var colorShape = ColorShape.CIRCLE |
|
|
|
|
|
|
|
|
|
|
|