Skip to content

Commit

Permalink
WIP add temporary message
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Hibbe <[email protected]>
  • Loading branch information
mahibi committed Dec 3, 2024
1 parent 67dc19b commit fea6de6
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,12 @@ interface ChatMessageRepository : LifecycleAwareManager {
referenceId: String
): Flow<Result<ChatOverallSingleMessage>>

suspend fun addTemporaryMessage(
message: CharSequence,
displayName: String,
replyTo: Int,
referenceId: String
): Flow<Result<ChatOverallSingleMessage>>

suspend fun editChatMessage(credentials: String, url: String, text: String): Flow<Result<ChatOverallSingleMessage>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,75 @@ class OfflineFirstChatRepository @Inject constructor(
}
}

override suspend fun addTemporaryMessage(
message: CharSequence,
displayName: String,
replyTo: Int,
referenceId: String
): Flow<Result<ChatOverallSingleMessage>> =
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<String, Int>()
reactions[emoji1] = 3
reactions[emoji2] = 4

val reactionsSelf = ArrayList<String>()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fea6de6

Please sign in to comment.