Skip to content

Commit 2b6a20f

Browse files
committed
Refactoring message drafts to use new source of truth fixing previous unsynchronized state bug
moving away from vanniktech emoji picker Signed-off-by: rapterjet2004 <juliuslinus1@gmail.com>
1 parent 5420da3 commit 2b6a20f

8 files changed

Lines changed: 322 additions & 92 deletions

File tree

app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4155,15 +4155,11 @@ class ChatActivity :
41554155
}
41564156

41574157
fun cancelReply() {
4158-
messageInputViewModel.reply(null)
4159-
chatViewModel.messageDraft.quotedMessageText = null
4160-
chatViewModel.messageDraft.quotedDisplayName = null
4161-
chatViewModel.messageDraft.quotedImageUrl = null
4162-
chatViewModel.messageDraft.quotedJsonId = null
4158+
messageInputViewModel.cancelReply()
41634159
}
41644160

41654161
fun cancelCreateThread() {
4166-
chatViewModel.clearThreadTitle()
4162+
messageInputViewModel.cancelCreateThread()
41674163
}
41684164

41694165
companion object {

app/src/main/java/com/nextcloud/talk/chat/MessageInputFragment.kt

Lines changed: 184 additions & 83 deletions
Large diffs are not rendered by default.

app/src/main/java/com/nextcloud/talk/chat/viewmodels/MessageInputViewModel.kt

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ import com.nextcloud.talk.chat.data.io.AudioRecorderManager
2323
import com.nextcloud.talk.chat.data.io.MediaPlayerManager
2424
import com.nextcloud.talk.chat.data.model.ChatMessage
2525
import com.nextcloud.talk.chat.data.network.ChatNetworkDataSource
26+
import com.nextcloud.talk.models.MessageDraft
2627
import com.nextcloud.talk.models.json.chat.ChatOverallSingleMessage
28+
import com.nextcloud.talk.models.json.chat.ChatUtils
2729
import com.nextcloud.talk.utils.message.SendMessageUtils
2830
import io.reactivex.disposables.Disposable
2931
import kotlinx.coroutines.flow.Flow
3032
import kotlinx.coroutines.flow.MutableStateFlow
3133
import kotlinx.coroutines.flow.StateFlow
34+
import kotlinx.coroutines.flow.update
3235
import kotlinx.coroutines.launch
3336
import javax.inject.Inject
3437

@@ -38,6 +41,18 @@ class MessageInputViewModel :
3841
ViewModel(),
3942
DefaultLifecycleObserver {
4043

44+
data class MessageInputState(
45+
val messageText: String = "",
46+
val messageCursor: Int = 0,
47+
val quotedJsonId: Int? = null,
48+
val quotedDisplayName: String? = null,
49+
val quotedMessageText: String? = null,
50+
val quotedImageUrl: String? = null,
51+
val threadTitle: String? = null,
52+
val editMessage: ChatMessage? = null,
53+
val isThreadCreationInProgress: Boolean = false
54+
)
55+
4156
enum class LifeCycleFlag {
4257
PAUSED,
4358
RESUMED,
@@ -234,14 +249,95 @@ class MessageInputViewModel :
234249
}
235250
}
236251

252+
private val _inputState = MutableStateFlow(MessageInputState())
253+
val inputState: StateFlow<MessageInputState> = _inputState
254+
255+
fun updateMessageText(text: String) {
256+
_inputState.update { it.copy(messageText = text) }
257+
}
258+
259+
fun updateMessageCursor(cursor: Int) {
260+
_inputState.update { it.copy(messageCursor = cursor) }
261+
}
262+
263+
fun updateThreadTitle(title: String) {
264+
_inputState.update { it.copy(threadTitle = title) }
265+
}
266+
267+
fun initFromDraft(draft: MessageDraft) {
268+
_inputState.update {
269+
it.copy(
270+
messageText = draft.messageText,
271+
messageCursor = draft.messageCursor,
272+
quotedJsonId = draft.quotedJsonId,
273+
quotedDisplayName = draft.quotedDisplayName,
274+
quotedMessageText = draft.quotedMessageText,
275+
quotedImageUrl = draft.quotedImageUrl,
276+
threadTitle = draft.threadTitle,
277+
isThreadCreationInProgress = false
278+
)
279+
}
280+
}
281+
282+
fun toDraft(): MessageDraft =
283+
MessageDraft(
284+
messageText = _inputState.value.messageText,
285+
messageCursor = _inputState.value.messageCursor,
286+
quotedJsonId = _inputState.value.quotedJsonId,
287+
quotedDisplayName = _inputState.value.quotedDisplayName,
288+
quotedMessageText = _inputState.value.quotedMessageText,
289+
quotedImageUrl = _inputState.value.quotedImageUrl,
290+
threadTitle = _inputState.value.threadTitle
291+
)
292+
237293
fun reply(message: ChatMessage?) {
294+
_inputState.update {
295+
it.copy(
296+
quotedJsonId = message?.jsonMessageId,
297+
quotedDisplayName = message?.actorDisplayName,
298+
quotedMessageText = message?.getRichText(),
299+
quotedImageUrl = "" // TODO
300+
)
301+
}
238302
_getReplyChatMessage.postValue(message)
239303
}
240304

305+
fun cancelReply() {
306+
_inputState.update {
307+
it.copy(
308+
quotedJsonId = null,
309+
quotedDisplayName = null,
310+
quotedMessageText = null,
311+
quotedImageUrl = null
312+
)
313+
}
314+
_getReplyChatMessage.postValue(null)
315+
}
316+
241317
fun edit(message: ChatMessage?) {
318+
val text = message?.let {
319+
ChatUtils.getParsedMessage(it.message, it.messageParameters)
320+
} ?: ""
321+
_inputState.update {
322+
it.copy(
323+
editMessage = message,
324+
messageText = text,
325+
messageCursor = text.length
326+
)
327+
}
242328
_getEditChatMessage.value = message
243329
}
244330

331+
fun cancelEdit() {
332+
_inputState.update { it.copy(editMessage = null) }
333+
_getEditChatMessage.value = null
334+
}
335+
336+
fun cancelCreateThread() {
337+
_inputState.update { it.copy(threadTitle = "") }
338+
stopThreadCreation()
339+
}
340+
245341
fun startMicInput(context: Context) {
246342
audioFocusRequestManager.audioFocusRequest(true) {
247343
audioRecorderManager.start(context)
@@ -284,10 +380,12 @@ class MessageInputViewModel :
284380
}
285381

286382
fun startThreadCreation() {
383+
_inputState.update { it.copy(isThreadCreationInProgress = true) }
287384
_createThreadViewState.postValue(CreateThreadEditState())
288385
}
289386

290387
fun stopThreadCreation() {
388+
_inputState.update { it.copy(isThreadCreationInProgress = false) }
291389
_createThreadViewState.postValue(CreateThreadStartState)
292390
}
293391

app/src/main/java/com/nextcloud/talk/ui/MessageInput.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class MessageInput : FrameLayout {
2626
lateinit var slideToCancelDescription: TextView
2727
lateinit var microphoneEnabledInfo: ImageView
2828
lateinit var microphoneEnabledInfoBackground: ImageView
29+
lateinit var smileyButton: ImageButton
2930
lateinit var deleteVoiceRecording: ImageView
3031
lateinit var sendVoiceRecording: ImageView
3132
lateinit var micInputCloud: MicInputCloud
@@ -70,6 +71,7 @@ class MessageInput : FrameLayout {
7071
slideToCancelDescription = findViewById(R.id.slideToCancelDescription)
7172
microphoneEnabledInfo = findViewById(R.id.microphoneEnabledInfo)
7273
microphoneEnabledInfoBackground = findViewById(R.id.microphoneEnabledInfoBackground)
74+
smileyButton = findViewById(R.id.smileyButton)
7375
deleteVoiceRecording = findViewById(R.id.deleteVoiceRecording)
7476
sendVoiceRecording = findViewById(R.id.sendVoiceRecording)
7577
micInputCloud = findViewById(R.id.micInputCloud)

app/src/main/java/com/nextcloud/talk/ui/theme/TalkSpecificViewThemeUtils.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import androidx.core.content.res.ResourcesCompat
3535
import androidx.core.graphics.ColorUtils
3636
import androidx.core.graphics.drawable.DrawableCompat
3737
import androidx.core.view.ViewCompat
38+
import androidx.emoji2.emojipicker.EmojiPickerView
3839
import com.google.android.material.button.MaterialButton
3940
import com.google.android.material.card.MaterialCardView
4041
import com.google.android.material.chip.Chip
@@ -492,6 +493,18 @@ class TalkSpecificViewThemeUtils @Inject constructor(
492493
}
493494
}
494495

496+
fun themeEmojiPicker(emojiPickerView: EmojiPickerView) {
497+
withScheme(emojiPickerView.context) { scheme ->
498+
emojiPickerView.setBackgroundColor(dynamicColor.surfaceContainerLow().getArgb(scheme))
499+
themeEmojiPickerCategoryTabs(
500+
emojiPickerView,
501+
dynamicColor.primary().getArgb(scheme),
502+
dynamicColor.onSurfaceVariant().getArgb(scheme)
503+
)
504+
protectEmojiPickerScrollGesture(emojiPickerView)
505+
}
506+
}
507+
495508
companion object {
496509
private val THEMEABLE_PLACEHOLDER_IDS = listOf(
497510
R.drawable.ic_mimetype_package_x_generic,

app/src/main/res/layout/create_thread_view.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
android:id="@+id/createThreadView"
1111
android:layout_width="match_parent"
1212
android:layout_height="match_parent"
13-
android:orientation="horizontal">
13+
android:orientation="horizontal"
14+
android:visibility="gone">
1415

1516
<LinearLayout
1617
android:layout_width="0dp"

app/src/main/res/layout/fragment_message_input.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
android:textColor="@color/medium_emphasis_text"
8282
android:visibility="gone"
8383
tools:visibility="visible" />
84+
85+
<androidx.emoji2.emojipicker.EmojiPickerView
86+
android:id="@+id/emoji_picker"
87+
android:layout_width="match_parent"
88+
android:layout_height="250dp"
89+
android:visibility="gone" />
8490
</LinearLayout>
8591
</com.google.android.material.card.MaterialCardView>
8692
</LinearLayout>

app/src/main/res/layout/view_message_input.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,25 @@
4444
android:src="@drawable/ic_baseline_attach_file_24"
4545
app:tint="?attr/colorControlNormal" />
4646

47+
<ImageButton
48+
android:id="@+id/smileyButton"
49+
android:layout_width="@dimen/min_size_clickable_area"
50+
android:layout_height="@dimen/min_size_clickable_area"
51+
android:layout_alignBottom="@id/messageInput"
52+
android:layout_toEndOf="@id/attachmentButton"
53+
android:background="@color/transparent"
54+
android:contentDescription="@string/nc_add_emojis"
55+
android:gravity="bottom"
56+
android:src="@drawable/ic_insert_emoticon_black_24dp"
57+
app:tint="?attr/colorControlNormal" />
58+
4759
<com.nextcloud.talk.utils.ImageEmojiEditText
4860
android:id="@+id/messageInput"
4961
android:layout_width="match_parent"
5062
android:layout_height="wrap_content"
5163
android:layout_centerHorizontal="true"
5264
android:layout_marginEnd="48dp"
53-
android:layout_toEndOf="@id/attachmentButton"
65+
android:layout_toEndOf="@id/smileyButton"
5466
android:background="@null"
5567
android:imeOptions="actionDone"
5668
android:inputType="textAutoCorrect|textMultiLine|textCapSentences"
@@ -237,7 +249,8 @@
237249
android:background="@color/transparent"
238250
android:contentDescription="@string/start_thread"
239251
android:src="@drawable/outline_forum_24"
240-
app:tint="?attr/colorControlNormal" />
252+
app:tint="?attr/colorControlNormal"
253+
android:visibility="gone" />
241254

242255
<ImageButton
243256
android:id="@+id/editMessageButton"

0 commit comments

Comments
 (0)