Skip to content

Commit

Permalink
WIP offline reactions - got it persisting, but now it's not showing u…
Browse files Browse the repository at this point in the history
…p on the UI

Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 committed Jul 18, 2024
1 parent bdcb183 commit 3c848f5
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ class RepositoryModule {
}

@Provides
fun provideReactionsRepository(ncApi: NcApi, userProvider: CurrentUserProviderNew): ReactionsRepository {
return ReactionsRepositoryImpl(ncApi, userProvider)
fun provideReactionsRepository(
ncApi: NcApi,
userProvider: CurrentUserProviderNew,
dao: ChatMessagesDao
): ReactionsRepository {
return ReactionsRepositoryImpl(ncApi, userProvider, dao)
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@
package com.nextcloud.talk.repositories.reactions

import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.chat.data.model.ChatMessage
import com.nextcloud.talk.data.database.dao.ChatMessagesDao
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.domain.ReactionAddedModel
import com.nextcloud.talk.models.domain.ReactionDeletedModel
import com.nextcloud.talk.chat.data.model.ChatMessage
import com.nextcloud.talk.models.json.generic.GenericMeta
import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
import io.reactivex.Observable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject

class ReactionsRepositoryImpl(private val ncApi: NcApi, currentUserProvider: CurrentUserProviderNew) :
ReactionsRepository {
class ReactionsRepositoryImpl @Inject constructor(
private val ncApi: NcApi,
private val currentUserProvider: CurrentUserProviderNew,
private val dao: ChatMessagesDao
) : ReactionsRepository {

val currentUser: User = currentUserProvider.currentUser.blockingGet()
val credentials: String = ApiUtils.getCredentials(currentUser.username, currentUser.token)!!
Expand All @@ -31,7 +40,11 @@ class ReactionsRepositoryImpl(private val ncApi: NcApi, currentUserProvider: Cur
message.id
),
emoji
).map { mapToReactionAddedModel(message, emoji, it.ocs?.meta!!) }
).map {
val model = mapToReactionAddedModel(message, emoji, it.ocs?.meta!!)
persistAddedModel(model, roomToken)
return@map model
}
}

override fun deleteReaction(
Expand All @@ -47,7 +60,11 @@ class ReactionsRepositoryImpl(private val ncApi: NcApi, currentUserProvider: Cur
message.id
),
emoji
).map { mapToReactionDeletedModel(message, emoji, it.ocs?.meta!!) }
).map {
val model = mapToReactionDeletedModel(message, emoji, it.ocs?.meta!!)
persistDeletedModel(model, roomToken)
return@map model
}
}

private fun mapToReactionAddedModel(
Expand Down Expand Up @@ -76,6 +93,66 @@ class ReactionsRepositoryImpl(private val ncApi: NcApi, currentUserProvider: Cur
)
}

private fun persistAddedModel(model: ReactionAddedModel, roomToken: String) =
CoroutineScope(Dispatchers.IO).launch {
// 1. Call DAO, Get a singular ChatMessageEntity with model.chatMessage.{PARAM}
val accountId = currentUser.id!!
val id = model.chatMessage.jsonMessageId.toLong()
val internalConversationId = "$accountId@$roomToken"
val emoji = model.emoji

val message = dao.getChatMessageForConversation(internalConversationId, id).first()

// 2. Check state of entity, create params as needed
if (message.reactions == null) {
message.reactions = LinkedHashMap()
}

if (message.reactionsSelf == null) {
message.reactionsSelf = ArrayList()
}

var amount = message.reactions!![emoji]
if (amount == null) {
amount = 0
}
message.reactions!![emoji] = amount + 1
message.reactionsSelf!!.add(emoji)

// 3. Call DAO again, to update the singular ChatMessageEntity with params
dao.updateChatMessage(message)
}

private fun persistDeletedModel(model: ReactionDeletedModel, roomToken: String) =
CoroutineScope(Dispatchers.IO).launch {
// 1. Call DAO, Get a singular ChatMessageEntity with model.chatMessage.{PARAM}
val accountId = currentUser.id!!
val id = model.chatMessage.jsonMessageId.toLong()
val internalConversationId = "$accountId@$roomToken"
val emoji = model.emoji

val message = dao.getChatMessageForConversation(internalConversationId, id).first()

// 2. Check state of entity, create params as needed
if (message.reactions == null) {
message.reactions = LinkedHashMap()
}

if (message.reactionsSelf == null) {
message.reactionsSelf = ArrayList()
}

var amount = message.reactions!![emoji]
if (amount == null) {
amount = 0
}
message.reactions!![emoji] = amount - 1
message.reactionsSelf!!.remove(emoji)

// 3. Call DAO again, to update the singular ChatMessageEntity with params
dao.updateChatMessage(message)
}

companion object {
private const val HTTP_OK: Int = 200
private const val HTTP_CREATED: Int = 201
Expand Down

0 comments on commit 3c848f5

Please sign in to comment.