FormMultiLineEditTextItem.kt 3.45 KB
Newer Older
Valere's avatar
Valere committed
1
/*
Benoit Marty's avatar
Benoit Marty committed
2
 * Copyright (c) 2021 New Vector Ltd
Valere's avatar
Valere committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 *
 * 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.
 */

package im.vector.app.features.form

import android.graphics.Typeface
import android.text.Editable
import android.view.View
import androidx.core.view.isVisible
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
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

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

    @EpoxyAttribute
    var hint: String? = null

    @EpoxyAttribute
    var value: String? = null

    @EpoxyAttribute
    var showBottomSeparator: Boolean = true

    @EpoxyAttribute
    var errorMessage: String? = null

    @EpoxyAttribute
    var enabled: Boolean = true

    @EpoxyAttribute
    var textSizeSp: Int? = null

    @EpoxyAttribute
    var minLines: Int = 3

    @EpoxyAttribute
    var typeFace: Typeface = Typeface.DEFAULT

Valere's avatar
Valere committed
59
    @EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
Valere's avatar
Valere committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    var onTextChange: ((String) -> Unit)? = null

    private val onTextChangeListener = object : SimpleTextWatcher() {
        override fun afterTextChanged(s: Editable) {
            onTextChange?.invoke(s.toString())
        }
    }

    override fun bind(holder: Holder) {
        super.bind(holder)
        holder.textInputLayout.isEnabled = enabled
        holder.textInputLayout.hint = hint
        holder.textInputLayout.error = errorMessage

        holder.textInputEditText.typeface = typeFace
Valere's avatar
Valere committed
75
        holder.textInputEditText.textSize = textSizeSp?.toFloat() ?: 14f
Valere's avatar
Valere committed
76
77
78
        holder.textInputEditText.minLines = minLines

        // Update only if text is different and value is not null
Valere's avatar
Valere committed
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.
        } else {
            holder.textInputEditText.setText(value)
        }
Valere's avatar
Valere committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
        holder.textInputEditText.isEnabled = enabled

        holder.textInputEditText.addTextChangedListener(onTextChangeListener)
        holder.bottomSeparator.isVisible = showBottomSeparator
    }

    override fun shouldSaveViewState(): Boolean {
        return false
    }

    override fun unbind(holder: Holder) {
        super.unbind(holder)
        holder.textInputEditText.removeTextChangedListener(onTextChangeListener)
    }

    class Holder : VectorEpoxyHolder() {
        val textInputLayout by bind<TextInputLayout>(R.id.formMultiLineTextInputLayout)
        val textInputEditText by bind<TextInputEditText>(R.id.formMultiLineEditText)
        val bottomSeparator by bind<View>(R.id.formTextInputDivider)
    }
}