|
|
|
|
@ -160,7 +160,6 @@ fun PreferenceParent.switchPreference(
|
|
|
|
|
addPref(layout) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data class PreferenceOption<T>(val name: String, val value: T) |
|
|
|
|
@Suppress("FunctionName") |
|
|
|
|
fun <T> PreferenceParent.PreferenceOption(pair: Pair<T, Int>): PreferenceOption<T> { |
|
|
|
|
@ -174,6 +173,53 @@ fun <T> PreferenceParent.listPreference(
|
|
|
|
|
selected: () -> T, |
|
|
|
|
onSelection: (T) -> Unit, |
|
|
|
|
) { |
|
|
|
|
val (layout, summaryView, optionView) = makeListPreferenceLayout() |
|
|
|
|
summaryView.text = title |
|
|
|
|
|
|
|
|
|
registerUpdate { |
|
|
|
|
val selectedOptionIndex = options.indexOfFirst { it.value == selected() } |
|
|
|
|
|
|
|
|
|
optionView.setText(options[selectedOptionIndex].name) |
|
|
|
|
|
|
|
|
|
layout.setOnClickListener { |
|
|
|
|
AlertDialog.Builder(context) |
|
|
|
|
.setSingleChoiceItems( |
|
|
|
|
options.map { it.name }.toTypedArray(), |
|
|
|
|
selectedOptionIndex, |
|
|
|
|
) { dialog, wh -> |
|
|
|
|
onSelection(options[wh].value) |
|
|
|
|
dialog.dismiss() |
|
|
|
|
} |
|
|
|
|
.setCancelable(true) |
|
|
|
|
.show() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun PreferenceParent.customListPreference( |
|
|
|
|
title: String, |
|
|
|
|
selected: () -> String, |
|
|
|
|
onClick: () -> Unit |
|
|
|
|
) { |
|
|
|
|
val (layout, summaryView, optionView) = makeListPreferenceLayout() |
|
|
|
|
summaryView.text = title |
|
|
|
|
|
|
|
|
|
layout.setOnClickListener { |
|
|
|
|
onClick() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
registerUpdate { |
|
|
|
|
optionView.text = selected() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private data class ListPreferenceLayout( |
|
|
|
|
val layout: LinearLayout, |
|
|
|
|
val summaryView: TextView, |
|
|
|
|
val optionView: TextView, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
private fun PreferenceParent.makeListPreferenceLayout(): ListPreferenceLayout { |
|
|
|
|
val layout = itemLayout(context).apply { |
|
|
|
|
isClickable = true |
|
|
|
|
val outValue = TypedValue() |
|
|
|
|
@ -186,12 +232,11 @@ fun <T> PreferenceParent.listPreference(
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val titleView = TextView(context).apply { |
|
|
|
|
text = title |
|
|
|
|
val summaryView = TextView(context).apply { |
|
|
|
|
setTextAppearanceRef(android.R.attr.textAppearanceListItem) |
|
|
|
|
setTextColorRef(android.R.attr.textColorPrimary) |
|
|
|
|
} |
|
|
|
|
linearLayout.addView(titleView) |
|
|
|
|
linearLayout.addView(summaryView) |
|
|
|
|
|
|
|
|
|
val optionView = TextView(context) |
|
|
|
|
linearLayout.addView(optionView) |
|
|
|
|
@ -199,25 +244,7 @@ fun <T> PreferenceParent.listPreference(
|
|
|
|
|
layout.addView(linearLayout) |
|
|
|
|
|
|
|
|
|
addPref(layout) |
|
|
|
|
|
|
|
|
|
registerUpdate { |
|
|
|
|
val selectedOptionIndex = options.indexOfFirst { it.value == selected() } |
|
|
|
|
|
|
|
|
|
optionView.setText(options[selectedOptionIndex].name) |
|
|
|
|
|
|
|
|
|
layout.setOnClickListener { |
|
|
|
|
AlertDialog.Builder(context) |
|
|
|
|
.setSingleChoiceItems( |
|
|
|
|
options.map { it.name }.toTypedArray(), |
|
|
|
|
selectedOptionIndex, |
|
|
|
|
) { dialog, wh -> |
|
|
|
|
onSelection(options[wh].value) |
|
|
|
|
dialog.dismiss() |
|
|
|
|
} |
|
|
|
|
.setCancelable(true) |
|
|
|
|
.show() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return ListPreferenceLayout(layout, summaryView, optionView) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|