FormEditTextItem.kt 3.7 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

Benoit Marty's avatar
Benoit Marty committed
19
import android.text.Editable
20
import android.view.View
Valere's avatar
Valere committed
21
import android.view.inputmethod.EditorInfo
22
import androidx.core.view.isVisible
23
24
25
26
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
27
28
29
30
import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.platform.SimpleTextWatcher
31
32
33
34
35
36
37
38
39
40

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

    @EpoxyAttribute
    var hint: String? = null

    @EpoxyAttribute
    var value: String? = null

41
42
43
44
45
46
    @EpoxyAttribute
    var showBottomSeparator: Boolean = true

    @EpoxyAttribute
    var errorMessage: String? = null

47
48
49
    @EpoxyAttribute
    var enabled: Boolean = true

50
51
52
    @EpoxyAttribute
    var inputType: Int? = null

Valere's avatar
Valere committed
53
54
55
56
57
58
    @EpoxyAttribute
    var singleLine: Boolean? = null

    @EpoxyAttribute
    var imeOptions: Int? = null

Valere's avatar
Valere committed
59
60
61
    @EpoxyAttribute
    var endIconMode: Int? = null

Valere's avatar
Valere committed
62
    @EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
63
64
    var onTextChange: ((String) -> Unit)? = null

65
    private val onTextChangeListener = object : SimpleTextWatcher() {
Benoit Marty's avatar
Benoit Marty committed
66
        override fun afterTextChanged(s: Editable) {
67
68
69
            onTextChange?.invoke(s.toString())
        }
    }
70
71

    override fun bind(holder: Holder) {
72
        super.bind(holder)
73
74
        holder.textInputLayout.isEnabled = enabled
        holder.textInputLayout.hint = hint
75
        holder.textInputLayout.error = errorMessage
Benoit Marty's avatar
Benoit Marty committed
76
        holder.textInputLayout.endIconMode = endIconMode ?: TextInputLayout.END_ICON_NONE
Valere's avatar
Valere committed
77

Valere's avatar
Valere committed
78
79
80
81
82
83
84
        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 text that was already inputted into the view.
            // Downside is if you ever wanted to programmatically change the content of the edit text while it is on screen you would not be able to
        } else {
            holder.textInputEditText.setText(value)
        }
85
        holder.textInputEditText.isEnabled = enabled
86
        inputType?.let { holder.textInputEditText.inputType = it }
Valere's avatar
Valere committed
87
88
        holder.textInputEditText.isSingleLine = singleLine ?: false
        holder.textInputEditText.imeOptions = imeOptions ?: EditorInfo.IME_ACTION_NONE
89

90
        holder.textInputEditText.addTextChangedListener(onTextChangeListener)
91
        holder.bottomSeparator.isVisible = showBottomSeparator
92
93
94
    }

    override fun shouldSaveViewState(): Boolean {
Benoit Marty's avatar
Benoit Marty committed
95
        return false
96
97
98
99
    }

    override fun unbind(holder: Holder) {
        super.unbind(holder)
100
        holder.textInputEditText.removeTextChangedListener(onTextChangeListener)
101
102
103
104
105
    }

    class Holder : VectorEpoxyHolder() {
        val textInputLayout by bind<TextInputLayout>(R.id.formTextInputTextInputLayout)
        val textInputEditText by bind<TextInputEditText>(R.id.formTextInputTextInputEditText)
106
        val bottomSeparator by bind<View>(R.id.formTextInputDivider)
107
108
    }
}