[Refactor][Chat] Updated Local User Logic (#711)
This commit is contained in:
Родитель
e7975fcab9
Коммит
50ea06f7ad
|
@ -21,12 +21,4 @@ internal class ChatCompositeEventHandlerRepository {
|
|||
fun getLocalParticipantRemovedHandlers(): List<ChatCompositeEventHandler<String>> {
|
||||
return eventHandlers
|
||||
}
|
||||
|
||||
fun addLocalParticipantRemovedEventHandler(handler: ChatCompositeEventHandler<String>) {
|
||||
eventHandlers.add(handler)
|
||||
}
|
||||
|
||||
fun removeLocalParticipantRemovedEventHandler(handler: ChatCompositeEventHandler<String>) {
|
||||
eventHandlers.remove(handler)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,8 @@ internal fun com.azure.android.communication.chat.models.ChatMessage.into(localP
|
|||
participants = this.content.participants?.map {
|
||||
RemoteParticipantInfoModel(
|
||||
userIdentifier = it.communicationIdentifier.into(),
|
||||
displayName = it.displayName
|
||||
displayName = it.displayName,
|
||||
isLocalUser = it.communicationIdentifier.into().id == localParticipantIdentifier
|
||||
)
|
||||
}?.toList() ?: emptyList(),
|
||||
internalId = null,
|
||||
|
|
|
@ -8,4 +8,5 @@ import com.azure.android.communication.ui.chat.service.sdk.wrapper.Communication
|
|||
internal data class RemoteParticipantInfoModel(
|
||||
val userIdentifier: CommunicationIdentifier,
|
||||
val displayName: String?,
|
||||
val isLocalUser: Boolean = false
|
||||
)
|
||||
|
|
|
@ -10,9 +10,8 @@ internal sealed class ParticipantAction : Action {
|
|||
class ParticipantsAdded(val participants: List<RemoteParticipantInfoModel>) :
|
||||
ParticipantAction()
|
||||
|
||||
class ParticipantsRemoved(val participants: List<RemoteParticipantInfoModel>, val localParticipantRemoved: Boolean) :
|
||||
class ParticipantsRemoved(val participants: List<RemoteParticipantInfoModel>) :
|
||||
ParticipantAction()
|
||||
|
||||
class AddParticipantTyping(val infoModel: ParticipantTimestampInfoModel) : ParticipantAction()
|
||||
class RemoveParticipantTyping(val infoModel: ParticipantTimestampInfoModel) :
|
||||
ParticipantAction()
|
||||
|
|
|
@ -50,13 +50,10 @@ internal class MessageRepositoryMiddlewareImpl(
|
|||
store::dispatch
|
||||
)
|
||||
is ParticipantAction.ParticipantsRemoved -> {
|
||||
processParticipantsRemoved(
|
||||
action,
|
||||
store.getCurrentState().participantState.localParticipantInfoModel.userIdentifier,
|
||||
store::dispatch,
|
||||
)
|
||||
if (action.localParticipantRemoved) {
|
||||
processLocalParticipantRemoved(action, store::dispatch)
|
||||
if (action.participants.any { it.isLocalUser }) {
|
||||
processLocalParticipantRemoved(store::dispatch)
|
||||
} else {
|
||||
processParticipantsRemoved(action, store::dispatch)
|
||||
}
|
||||
}
|
||||
is NetworkAction.Disconnected -> processNetworkDisconnected(store::dispatch)
|
||||
|
@ -167,44 +164,35 @@ internal class MessageRepositoryMiddlewareImpl(
|
|||
|
||||
private fun processParticipantsRemoved(
|
||||
action: ParticipantAction.ParticipantsRemoved,
|
||||
localUserId: String,
|
||||
dispatch: Dispatch,
|
||||
) {
|
||||
val participants = action.participants.filter { it.userIdentifier.id != localUserId }
|
||||
|
||||
if (participants.isNotEmpty()) {
|
||||
messageRepository.addMessage(
|
||||
MessageInfoModel(
|
||||
internalId = System.currentTimeMillis().toString(),
|
||||
participants = participants,
|
||||
content = null,
|
||||
createdOn = OffsetDateTime.now(),
|
||||
senderDisplayName = null,
|
||||
messageType = ChatMessageType.PARTICIPANT_REMOVED,
|
||||
)
|
||||
messageRepository.addMessage(
|
||||
MessageInfoModel(
|
||||
internalId = System.currentTimeMillis().toString(),
|
||||
participants = action.participants,
|
||||
content = null,
|
||||
createdOn = OffsetDateTime.now(),
|
||||
senderDisplayName = null,
|
||||
messageType = ChatMessageType.PARTICIPANT_REMOVED,
|
||||
)
|
||||
notifyUpdate(dispatch)
|
||||
}
|
||||
)
|
||||
notifyUpdate(dispatch)
|
||||
}
|
||||
|
||||
private fun processLocalParticipantRemoved(
|
||||
action: ParticipantAction.ParticipantsRemoved,
|
||||
dispatch: Dispatch,
|
||||
) {
|
||||
|
||||
if (action.localParticipantRemoved) {
|
||||
messageRepository.addMessage(
|
||||
MessageInfoModel(
|
||||
internalId = System.currentTimeMillis().toString(),
|
||||
isCurrentUser = true,
|
||||
content = null,
|
||||
createdOn = OffsetDateTime.now(),
|
||||
senderDisplayName = null,
|
||||
messageType = ChatMessageType.PARTICIPANT_REMOVED,
|
||||
)
|
||||
messageRepository.addMessage(
|
||||
MessageInfoModel(
|
||||
internalId = System.currentTimeMillis().toString(),
|
||||
isCurrentUser = true,
|
||||
content = null,
|
||||
createdOn = OffsetDateTime.now(),
|
||||
senderDisplayName = null,
|
||||
messageType = ChatMessageType.PARTICIPANT_REMOVED,
|
||||
)
|
||||
notifyUpdate(dispatch)
|
||||
}
|
||||
)
|
||||
notifyUpdate(dispatch)
|
||||
}
|
||||
|
||||
private fun processDeletedMessage(action: ChatAction.MessageDeleted, dispatch: Dispatch) {
|
||||
|
|
|
@ -12,7 +12,6 @@ import com.azure.android.communication.ui.chat.models.MessageInfoModel
|
|||
import com.azure.android.communication.ui.chat.models.MessagesPageModel
|
||||
import com.azure.android.communication.ui.chat.models.ParticipantTimestampInfoModel
|
||||
import com.azure.android.communication.ui.chat.models.RemoteParticipantsInfoModel
|
||||
import com.azure.android.communication.ui.chat.models.RemoteParticipantInfoModel
|
||||
import com.azure.android.communication.ui.chat.redux.Dispatch
|
||||
import com.azure.android.communication.ui.chat.redux.Store
|
||||
import com.azure.android.communication.ui.chat.redux.action.ChatAction
|
||||
|
@ -160,11 +159,7 @@ internal class ChatServiceListener(
|
|||
|
||||
dispatch(
|
||||
ParticipantAction.ParticipantsRemoved(
|
||||
participants = it.infoModel.participants,
|
||||
localParticipantRemoved = isLocalParticipantRemoved(
|
||||
it.infoModel.participants,
|
||||
localParticipantInfoModel
|
||||
)
|
||||
participants = it.infoModel.participants
|
||||
)
|
||||
)
|
||||
}
|
||||
|
@ -173,10 +168,4 @@ internal class ChatServiceListener(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun isLocalParticipantRemoved(
|
||||
participants: List<RemoteParticipantInfoModel>,
|
||||
localParticipantInfoModel: LocalParticipantInfoModel,
|
||||
) =
|
||||
participants.any { it.userIdentifier.id == localParticipantInfoModel.userIdentifier }
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ internal class ParticipantsReducerImpl : ParticipantsReducer {
|
|||
is ParticipantAction.ParticipantsRemoved -> {
|
||||
val participantTypingKeys = state.participantTyping.keys
|
||||
val removedParticipants = action.participants.map { it.userIdentifier.id }
|
||||
var hasLocalParticipant = action.participants.any { it.isLocalUser }
|
||||
var participantTyping = state.participantTyping
|
||||
// TODO: improve this logic
|
||||
removedParticipants.forEach { id ->
|
||||
|
@ -40,7 +41,7 @@ internal class ParticipantsReducerImpl : ParticipantsReducer {
|
|||
|
||||
var updatedState = state
|
||||
|
||||
if (action.localParticipantRemoved) {
|
||||
if (hasLocalParticipant) {
|
||||
updatedState = updatedState.copy(
|
||||
localParticipantInfoModel =
|
||||
state.localParticipantInfoModel.copy(isActiveChatThreadParticipant = false)
|
||||
|
|
|
@ -193,7 +193,8 @@ internal class ChatEventHandler {
|
|||
participants = event.participantsAdded.map {
|
||||
RemoteParticipantInfoModel(
|
||||
userIdentifier = it.communicationIdentifier.into(),
|
||||
displayName = it.displayName
|
||||
displayName = it.displayName,
|
||||
isLocalUser = it.communicationIdentifier.into().id == this.localParticipantIdentifier
|
||||
)
|
||||
}
|
||||
)
|
||||
|
@ -210,7 +211,8 @@ internal class ChatEventHandler {
|
|||
participants = event.participantsRemoved.map {
|
||||
RemoteParticipantInfoModel(
|
||||
userIdentifier = it.communicationIdentifier.into(),
|
||||
displayName = it.displayName
|
||||
displayName = it.displayName,
|
||||
isLocalUser = it.communicationIdentifier.into().id == this.localParticipantIdentifier
|
||||
)
|
||||
}
|
||||
)
|
||||
|
|
|
@ -71,7 +71,8 @@ internal class ChatFetchNotificationHandler(coroutineContextProvider: CoroutineC
|
|||
participants = message.content.participants.map {
|
||||
RemoteParticipantInfoModel(
|
||||
userIdentifier = it.communicationIdentifier.into(),
|
||||
displayName = it.displayName
|
||||
displayName = it.displayName,
|
||||
isLocalUser = it.communicationIdentifier.into().id == this.localParticipantIdentifier
|
||||
)
|
||||
}
|
||||
)
|
||||
|
@ -88,7 +89,8 @@ internal class ChatFetchNotificationHandler(coroutineContextProvider: CoroutineC
|
|||
participants = message.content.participants.map {
|
||||
RemoteParticipantInfoModel(
|
||||
userIdentifier = it.communicationIdentifier.into(),
|
||||
displayName = it.displayName
|
||||
displayName = it.displayName,
|
||||
isLocalUser = it.communicationIdentifier.into().id == this.localParticipantIdentifier
|
||||
)
|
||||
}
|
||||
)
|
||||
|
|
|
@ -202,7 +202,8 @@ internal class ChatSDKWrapper(
|
|||
participants.add(
|
||||
RemoteParticipantInfoModel(
|
||||
userIdentifier = it.communicationIdentifier.into(),
|
||||
displayName = it.displayName
|
||||
displayName = it.displayName,
|
||||
isLocalUser = it.communicationIdentifier.into().id == localParticipantIdentifier
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
|
@ -355,7 +355,7 @@ class ParticipantsReducerUnitTest {
|
|||
mutableSetOf(),
|
||||
)
|
||||
val action =
|
||||
ParticipantAction.ParticipantsRemoved(participants = listOf(userThree, userFour), false)
|
||||
ParticipantAction.ParticipantsRemoved(participants = listOf(userThree, userFour))
|
||||
|
||||
// act
|
||||
val newState = reducer.reduce(previousState, action)
|
||||
|
|
Загрузка…
Ссылка в новой задаче