From afd29ac03ce4dce8a5b571e2538a422adae57cec Mon Sep 17 00:00:00 2001 From: rapterjet2004 Date: Wed, 25 Sep 2024 07:46:46 -0500 Subject: [PATCH] should have fixed the UI now with lifecycle and error handling Signed-off-by: rapterjet2004 --- .../com/nextcloud/talk/chat/ChatActivity.kt | 35 +++++++++++----- .../talk/chat/MessageInputFragment.kt | 7 ++-- .../network/OfflineFirstChatRepository.kt | 40 ++++++++++--------- .../chat/viewmodels/MessageInputViewModel.kt | 19 +++++++-- 4 files changed, 66 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 8428264d2..a322eba53 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -535,14 +535,30 @@ class ChatActivity : private fun initObservers() { Log.d(TAG, "initObservers Called") - messageInputViewModel.messageQueueFlow.observe(this) { message -> - // TODO shouldn't be able save state - val temporaryChatMessage = ChatMessage() - temporaryChatMessage.jsonMessageId = -3 - temporaryChatMessage.actorId = "-3" - temporaryChatMessage.timestamp = (adapter?.items?.get(0)?.item as ChatMessage).timestamp - temporaryChatMessage.message = message - adapter?.addToStart(temporaryChatMessage, true) + messageInputViewModel.messageQueueFlow.observe(this) { list -> + for (message in list) { + Log.d("Julius", "Message recieved: $message") + val temporaryChatMessage = ChatMessage() + temporaryChatMessage.jsonMessageId = -3 + temporaryChatMessage.actorId = "-3" + temporaryChatMessage.timestamp = System.currentTimeMillis() / 1000 + temporaryChatMessage.message = message + adapter?.addToStart(temporaryChatMessage, true) + } + } + + messageInputViewModel.messageQueueSizeFlow.observe(this) { size -> + if (size == 0) { + var i = 0 + var pos = adapter?.getMessagePositionById("-3") + while (pos != null && pos > -1) { + adapter?.items?.removeAt(pos) + i++ + pos = adapter?.getMessagePositionById("-3") + } + Log.d("Julius", "End i: $i") + } + } this.lifecycleScope.launch { @@ -632,6 +648,7 @@ class ChatActivity : withUrl = urlForChatting ) } + messageInputViewModel.getTempMessagesFromMessageQueue(roomToken) } is ChatViewModel.GetCapabilitiesErrorState -> { @@ -2352,8 +2369,8 @@ class ChatActivity : try { EmojiCompat.get().process(currentConversation?.displayName as CharSequence).toString() } catch (e: java.lang.IllegalStateException) { + Log.e(TAG, "setActionBarTitle failed $e") currentConversation?.displayName - error(e) } } else { "" diff --git a/app/src/main/java/com/nextcloud/talk/chat/MessageInputFragment.kt b/app/src/main/java/com/nextcloud/talk/chat/MessageInputFragment.kt index 03858b4b5..085dd8fe5 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/MessageInputFragment.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/MessageInputFragment.kt @@ -73,7 +73,6 @@ import com.nextcloud.talk.utils.text.Spans import com.otaliastudios.autocomplete.Autocomplete import com.stfalcon.chatkit.commons.models.IMessage import com.vanniktech.emoji.EmojiPopup -import kotlinx.coroutines.delay import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch @@ -184,13 +183,15 @@ class MessageInputFragment : Fragment() { val connectionGained = (!wasOnline && isOnline) wasOnline = !binding.fragmentMessageInputView.isShown Log.d(TAG, "isOnline: $isOnline\nwasOnline: $wasOnline\nconnectionGained: $connectionGained") - delay(500) + // delay(2000) handleMessageQueue(isOnline) handleUI(isOnline, connectionGained) }.collect() } chatActivity.messageInputViewModel.messageQueueSizeFlow.observe(viewLifecycleOwner) { size -> + Log.d("Julius", "MessageQueueSizeFlow recieved: $size") + if (size > 0) { binding.fragmentConnectionLost.text = getString(R.string.connection_lost_queued, size) } else { @@ -233,7 +234,7 @@ class MessageInputFragment : Fragment() { binding.fragmentConnectionLost.clearAnimation() binding.fragmentConnectionLost.visibility = View.GONE binding.fragmentConnectionLost.setBackgroundColor(resources.getColor(R.color.hwSecurityRed)) - binding.fragmentConnectionLost.text = getString(R.string.connection_lost_sent_messages_are_queued) + // binding.fragmentConnectionLost.text = getString(R.string.connection_lost_sent_messages_are_queued) binding.fragmentConnectionLost.visibility = View.VISIBLE binding.fragmentMessageInputView.attachmentButton.isEnabled = false binding.fragmentMessageInputView.recordAudioButton.isEnabled = false diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt index 4c52f7f31..15ab13cdc 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt @@ -38,6 +38,8 @@ import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch +import java.lang.Thread.sleep +import java.util.concurrent.TimeUnit import javax.inject.Inject class OfflineFirstChatRepository @Inject constructor( @@ -390,29 +392,29 @@ class OfflineFirstChatRepository @Inject constructor( Integer.parseInt(it) } - return@map Pair( - HTTP_CODE_OK, - (it.body() as ChatOverall).ocs!!.data!! - ) - } + return@map Pair( + HTTP_CODE_OK, + (it.body() as ChatOverall).ocs!!.data!! + ) + } - HTTP_CODE_NOT_MODIFIED -> { - Log.d(TAG, "getMessagesFromServer HTTP_CODE_NOT_MODIFIED") + HTTP_CODE_NOT_MODIFIED -> { + Log.d(TAG, "getMessagesFromServer HTTP_CODE_NOT_MODIFIED") - return@map Pair( - HTTP_CODE_NOT_MODIFIED, - listOf() - ) - } + return@map Pair( + HTTP_CODE_NOT_MODIFIED, + listOf() + ) + } - HTTP_CODE_PRECONDITION_FAILED -> { - Log.d(TAG, "getMessagesFromServer HTTP_CODE_PRECONDITION_FAILED") + HTTP_CODE_PRECONDITION_FAILED -> { + Log.d(TAG, "getMessagesFromServer HTTP_CODE_PRECONDITION_FAILED") - return@map Pair( - HTTP_CODE_PRECONDITION_FAILED, - listOf() - ) - } + return@map Pair( + HTTP_CODE_PRECONDITION_FAILED, + listOf() + ) + } else -> { return@map Pair( diff --git a/app/src/main/java/com/nextcloud/talk/chat/viewmodels/MessageInputViewModel.kt b/app/src/main/java/com/nextcloud/talk/chat/viewmodels/MessageInputViewModel.kt index 41f69a0f3..9c408724e 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/viewmodels/MessageInputViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/viewmodels/MessageInputViewModel.kt @@ -124,8 +124,8 @@ class MessageInputViewModel @Inject constructor( val messageQueueSizeFlow: LiveData get() = _messageQueueSizeFlow.asLiveData() - private val _messageQueueFlow: MutableLiveData = MutableLiveData() - val messageQueueFlow: LiveData + private val _messageQueueFlow: MutableLiveData> = MutableLiveData() + val messageQueueFlow: LiveData> get() = _messageQueueFlow @Suppress("LongParameterList") @@ -142,7 +142,7 @@ class MessageInputViewModel @Inject constructor( messageQueue.add(QueuedMessage(message, displayName, replyTo, sendWithoutNotification)) dataStore.saveMessageQueue(roomToken, messageQueue) _messageQueueSizeFlow.update { messageQueue.size } - _messageQueueFlow.postValue(message.toString()) + _messageQueueFlow.postValue(listOf(message.toString())) return } @@ -266,6 +266,17 @@ class MessageInputViewModel @Inject constructor( msg.sendWithoutNotification!! ) } + _messageQueueSizeFlow.tryEmit(0) + } + + fun getTempMessagesFromMessageQueue(roomToken: String) { + val queue = dataStore.getMessageQueue(roomToken) + val list = mutableListOf() + for (msg in queue) { + Log.d("Julius", "Msg: ${msg.message}") + list.add(msg.message.toString()) + } + _messageQueueFlow.postValue(list) } fun switchToMessageQueue(shouldQueue: Boolean) { @@ -279,6 +290,6 @@ class MessageInputViewModel @Inject constructor( companion object { private val TAG = MessageInputViewModel::class.java.simpleName - private const val DELAY_BETWEEN_QUEUED_MESSAGES: Long = 500 + private const val DELAY_BETWEEN_QUEUED_MESSAGES: Long = 100 } }