RoomAliasEditItem.kt 3.1 KB
Newer Older
1
2
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
59
60
61
62
63
/*
 * 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.
 */

package im.vector.app.features.roomdirectory.createroom

import android.text.Editable
import android.view.View
import android.widget.TextView
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_room_alias_text_input)
abstract class RoomAliasEditItem : VectorEpoxyModel<RoomAliasEditItem.Holder>() {

    @EpoxyAttribute
    var value: String? = null

    @EpoxyAttribute
    var showBottomSeparator: Boolean = true

    @EpoxyAttribute
    var errorMessage: String? = null

    @EpoxyAttribute
    var homeServer: String? = null

    @EpoxyAttribute
    var enabled: Boolean = true

    @EpoxyAttribute
    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.error = errorMessage

Valere's avatar
Valere committed
64
65
66
67
68
69
        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)
        }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
        holder.textInputEditText.isEnabled = enabled
        holder.textInputEditText.addTextChangedListener(onTextChangeListener)
        holder.homeServerText.text = homeServer
        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.itemRoomAliasTextInputLayout)
        val textInputEditText by bind<TextInputEditText>(R.id.itemRoomAliasTextInputEditText)
        val homeServerText by bind<TextView>(R.id.itemRoomAliasHomeServer)
        val bottomSeparator by bind<View>(R.id.itemRoomAliasDivider)
    }
}