FormSwitchItem.kt 2.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * Copyright 2019 New Vector Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
package im.vector.app.features.form
18

19
import android.view.View
20
import android.widget.TextView
21
import androidx.core.view.isVisible
22
23
24
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import com.google.android.material.switchmaterial.SwitchMaterial
25
26
27
28
import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.extensions.setTextOrHide
29
30
31
32
33
34
35
36
37
38
39
40
41
42

@EpoxyModelClass(layout = R.layout.item_form_switch)
abstract class FormSwitchItem : VectorEpoxyModel<FormSwitchItem.Holder>() {

    @EpoxyAttribute
    var listener: ((Boolean) -> Unit)? = null

    @EpoxyAttribute
    var enabled: Boolean = true

    @EpoxyAttribute
    var switchChecked: Boolean = false

    @EpoxyAttribute
Valere's avatar
Valere committed
43
    var title: CharSequence? = null
44
45
46
47

    @EpoxyAttribute
    var summary: String? = null

48
49
50
    @EpoxyAttribute
    var showDivider: Boolean = true

51
    override fun bind(holder: Holder) {
52
        super.bind(holder)
Benoît Marty's avatar
Benoît Marty committed
53
54
55
56
57
58
        holder.view.setOnClickListener {
            if (enabled) {
                holder.switchView.toggle()
            }
        }

59
60
61
62
63
        holder.titleView.text = title
        holder.summaryView.setTextOrHide(summary)

        holder.switchView.isEnabled = enabled

Valere's avatar
Valere committed
64
65
66
67
68
69
70
71
72
        if (holder.view.isAttachedToWindow) {
            // the view is attached to the window
            // So it is a rebind of new data and you could ignore it assuming this is value that was already inputted into the view.
        } else {
            holder.switchView.setOnCheckedChangeListener(null)
            holder.switchView.isChecked = switchChecked
            holder.switchView.setOnCheckedChangeListener { _, isChecked ->
                listener?.invoke(isChecked)
            }
73
        }
74
        holder.divider.isVisible = showDivider
75
76
77
    }

    override fun shouldSaveViewState(): Boolean {
Benoit Marty's avatar
Benoit Marty committed
78
        return false
79
80
81
82
83
84
85
86
87
88
89
90
    }

    override fun unbind(holder: Holder) {
        super.unbind(holder)

        holder.switchView.setOnCheckedChangeListener(null)
    }

    class Holder : VectorEpoxyHolder() {
        val titleView by bind<TextView>(R.id.formSwitchTitle)
        val summaryView by bind<TextView>(R.id.formSwitchSummary)
        val switchView by bind<SwitchMaterial>(R.id.formSwitchSwitch)
91
        val divider by bind<View>(R.id.formSwitchDivider)
92
93
    }
}