Edit inputText view refactoring

Signed-off-by: Sowjanya Kota <101803542+sowjanyakch@users.noreply.github.com>
Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-01-25 17:47:21 +01:00
parent 337f07abfe
commit 33de105048
5 changed files with 95 additions and 132 deletions

View File

@ -303,6 +303,8 @@ class ChatActivity :
lateinit var chatViewModel: ChatViewModel lateinit var chatViewModel: ChatViewModel
val editableBehaviorSubject = BehaviorSubject.createDefault(false) val editableBehaviorSubject = BehaviorSubject.createDefault(false)
val editedTextBehaviorSubject = BehaviorSubject.createDefault("")
private lateinit var editMessage: ChatMessage
override val view: View override val view: View
get() = binding.root get() = binding.root
@ -353,9 +355,6 @@ class ChatActivity :
private var recorder: MediaRecorder? = null private var recorder: MediaRecorder? = null
private lateinit var originalMessage:ChatMessage
private enum class MediaRecorderState { private enum class MediaRecorderState {
INITIAL, INITIAL,
INITIALIZED, INITIALIZED,
@ -753,7 +752,6 @@ class ChatActivity :
}) })
initMessageInputView() initMessageInputView()
loadAvatarForStatusBar() loadAvatarForStatusBar()
setActionBarTitle() setActionBarTitle()
@ -764,18 +762,20 @@ class ChatActivity :
val filters = arrayOfNulls<InputFilter>(1) val filters = arrayOfNulls<InputFilter>(1)
val lengthFilter = CapabilitiesUtilNew.getMessageMaxLength(conversationUser) val lengthFilter = CapabilitiesUtilNew.getMessageMaxLength(conversationUser)
if(!editableBehaviorSubject.value!!){ if (editableBehaviorSubject.value!!) {
binding.messageInputView.editMessageButton.visibility = View.GONE val editableText = Editable.Factory.getInstance().newEditable(editMessage.message)
binding.messageInputView.messageSendButton.visibility = View.VISIBLE binding.messageInputView.inputEditText.text = editableText
binding.messageInputView.inputEditText.setSelection(editableText.length)
} }
filters[0] = InputFilter.LengthFilter(lengthFilter) filters[0] = InputFilter.LengthFilter(lengthFilter)
binding.messageInputView.inputEditText?.filters = filters 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) { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
// unused atm // unused atm
} }
@Suppress("Detekt.TooGenericExceptionCaught") @Suppress("Detekt.TooGenericExceptionCaught")
@ -792,6 +792,7 @@ class ChatActivity :
} }
val editable = binding.messageInputView.inputEditText?.editableText val editable = binding.messageInputView.inputEditText?.editableText
editedTextBehaviorSubject.onNext(editable.toString().trim())
if (editable != null && binding.messageInputView.inputEditText != null) { if (editable != null && binding.messageInputView.inputEditText != null) {
val mentionSpans = editable.getSpans( val mentionSpans = editable.getSpans(
@ -815,7 +816,6 @@ class ChatActivity :
} }
} }
} }
} }
override fun afterTextChanged(s: Editable) { override fun afterTextChanged(s: Editable) {
@ -825,119 +825,53 @@ class ChatActivity :
// Image keyboard support // Image keyboard support
// See: https://developer.android.com/guide/topics/text/image-keyboard // 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)
if (sharedText.isNotEmpty()) { }
binding.messageInputView.inputEditText?.setText(sharedText) initVoiceRecordButton()
} if (editableBehaviorSubject.value!!) {
binding.messageInputView.setAttachmentsListener {
AttachmentDialog(this, this).show()
}
binding.messageInputView.button?.setOnClickListener {
submitMessage(false)
}
if (CapabilitiesUtilNew.hasSpreedFeatureCapability(conversationUser, "silent-send")) {
binding.messageInputView.button?.setOnLongClickListener {
showSendButtonMenu()
true
}
}
binding.messageInputView.button?.contentDescription =
resources?.getString(R.string.nc_description_send_message_button)
}
private fun editMessageInputView(message:ChatMessage) {
editableBehaviorSubject.onNext(true)
val filters = arrayOfNulls<InputFilter>(1)
val lengthFilter = CapabilitiesUtilNew.getMessageMaxLength(conversationUser)
var editText = ""
filters[0] = InputFilter.LengthFilter(lengthFilter)
binding.messageInputView.inputEditText?.filters = filters
val editableText = Editable.Factory.getInstance().newEditable(message.message)
binding.messageInputView.inputEditText.text = editableText
if(editableBehaviorSubject.value!!){
binding.messageInputView.editMessageButton.visibility = View.VISIBLE
binding.messageInputView.messageSendButton.visibility = View.GONE binding.messageInputView.messageSendButton.visibility = View.GONE
binding.messageInputView.button.visibility = GONE binding.messageInputView.recordAudioButton.visibility = View.GONE
binding.messageInputView.editMessageButton.visibility = View.VISIBLE
binding.messageInputView.clearEditMessage.visibility = View.VISIBLE
} }
binding.messageInputView.inputEditText?.addTextChangedListener(object : TextWatcher { if (sharedText.isNotEmpty()) {
binding.messageInputView.inputEditText?.setText(sharedText)
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)
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
editText = editable.toString()
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
) {
editable.removeSpan(mentionSpan)
}
}
}
}
}
override fun afterTextChanged(s: Editable) {
// unused atm
binding.messageInputView.messageSendButton.visibility = GONE
}
})
binding.messageInputView.setAttachmentsListener { binding.messageInputView.setAttachmentsListener {
AttachmentDialog(this, this).show() AttachmentDialog(this, this).show()
} }
binding.messageInputView.editMessageButton.setOnClickListener { binding.messageInputView.button?.setOnClickListener {
editMessageAPI(message, editedMessage = editText) submitMessage(false)
} }
binding.messageInputView.editMessageButton.setOnClickListener {
if(editMessage.message == editedTextBehaviorSubject.value!!){
clearEditUI()
return@setOnClickListener
}
editMessageAPI(editMessage, editedMessageText = editedTextBehaviorSubject.value!!)
}
binding.messageInputView.clearEditMessage.setOnClickListener {
clearEditUI()
}
if (CapabilitiesUtilNew.hasSpreedFeatureCapability(conversationUser, "silent-send")) {
binding.messageInputView.button?.setOnLongClickListener {
showSendButtonMenu()
true
}
}
binding.messageInputView.button?.contentDescription =
resources?.getString(R.string.nc_description_send_message_button)
} }
private fun editMessageAPI(message:ChatMessage, editedMessage:String){ private fun editMessageAPI(message: ChatMessage, editedMessageText: String) {
var apiVersion = 1 var apiVersion = 1
// FIXME Fix API checking with guests? // FIXME Fix API checking with guests?
if (conversationUser != null) { if (conversationUser != null) {
@ -951,7 +885,7 @@ class ChatActivity :
conversationUser?.baseUrl, conversationUser?.baseUrl,
roomToken, roomToken,
message?.id message?.id
),editedMessage ), editedMessageText
)?.subscribeOn(Schedulers.io()) )?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread()) ?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(object : Observer<ChatOCSSingleMessage> { ?.subscribe(object : Observer<ChatOCSSingleMessage> {
@ -964,16 +898,20 @@ class ChatActivity :
} }
override fun onError(e: Throwable) { override fun onError(e: Throwable) {
} }
override fun onComplete() { override fun onComplete() {
binding.messageInputView.editMessageButton.visibility = GONE clearEditUI()
binding.messageInputView.messageSendButton.visibility = View.VISIBLE
editableBehaviorSubject.onNext(false)
binding.messageInputView.inputEditText.setText("")
} }
}) })
// remove last item from list
}
private fun clearEditUI() {
binding.messageInputView.editMessageButton.visibility = GONE
binding.messageInputView.clearEditMessage.visibility = View.GONE
editableBehaviorSubject.onNext(false)
binding.messageInputView.inputEditText.setText("")
} }
private fun themeMessageInputView() { private fun themeMessageInputView() {
@ -1202,10 +1140,12 @@ class ChatActivity :
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
private fun initVoiceRecordButton() { private fun initVoiceRecordButton() {
if (!isVoiceRecordingLocked) { if (!isVoiceRecordingLocked) {
if (binding.messageInputView.messageInput.text!!.isNotEmpty()) { if (!editableBehaviorSubject.value!!) {
showMicrophoneButton(false) if (binding.messageInputView.messageInput.text!!.isNotEmpty()) {
} else { showMicrophoneButton(false)
showMicrophoneButton(true) } else {
showMicrophoneButton(true)
}
} }
} else if (mediaRecorderState == MediaRecorderState.RECORDING) { } else if (mediaRecorderState == MediaRecorderState.RECORDING) {
binding.messageInputView.playPauseBtn.visibility = View.GONE binding.messageInputView.playPauseBtn.visibility = View.GONE
@ -1219,10 +1159,12 @@ class ChatActivity :
isVoicePreviewPlaying = false isVoicePreviewPlaying = false
binding.messageInputView.messageInput.doAfterTextChanged { binding.messageInputView.messageInput.doAfterTextChanged {
if (binding.messageInputView.messageInput.text?.isEmpty() == true) { if (!editableBehaviorSubject.value!!) {
showMicrophoneButton(true) if (binding.messageInputView.messageInput.text?.isEmpty() == true) {
} else { showMicrophoneButton(true)
showMicrophoneButton(false) } else {
showMicrophoneButton(false)
}
} }
} }
@ -4381,6 +4323,7 @@ class ChatActivity :
} }
private fun showMicrophoneButton(show: Boolean) { private fun showMicrophoneButton(show: Boolean) {
if (show && CapabilitiesUtilNew.hasSpreedFeatureCapability(conversationUser, "voice-message-sharing")) { if (show && CapabilitiesUtilNew.hasSpreedFeatureCapability(conversationUser, "voice-message-sharing")) {
Log.d(TAG, "Microphone shown") Log.d(TAG, "Microphone shown")
binding.messageInputView.messageSendButton.visibility = View.GONE binding.messageInputView.messageSendButton.visibility = View.GONE
@ -4675,13 +4618,10 @@ class ChatActivity :
startActivity(shareIntent) startActivity(shareIntent)
} }
fun editMessage(message: ChatMessage) { fun editMessage(message: ChatMessage) {
editableBehaviorSubject.onNext(true)
editMessageInputView(message) editMessage = message
initMessageInputView()
} }
companion object { companion object {

View File

@ -44,6 +44,7 @@ class MessageInput : MessageInput {
lateinit var micInputCloud: MicInputCloud lateinit var micInputCloud: MicInputCloud
lateinit var playPauseBtn: MaterialButton lateinit var playPauseBtn: MaterialButton
lateinit var editMessageButton:ImageButton lateinit var editMessageButton:ImageButton
lateinit var clearEditMessage:ImageButton
lateinit var seekBar: SeekBar lateinit var seekBar: SeekBar
constructor(context: Context?) : super(context) { constructor(context: Context?) : super(context) {
@ -71,6 +72,7 @@ class MessageInput : MessageInput {
playPauseBtn = findViewById(R.id.playPauseBtn) playPauseBtn = findViewById(R.id.playPauseBtn)
seekBar = findViewById(R.id.seekbar) seekBar = findViewById(R.id.seekbar)
editMessageButton = findViewById(R.id.editMessageButton) editMessageButton = findViewById(R.id.editMessageButton)
clearEditMessage = findViewById(R.id.clearEditButton)
} }
var messageInput: EmojiEditText var messageInput: EmojiEditText

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>

View File

@ -230,7 +230,6 @@
android:contentDescription="@string/nc_description_send_message_button" /> android:contentDescription="@string/nc_description_send_message_button" />
<ImageButton <ImageButton
android:id="@+id/recordAudioButton" android:id="@+id/recordAudioButton"
android:layout_width="48dp" android:layout_width="48dp"
@ -250,8 +249,23 @@
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:background="@color/transparent" android:background="@color/transparent"
android:src="@drawable/ic_check_24" android:src="@drawable/ic_check_24"
android:visibility = "visible" android:visibility = "gone"
android:contentDescription="@string/nc_description_record_voice" /> tools:visibility = "visible"
android:contentDescription="@string/nc_send_edit_message" />
<ImageButton
android:id="@+id/clearEditButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_below="@id/quotedChatMessageView"
android:layout_alignParentEnd="true"
android:layout_marginEnd="48dp"
android:background="@color/transparent"
android:contentDescription="@string/nc_clear_edit_message"
android:src="@drawable/ic_clear_24"
android:visibility="gone"
tools:visibility = "visible"
app:layout_constraintEnd_toStartOf="@id/editMessageButton" />
<Space <Space
android:id="@id/attachmentButtonSpace" android:id="@id/attachmentButtonSpace"

View File

@ -788,4 +788,6 @@ How to translate with transifex:
<string name="languages_error_message">Languages could not be retrieved</string> <string name="languages_error_message">Languages could not be retrieved</string>
<string name="edit_message_icon_description">Edit Message Icon</string> <string name="edit_message_icon_description">Edit Message Icon</string>
<string name="nc_edit_message">Edit message</string> <string name="nc_edit_message">Edit message</string>
<string name="nc_send_edit_message">Send Edit Message</string>
<string name="nc_clear_edit_message">Clear Edit Message</string>
</resources> </resources>