From 48abc6373bf019634e92e6b5b6d922663b768f20 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Fri, 22 Nov 2024 12:49:09 +0100 Subject: [PATCH] WIP add temporary message Signed-off-by: Marcel Hibbe --- .../talk/chat/data/ChatMessageRepository.kt | 7 ++ .../network/OfflineFirstChatRepository.kt | 69 +++++++++++++++++++ .../chat/viewmodels/MessageInputViewModel.kt | 16 +++++ 3 files changed, 92 insertions(+) diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt b/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt index fb0bd0bd1e..737ff18fe3 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/ChatMessageRepository.kt @@ -81,5 +81,12 @@ interface ChatMessageRepository : LifecycleAwareManager { referenceId: String ): Flow> + suspend fun addTemporaryMessage( + message: CharSequence, + displayName: String, + replyTo: Int, + referenceId: String + ): Flow> + suspend fun editChatMessage(credentials: String, url: String, text: String): Flow> } 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 cc15aabc3d..9c985ced7b 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 @@ -777,6 +777,75 @@ class OfflineFirstChatRepository @Inject constructor( } } + override suspend fun addTemporaryMessage( + message: CharSequence, + displayName: String, + replyTo: Int, + referenceId: String + ): Flow> = + flow { + try { + val tempChatMessageEntity = createChatMessageEntity(internalConversationId, message.toString()) + // accessing internalConversationId creates UninitializedPropertyException because ChatViewModel and + // MessageInputViewModel use different instances of ChatRepository for now + + + chatDao.upsertChatMessage(tempChatMessageEntity) + + val tempChatMessageModel = tempChatMessageEntity.asModel() + + // emit(Result.success(response)) + + val triple = Triple(false, false, listOf(tempChatMessageModel)) + _messageFlow.emit(triple) + + } catch (e: Exception) { + Log.e(TAG, "Something went wrong when adding temporary message", e) + emit(Result.failure(e)) + } + } + + private fun createChatMessageEntity(internalConversationId: String, message: String): ChatMessageEntity { + // val id = chatMessageCounter++ + + val emoji1 = "\uD83D\uDE00" // 😀 + val emoji2 = "\uD83D\uDE1C" // 😜 + val reactions = LinkedHashMap() + reactions[emoji1] = 3 + reactions[emoji2] = 4 + + val reactionsSelf = ArrayList() + reactionsSelf.add(emoji1) + + val entity = ChatMessageEntity( + internalId = internalConversationId + "_temp1", + internalConversationId = internalConversationId, + id = 111111111, + message = message, + reactions = reactions, + reactionsSelf = reactionsSelf, + deleted = false, + token = "", + actorId = "", + actorType = "", + accountId = 1, + messageParameters = null, + messageType = "", + parentMessageId = null, + systemMessageType = ChatMessage.SystemMessageType.DUMMY, + replyable = false, + timestamp = 0, + expirationTimestamp = 0, + actorDisplayName = "", + lastEditActorType = null, + lastEditTimestamp = null, + renderMarkdown = true, + lastEditActorId = "", + lastEditActorDisplayName = "" + ) + return entity + } + companion object { val TAG = OfflineFirstChatRepository::class.simpleName private const val HTTP_CODE_OK: Int = 200 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 a8fb5f413b..02e0088f8b 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 @@ -150,6 +150,22 @@ class MessageInputViewModel @Inject constructor( val referenceId = SendMessageUtils().generateReferenceId() Log.d(TAG, "Random SHA-256 Hash: $referenceId") + viewModelScope.launch { + chatRepository.addTemporaryMessage( + message, + displayName, + replyTo, + referenceId + ).collect { result -> + if (result.isSuccess) { + Log.d(TAG, "bbbb") + } else { + Log.d(TAG, "xxxx") + } + } + } + + if (isQueueing) { val tempID = System.currentTimeMillis().toInt() val qMsg = QueuedMessage(tempID, message, displayName, replyTo, sendWithoutNotification)