/
DefaultMessageComposerLeadingContent.kt
161 lines (140 loc) · 6.82 KB
/
DefaultMessageComposerLeadingContent.kt
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (c) 2014-2022 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
*
* 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 io.getstream.chat.android.ui.feature.messages.composer.content
import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
import androidx.core.view.isVisible
import io.getstream.chat.android.models.ChannelCapabilities
import io.getstream.chat.android.ui.R
import io.getstream.chat.android.ui.common.internal.getColorList
import io.getstream.chat.android.ui.common.state.messages.Edit
import io.getstream.chat.android.ui.common.state.messages.composer.MessageComposerState
import io.getstream.chat.android.ui.common.state.messages.composer.RecordingState
import io.getstream.chat.android.ui.databinding.StreamUiMessageComposerDefaultLeadingContentBinding
import io.getstream.chat.android.ui.feature.messages.composer.MessageComposerContext
import io.getstream.chat.android.ui.feature.messages.composer.MessageComposerView
import io.getstream.chat.android.ui.feature.messages.composer.MessageComposerViewStyle
import io.getstream.chat.android.ui.utils.extensions.createStreamThemeWrapper
import io.getstream.chat.android.ui.utils.extensions.getColorCompat
import io.getstream.chat.android.ui.utils.extensions.setBorderlessRipple
import io.getstream.chat.android.ui.utils.extensions.streamThemeInflater
/**
* Represents the content shown at the start of [MessageComposerView].
*/
public interface MessageComposerLeadingContent : MessageComposerContent {
/**
* Click listener for the pick attachments button.
*/
public var attachmentsButtonClickListener: (() -> Unit)?
/**
* Click listener for the pick commands button.
*/
public var commandsButtonClickListener: (() -> Unit)?
}
/**
* Represents the default content shown at the start of [MessageComposerView].
*/
public open class DefaultMessageComposerLeadingContent : FrameLayout, MessageComposerLeadingContent {
/**
* Generated binding class for the XML layout.
*/
protected lateinit var binding: StreamUiMessageComposerDefaultLeadingContentBinding
/**
* The style for [MessageComposerView].
*/
protected lateinit var style: MessageComposerViewStyle
/**
* Click listener for the pick attachments button.
*/
public override var attachmentsButtonClickListener: (() -> Unit)? = null
/**
* Click listener for the pick commands button.
*/
public override var commandsButtonClickListener: (() -> Unit)? = null
public constructor(context: Context) : this(context, null)
public constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
public constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context.createStreamThemeWrapper(),
attrs,
defStyleAttr,
) {
init()
}
/**
* Initializes the initial layout of the view.
*/
private fun init() {
binding = StreamUiMessageComposerDefaultLeadingContentBinding.inflate(streamThemeInflater, this)
binding.attachmentsButton.setOnClickListener { attachmentsButtonClickListener?.invoke() }
binding.commandsButton.setOnClickListener { commandsButtonClickListener?.invoke() }
}
/**
* Initializes the content view with [MessageComposerContext].
*
* @param messageComposerContext The context of this [MessageComposerView].
*/
override fun attachContext(messageComposerContext: MessageComposerContext) {
this.style = messageComposerContext.style
binding.commandsButton.isVisible = style.commandsButtonVisible
binding.attachmentsButton.setImageDrawable(style.attachmentsButtonIconDrawable)
binding.attachmentsButton.setBorderlessRipple(style.attachmentsButtonRippleColor)
binding.attachmentsButton.isVisible = style.attachmentsButtonVisible
binding.commandsButton.setImageDrawable(style.commandsButtonIconDrawable)
binding.commandsButton.setBorderlessRipple(style.commandsButtonRippleColor)
val getStateListColor = { tintColor: Int ->
getColorList(
normalColor = context.getColorCompat(R.color.stream_ui_grey),
selectedColor = tintColor,
disabledColor = context.getColorCompat(R.color.stream_ui_grey_gainsboro),
)
}
style.attachmentsButtonIconTintList?.also { tintList ->
binding.attachmentsButton.imageTintList = tintList
} ?: style.buttonIconDrawableTintColor?.let { tintColor ->
binding.attachmentsButton.imageTintList = getStateListColor(tintColor)
}
style.commandsButtonIconTintList?.also { tintList ->
binding.commandsButton.imageTintList = tintList
} ?: style.buttonIconDrawableTintColor?.let { tintColor ->
binding.commandsButton.imageTintList = getStateListColor(tintColor)
}
}
/**
* Invoked when the state has changed and the UI needs to be updated accordingly.
*
* @param state The state that will be used to render the updated UI.
*/
override fun renderState(state: MessageComposerState) {
val canSendMessage = state.ownCapabilities.contains(ChannelCapabilities.SEND_MESSAGE)
val canUploadFile = state.ownCapabilities.contains(ChannelCapabilities.UPLOAD_FILE)
val hasTextInput = state.inputValue.isNotEmpty()
val hasAttachments = state.attachments.isNotEmpty()
val hasCommandInput = state.inputValue.startsWith("/")
val hasCommandSuggestions = state.commandSuggestions.isNotEmpty()
val hasMentionSuggestions = state.mentionSuggestions.isNotEmpty()
val isInEditMode = state.action is Edit
val hasCommands = state.hasCommands
val noRecording = state.recording is RecordingState.Idle
binding.root.isVisible = noRecording
// isVisible = noRecording
binding.attachmentsButton.isEnabled = !hasCommandInput && !hasCommandSuggestions && !hasMentionSuggestions
binding.attachmentsButton.isVisible = style.attachmentsButtonVisible && canSendMessage && canUploadFile && !isInEditMode
binding.commandsButton.isEnabled = !hasTextInput && !hasAttachments
binding.commandsButton.isVisible = style.commandsButtonVisible && canSendMessage && !isInEditMode && hasCommands
binding.commandsButton.isSelected = hasCommandSuggestions
}
}