Browse Source

fix to not set clipboard text repeatedly

initMessageInputView() is called repeatedly when capabilities are received. This is not a good solution but was done because capabilities are needed inside initMessageInputView().

As sharedText is set inside initMessageInputView(), this caused the bug that text was repeatedly inserted in to the message input field.

All in all, there is only the need to initialize initMessageInputView() once. With this fix, it is for now done when there is no filter set yet. As a long term solution, the architecture must be changed so that initMessageInputView() is not called repeatedly, but all UI elements that need capabilities etc are updated by livedata.

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe 1 year ago
parent
commit
cc823dcc0c
1 changed files with 67 additions and 65 deletions
  1. 67 65
      app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt

+ 67 - 65
app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt

@@ -1156,92 +1156,94 @@ class ChatActivity :
     }
     }
 
 
     private fun initMessageInputView() {
     private fun initMessageInputView() {
-        val filters = arrayOfNulls<InputFilter>(1)
-        val lengthFilter = CapabilitiesUtil.getMessageMaxLength(spreedCapabilities)
+        if (binding.messageInputView.inputEditText?.filters?.isEmpty() == true) {
+            val filters = arrayOfNulls<InputFilter>(1)
+            val lengthFilter = CapabilitiesUtil.getMessageMaxLength(spreedCapabilities)
 
 
-        filters[0] = InputFilter.LengthFilter(lengthFilter)
-        binding.messageInputView.inputEditText?.filters = filters
+            filters[0] = InputFilter.LengthFilter(lengthFilter)
+            binding.messageInputView.inputEditText?.filters = filters
 
 
-        binding.messageInputView.inputEditText?.addTextChangedListener(object : TextWatcher {
+            binding.messageInputView.inputEditText?.addTextChangedListener(object : TextWatcher {
 
 
-            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
-                // unused atm
-            }
+                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
+                    // unused atm
+                }
 
 
-            @Suppress("Detekt.TooGenericExceptionCaught")
-            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
-                updateOwnTypingStatus(s)
+                @Suppress("Detekt.TooGenericExceptionCaught")
+                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
+                    updateOwnTypingStatus(s)
 
 
-                if (s.length >= lengthFilter) {
-                    binding.messageInputView.inputEditText?.error = String.format(
-                        Objects.requireNonNull<Resources>(resources).getString(R.string.nc_limit_hit),
-                        lengthFilter.toString()
-                    )
-                } else {
-                    binding.messageInputView.inputEditText?.error = null
-                }
+                    if (s.length >= lengthFilter) {
+                        binding.messageInputView.inputEditText?.error = String.format(
+                            Objects.requireNonNull<Resources>(resources).getString(R.string.nc_limit_hit),
+                            lengthFilter.toString()
+                        )
+                    } else {
+                        binding.messageInputView.inputEditText?.error = null
+                    }
 
 
-                val editable = binding.messageInputView.inputEditText?.editableText
-                editedTextBehaviorSubject.onNext(editable.toString().trim())
+                    val editable = binding.messageInputView.inputEditText?.editableText
+                    editedTextBehaviorSubject.onNext(editable.toString().trim())
 
 
-                if (editable != null && binding.messageInputView.inputEditText != null) {
-                    val mentionSpans = editable.getSpans(
-                        0,
-                        binding.messageInputView.inputEditText!!.length(),
-                        Spans.MentionChipSpan::class.java
-                    )
-                    var mentionSpan: Spans.MentionChipSpan
-                    for (i in mentionSpans.indices) {
-                        mentionSpan = mentionSpans[i]
-                        if (start >= editable.getSpanStart(mentionSpan) &&
-                            start < editable.getSpanEnd(mentionSpan)
-                        ) {
-                            if (editable.subSequence(
-                                    editable.getSpanStart(mentionSpan),
-                                    editable.getSpanEnd(mentionSpan)
-                                ).toString().trim { it <= ' ' } != mentionSpan.label
+                    if (editable != null && binding.messageInputView.inputEditText != null) {
+                        val mentionSpans = editable.getSpans(
+                            0,
+                            binding.messageInputView.inputEditText!!.length(),
+                            Spans.MentionChipSpan::class.java
+                        )
+                        var mentionSpan: Spans.MentionChipSpan
+                        for (i in mentionSpans.indices) {
+                            mentionSpan = mentionSpans[i]
+                            if (start >= editable.getSpanStart(mentionSpan) &&
+                                start < editable.getSpanEnd(mentionSpan)
                             ) {
                             ) {
-                                editable.removeSpan(mentionSpan)
+                                if (editable.subSequence(
+                                        editable.getSpanStart(mentionSpan),
+                                        editable.getSpanEnd(mentionSpan)
+                                    ).toString().trim { it <= ' ' } != mentionSpan.label
+                                ) {
+                                    editable.removeSpan(mentionSpan)
+                                }
                             }
                             }
                         }
                         }
                     }
                     }
                 }
                 }
-            }
 
 
-            override fun afterTextChanged(s: Editable) {
-                // unused atm
-            }
-        })
+                override fun afterTextChanged(s: Editable) {
+                    // unused atm
+                }
+            })
 
 
-        // Image keyboard support
-        // See: https://developer.android.com/guide/topics/text/image-keyboard
+            // Image keyboard support
+            // See: https://developer.android.com/guide/topics/text/image-keyboard
 
 
-        (binding.messageInputView.inputEditText as ImageEmojiEditText).onCommitContentListener = {
-            uploadFile(it.toString(), false)
-        }
-        initVoiceRecordButton()
+            (binding.messageInputView.inputEditText as ImageEmojiEditText).onCommitContentListener = {
+                uploadFile(it.toString(), false)
+            }
+            initVoiceRecordButton()
 
 
-        if (sharedText.isNotEmpty()) {
-            binding.messageInputView.inputEditText?.setText(sharedText)
-        }
+            if (sharedText.isNotEmpty()) {
+                binding.messageInputView.inputEditText?.setText(sharedText)
+            }
 
 
-        binding.messageInputView.setAttachmentsListener {
-            AttachmentDialog(this, this).show()
-        }
+            binding.messageInputView.setAttachmentsListener {
+                AttachmentDialog(this, this).show()
+            }
 
 
-        binding.messageInputView.button?.setOnClickListener {
-            submitMessage(false)
-        }
+            binding.messageInputView.button?.setOnClickListener {
+                submitMessage(false)
+            }
 
 
-        if (CapabilitiesUtil.hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.SILENT_SEND)) {
-            binding.messageInputView.button?.setOnLongClickListener {
-                showSendButtonMenu()
-                true
+            if (CapabilitiesUtil.hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.SILENT_SEND)) {
+                binding.messageInputView.button?.setOnLongClickListener {
+                    showSendButtonMenu()
+                    true
+                }
             }
             }
-        }
 
 
-        binding.messageInputView.button?.contentDescription =
-            resources?.getString(R.string.nc_description_send_message_button)
+            binding.messageInputView.button?.contentDescription =
+                resources?.getString(R.string.nc_description_send_message_button)
+        }
     }
     }
 
 
     private fun editMessageAPI(message: ChatMessage, editedMessageText: String) {
     private fun editMessageAPI(message: ChatMessage, editedMessageText: String) {