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,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 {
@ -647,6 +663,7 @@ class ChatActivity :
"currentConversation was null in observer ChatViewModel.GetCapabilitiesInitialLoadState"
)
}
messageInputViewModel.getTempMessagesFromMessageQueue(roomToken)
}
is ChatViewModel.GetCapabilitiesErrorState -> {
@ -2367,8 +2384,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 {
""

View file

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

View file

@ -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<ChatMessageJson>()
)
}
return@map Pair(
HTTP_CODE_NOT_MODIFIED,
listOf<ChatMessageJson>()
)
}
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<ChatMessageJson>()
)
}
return@map Pair(
HTTP_CODE_PRECONDITION_FAILED,
listOf<ChatMessageJson>()
)
}
else -> {
return@map Pair(

View file

@ -124,8 +124,8 @@ class MessageInputViewModel @Inject constructor(
val messageQueueSizeFlow: LiveData<Int>
get() = _messageQueueSizeFlow.asLiveData()
private val _messageQueueFlow: MutableLiveData<String> = MutableLiveData()
val messageQueueFlow: LiveData<String>
private val _messageQueueFlow: MutableLiveData<List<String>> = MutableLiveData()
val messageQueueFlow: LiveData<List<String>>
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<String>()
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
}
}