should have fixed the UI now with lifecycle and error handling

Signed-off-by: rapterjet2004 <juliuslinus1@gmail.com>
This commit is contained in:
rapterjet2004 2024-09-25 07:46:46 -05:00 committed by Marcel Hibbe
parent fb9a7b8e04
commit 98e0b7a270
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B
4 changed files with 66 additions and 35 deletions

View file

@ -535,15 +535,31 @@ class ChatActivity :
private fun initObservers() { private fun initObservers() {
Log.d(TAG, "initObservers Called") Log.d(TAG, "initObservers Called")
messageInputViewModel.messageQueueFlow.observe(this) { message -> messageInputViewModel.messageQueueFlow.observe(this) { list ->
// TODO shouldn't be able save state for (message in list) {
Log.d("Julius", "Message recieved: $message")
val temporaryChatMessage = ChatMessage() val temporaryChatMessage = ChatMessage()
temporaryChatMessage.jsonMessageId = -3 temporaryChatMessage.jsonMessageId = -3
temporaryChatMessage.actorId = "-3" temporaryChatMessage.actorId = "-3"
temporaryChatMessage.timestamp = (adapter?.items?.get(0)?.item as ChatMessage).timestamp temporaryChatMessage.timestamp = System.currentTimeMillis() / 1000
temporaryChatMessage.message = message temporaryChatMessage.message = message
adapter?.addToStart(temporaryChatMessage, true) 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 { this.lifecycleScope.launch {
chatViewModel.getConversationFlow chatViewModel.getConversationFlow
@ -647,6 +663,7 @@ class ChatActivity :
"currentConversation was null in observer ChatViewModel.GetCapabilitiesInitialLoadState" "currentConversation was null in observer ChatViewModel.GetCapabilitiesInitialLoadState"
) )
} }
messageInputViewModel.getTempMessagesFromMessageQueue(roomToken)
} }
is ChatViewModel.GetCapabilitiesErrorState -> { is ChatViewModel.GetCapabilitiesErrorState -> {
@ -2367,8 +2384,8 @@ class ChatActivity :
try { try {
EmojiCompat.get().process(currentConversation?.displayName as CharSequence).toString() EmojiCompat.get().process(currentConversation?.displayName as CharSequence).toString()
} catch (e: java.lang.IllegalStateException) { } catch (e: java.lang.IllegalStateException) {
Log.e(TAG, "setActionBarTitle failed $e")
currentConversation?.displayName currentConversation?.displayName
error(e)
} }
} else { } else {
"" ""

View file

@ -73,7 +73,6 @@ import com.nextcloud.talk.utils.text.Spans
import com.otaliastudios.autocomplete.Autocomplete import com.otaliastudios.autocomplete.Autocomplete
import com.stfalcon.chatkit.commons.models.IMessage import com.stfalcon.chatkit.commons.models.IMessage
import com.vanniktech.emoji.EmojiPopup import com.vanniktech.emoji.EmojiPopup
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -184,13 +183,15 @@ class MessageInputFragment : Fragment() {
val connectionGained = (!wasOnline && isOnline) val connectionGained = (!wasOnline && isOnline)
wasOnline = !binding.fragmentMessageInputView.isShown wasOnline = !binding.fragmentMessageInputView.isShown
Log.d(TAG, "isOnline: $isOnline\nwasOnline: $wasOnline\nconnectionGained: $connectionGained") Log.d(TAG, "isOnline: $isOnline\nwasOnline: $wasOnline\nconnectionGained: $connectionGained")
delay(500) // delay(2000)
handleMessageQueue(isOnline) handleMessageQueue(isOnline)
handleUI(isOnline, connectionGained) handleUI(isOnline, connectionGained)
}.collect() }.collect()
} }
chatActivity.messageInputViewModel.messageQueueSizeFlow.observe(viewLifecycleOwner) { size -> chatActivity.messageInputViewModel.messageQueueSizeFlow.observe(viewLifecycleOwner) { size ->
Log.d("Julius", "MessageQueueSizeFlow recieved: $size")
if (size > 0) { if (size > 0) {
binding.fragmentConnectionLost.text = getString(R.string.connection_lost_queued, size) binding.fragmentConnectionLost.text = getString(R.string.connection_lost_queued, size)
} else { } else {
@ -233,7 +234,7 @@ class MessageInputFragment : Fragment() {
binding.fragmentConnectionLost.clearAnimation() binding.fragmentConnectionLost.clearAnimation()
binding.fragmentConnectionLost.visibility = View.GONE binding.fragmentConnectionLost.visibility = View.GONE
binding.fragmentConnectionLost.setBackgroundColor(resources.getColor(R.color.hwSecurityRed)) 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.fragmentConnectionLost.visibility = View.VISIBLE
binding.fragmentMessageInputView.attachmentButton.isEnabled = false binding.fragmentMessageInputView.attachmentButton.isEnabled = false
binding.fragmentMessageInputView.recordAudioButton.isEnabled = false binding.fragmentMessageInputView.recordAudioButton.isEnabled = false

View file

@ -38,6 +38,8 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.lang.Thread.sleep
import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
class OfflineFirstChatRepository @Inject constructor( class OfflineFirstChatRepository @Inject constructor(

View file

@ -124,8 +124,8 @@ class MessageInputViewModel @Inject constructor(
val messageQueueSizeFlow: LiveData<Int> val messageQueueSizeFlow: LiveData<Int>
get() = _messageQueueSizeFlow.asLiveData() get() = _messageQueueSizeFlow.asLiveData()
private val _messageQueueFlow: MutableLiveData<String> = MutableLiveData() private val _messageQueueFlow: MutableLiveData<List<String>> = MutableLiveData()
val messageQueueFlow: LiveData<String> val messageQueueFlow: LiveData<List<String>>
get() = _messageQueueFlow get() = _messageQueueFlow
@Suppress("LongParameterList") @Suppress("LongParameterList")
@ -142,7 +142,7 @@ class MessageInputViewModel @Inject constructor(
messageQueue.add(QueuedMessage(message, displayName, replyTo, sendWithoutNotification)) messageQueue.add(QueuedMessage(message, displayName, replyTo, sendWithoutNotification))
dataStore.saveMessageQueue(roomToken, messageQueue) dataStore.saveMessageQueue(roomToken, messageQueue)
_messageQueueSizeFlow.update { messageQueue.size } _messageQueueSizeFlow.update { messageQueue.size }
_messageQueueFlow.postValue(message.toString()) _messageQueueFlow.postValue(listOf(message.toString()))
return return
} }
@ -266,6 +266,17 @@ class MessageInputViewModel @Inject constructor(
msg.sendWithoutNotification!! msg.sendWithoutNotification!!
) )
} }
_messageQueueSizeFlow.tryEmit(0)
}
fun getTempMessagesFromMessageQueue(roomToken: String) {
val queue = dataStore.getMessageQueue(roomToken)
val list = mutableListOf<String>()
for (msg in queue) {
Log.d("Julius", "Msg: ${msg.message}")
list.add(msg.message.toString())
}
_messageQueueFlow.postValue(list)
} }
fun switchToMessageQueue(shouldQueue: Boolean) { fun switchToMessageQueue(shouldQueue: Boolean) {
@ -279,6 +290,6 @@ class MessageInputViewModel @Inject constructor(
companion object { companion object {
private val TAG = MessageInputViewModel::class.java.simpleName 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
} }
} }