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 backportbot[bot]
parent 7777ae1766
commit afd29ac03c
4 changed files with 66 additions and 35 deletions

View file

@ -535,14 +535,30 @@ 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) {
val temporaryChatMessage = ChatMessage() Log.d("Julius", "Message recieved: $message")
temporaryChatMessage.jsonMessageId = -3 val temporaryChatMessage = ChatMessage()
temporaryChatMessage.actorId = "-3" temporaryChatMessage.jsonMessageId = -3
temporaryChatMessage.timestamp = (adapter?.items?.get(0)?.item as ChatMessage).timestamp temporaryChatMessage.actorId = "-3"
temporaryChatMessage.message = message temporaryChatMessage.timestamp = System.currentTimeMillis() / 1000
adapter?.addToStart(temporaryChatMessage, true) 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 { this.lifecycleScope.launch {
@ -632,6 +648,7 @@ class ChatActivity :
withUrl = urlForChatting withUrl = urlForChatting
) )
} }
messageInputViewModel.getTempMessagesFromMessageQueue(roomToken)
} }
is ChatViewModel.GetCapabilitiesErrorState -> { is ChatViewModel.GetCapabilitiesErrorState -> {
@ -2352,8 +2369,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(
@ -390,29 +392,29 @@ class OfflineFirstChatRepository @Inject constructor(
Integer.parseInt(it) Integer.parseInt(it)
} }
return@map Pair( return@map Pair(
HTTP_CODE_OK, HTTP_CODE_OK,
(it.body() as ChatOverall).ocs!!.data!! (it.body() as ChatOverall).ocs!!.data!!
) )
} }
HTTP_CODE_NOT_MODIFIED -> { HTTP_CODE_NOT_MODIFIED -> {
Log.d(TAG, "getMessagesFromServer HTTP_CODE_NOT_MODIFIED") Log.d(TAG, "getMessagesFromServer HTTP_CODE_NOT_MODIFIED")
return@map Pair( return@map Pair(
HTTP_CODE_NOT_MODIFIED, HTTP_CODE_NOT_MODIFIED,
listOf<ChatMessageJson>() listOf<ChatMessageJson>()
) )
} }
HTTP_CODE_PRECONDITION_FAILED -> { HTTP_CODE_PRECONDITION_FAILED -> {
Log.d(TAG, "getMessagesFromServer HTTP_CODE_PRECONDITION_FAILED") Log.d(TAG, "getMessagesFromServer HTTP_CODE_PRECONDITION_FAILED")
return@map Pair( return@map Pair(
HTTP_CODE_PRECONDITION_FAILED, HTTP_CODE_PRECONDITION_FAILED,
listOf<ChatMessageJson>() listOf<ChatMessageJson>()
) )
} }
else -> { else -> {
return@map Pair( return@map Pair(

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
} }
} }