MCPcopy Create free account
hub / github.com/cinit/TMoe / AbstractSwitch

Class AbstractSwitch

app/src/main/java/cc/ioctl/tmoe/ui/dsl/item/AbstractSwitch.kt:12–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10import cc.ioctl.tmoe.ui.wrapper.cell.TextCheckCell
11
12class AbstractSwitch(
13 val titleKey: String,
14 val titleResId: Int,
15 private val isCheckedLambda: () -> Boolean,
16 private val onCheckedChanged: (Boolean) -> Unit,
17 private val descKey: String? = null,
18 private val descResId: Int? = null,
19 private val descProvider: ((Context) -> String?)? = null,
20 private val enabledLambda: (() -> Boolean)?,
21 private val extraOnClickListener: View.OnClickListener? = null
22) : DslTMsgListItemInflatable, TMsgListItem {
23
24 class ViewHolder(cell: TextCheckCell) : RecyclerView.ViewHolder(cell)
25
26 override val isEnabled = true
27 override val isVoidBackground = false
28
29 override fun createViewHolder(context: Context, parent: ViewGroup) =
30 ViewHolder(TextCheckCell(context))
31
32 override fun bindView(viewHolder: RecyclerView.ViewHolder, position: Int, context: Context) {
33 val cell = viewHolder.itemView as TextCheckCell
34 val titleString = LocaleController.getString(titleKey, titleResId)
35 val descString: String? = descProvider?.invoke(context) ?: descKey?.let {
36 LocaleController.getString(it, descResId ?: 0)
37 }
38 if (descString != null) {
39 cell.setTextAndValueAndCheck(titleString, descString, isCheckedLambda(), true, true)
40 } else {
41 cell.setTextAndCheck(titleString, isCheckedLambda(), true)
42 }
43 cell.drawLine = extraOnClickListener != null
44 }
45
46 override fun onItemClick(v: View, position: Int, x: Float, y: Float) {
47 val cell = v as TextCheckCell
48 if (extraOnClickListener == null || cell.isInSwitchRange(x.toInt())) {
49 if (enabledLambda?.invoke() == false) return
50 val checked = cell.toggle()
51 onCheckedChanged(checked)
52 } else {
53 extraOnClickListener.onClick(v)
54 }
55 // update description if dynamic
56 if (descProvider != null) {
57 val descString = descProvider.invoke(v.context)
58 val titleString = LocaleController.getString(titleKey, titleResId)
59 cell.setTextAndValueAndCheck(titleString, descString, isCheckedLambda(), true, true)
60 }
61 }
62}

Callers 1

SettingsFragmentClass · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected