Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: composite messages #1997

Merged
merged 16 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/src/main/kotlin/com/wire/android/di/CoreLogicModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import com.wire.kalium.logic.feature.message.SendEditTextMessageUseCase
import com.wire.kalium.logic.feature.message.SendKnockUseCase
import com.wire.kalium.logic.feature.message.SendTextMessageUseCase
import com.wire.kalium.logic.feature.message.ToggleReactionUseCase
import com.wire.kalium.logic.feature.message.composite.SendButtonActionMessageUseCase
import com.wire.kalium.logic.feature.message.ephemeral.EnqueueMessageSelfDeletionUseCase
import com.wire.kalium.logic.feature.message.getPaginatedFlowOfMessagesByConversation
import com.wire.kalium.logic.feature.publicuser.GetAllContactsUseCase
Expand Down Expand Up @@ -1208,6 +1209,14 @@ class UseCaseModule {
): DeleteAccountUseCase =
coreLogic.getSessionScope(currentAccount).users.deleteAccount

@ViewModelScoped
@Provides
fun provideSendButtonActionMessageUseCase(
@KaliumCoreLogic coreLogic: CoreLogic,
@CurrentAccount currentAccount: UserId
): SendButtonActionMessageUseCase =
coreLogic.getSessionScope(currentAccount).messages.sendButtonActionMessage

@ViewModelScoped
@Provides
fun providePersistScreenshotCensoringConfigUseCase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,20 @@ fun MessagePreview.uiLastMessageContent(): UILastMessageContent {
is WithUser.TeamMemberRemoved -> UILastMessageContent.None // TODO
is WithUser.Text -> UILastMessageContent.SenderWithMessage(
sender = userUIText,
message = UIText.DynamicString((content as WithUser.Text).messageBody),
message = (content as WithUser.Text).messageBody.let { UIText.DynamicString(it) },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this crash prone ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, since this code will only be executed when the content is Text

separator = ": "
)

is WithUser.Composite -> {
val text = (content as WithUser.Composite).messageBody?.let { UIText.DynamicString(it) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn;t this crash prone when content is not WithUser.Composite and we try to cast it ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is impossible to crash this because of the when this code will run iff the content is Composite

?: UIText.StringResource(R.string.last_message_composite_with_missing_text)
UILastMessageContent.SenderWithMessage(
sender = userUIText,
message = text,
separator = ": "
)
}

is WithUser.MissedCall -> UILastMessageContent.TextMessage(
MessageBody(UIText.PluralResource(R.plurals.unread_event_call, 1, 1))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.wire.android.model.ImageAsset
import com.wire.android.ui.home.conversations.findUser
import com.wire.android.ui.home.conversations.model.DeliveryStatusContent
import com.wire.android.ui.home.conversations.model.MessageBody
import com.wire.android.ui.home.conversations.model.MessageButton
import com.wire.android.ui.home.conversations.model.UIMessageContent
import com.wire.android.ui.home.conversations.model.UIQuotedMessage
import com.wire.android.util.time.ISOFormatter
Expand Down Expand Up @@ -91,6 +92,33 @@ class RegularMessageMapper @Inject constructor(
message.deliveryStatus
)

is MessageContent.Composite -> {
val text = content.textContent?.let { textContent ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can;t we map it inside the mapper ?

val quotedMessage = textContent.quotedMessageDetails?.let { mapQuoteData(message.conversationId, it) }
?: if (textContent.quotedMessageReference?.quotedMessageId != null) {
UIQuotedMessage.UnavailableData
} else {
null
}

MessageBody(
message = UIText.DynamicString(textContent.value, content.textContent?.mentions.orEmpty()),
quotedMessage = quotedMessage
)
}

UIMessageContent.Composite(
messageBody = text,
buttonList = content.buttonList.map {
MessageButton(
id = it.id,
text = it.text,
isSelected = it.isSelected
)
}
)
}

else -> toText(message.conversationId, content, userList, message.deliveryStatus)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ data class Device(
clientId = client.id,
registrationTime = client.registrationTime?.toIsoDateTimeString(),
lastActiveInWholeWeeks = client.lastActiveInWholeWeeks(),
isValid = client.isVerified,
isValid = client.isValid,
isVerified = client.isVerified
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Wire
* Copyright (C) 2023 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.ui.home.conversations

import androidx.annotation.VisibleForTesting
import androidx.compose.runtime.mutableStateMapOf
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.wire.android.navigation.EXTRA_CONVERSATION_ID
import com.wire.android.navigation.SavedStateViewModel
import com.wire.kalium.logic.data.id.MessageButtonId
import com.wire.kalium.logic.data.id.MessageId
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.id.QualifiedIdMapper
import com.wire.kalium.logic.feature.message.composite.SendButtonActionMessageUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class CompositeMessagesViewModel @Inject constructor(
MohamadJaara marked this conversation as resolved.
Show resolved Hide resolved
private val sendButtonActionMessageUseCase: SendButtonActionMessageUseCase,
qualifiedIdMapper: QualifiedIdMapper,
savedStateHandle: SavedStateHandle,
) : SavedStateViewModel(savedStateHandle) {

val conversationId: QualifiedID = qualifiedIdMapper.fromStringToQualifiedID(
savedStateHandle.get<String>(EXTRA_CONVERSATION_ID)!!
)

var pendingButtons = mutableStateMapOf<MessageId, MessageButtonId>()
@VisibleForTesting
set

fun onButtonClicked(messageId: String, buttonId: String) {
if (pendingButtons.containsKey(messageId)) return

pendingButtons[messageId] = buttonId
viewModelScope.launch {
sendButtonActionMessageUseCase(conversationId, messageId, buttonId)
}.invokeOnCompletion {
pendingButtons.remove(messageId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ fun ConversationScreen(
conversationBannerViewModel: ConversationBannerViewModel = hiltSavedStateViewModel(backNavArgs = backNavArgs),
conversationCallViewModel: ConversationCallViewModel = hiltSavedStateViewModel(backNavArgs = backNavArgs),
conversationMessagesViewModel: ConversationMessagesViewModel = hiltSavedStateViewModel(backNavArgs = backNavArgs),
messageComposerViewModel: MessageComposerViewModel = hiltSavedStateViewModel(backNavArgs = backNavArgs)
messageComposerViewModel: MessageComposerViewModel = hiltSavedStateViewModel(backNavArgs = backNavArgs),
compositeMessagesViewModel: CompositeMessagesViewModel = hiltSavedStateViewModel(backNavArgs = backNavArgs)
) {
val coroutineScope = rememberCoroutineScope()
val showDialog = remember { mutableStateOf(ConversationScreenDialogType.NONE) }
Expand Down Expand Up @@ -219,6 +220,7 @@ fun ConversationScreen(
onFailedMessageRetryClicked = messageComposerViewModel::retrySendingMessage,
requestMentions = messageComposerViewModel::searchMembersToMention,
onClearMentionSearchResult = messageComposerViewModel::clearMentionSearchResult,
compositeMessagesViewModel = compositeMessagesViewModel
)
DeleteMessageDialog(
state = messageComposerViewModel.deleteMessageDialogsState,
Expand Down Expand Up @@ -305,6 +307,7 @@ private fun ConversationScreen(
composerMessages: SharedFlow<SnackBarMessage>,
conversationMessages: SharedFlow<SnackBarMessage>,
conversationMessagesViewModel: ConversationMessagesViewModel,
compositeMessagesViewModel: CompositeMessagesViewModel,
onSelfDeletingMessageRead: (UIMessage) -> Unit,
onNewSelfDeletingMessagesStatus: (SelfDeletionTimer) -> Unit,
tempWritableImageUri: Uri?,
Expand Down Expand Up @@ -421,7 +424,9 @@ private fun ConversationScreen(
onClearMentionSearchResult = onClearMentionSearchResult,
tempWritableImageUri = tempWritableImageUri,
tempWritableVideoUri = tempWritableVideoUri,
snackBarHostState = conversationScreenState.snackBarHostState
snackBarHostState = conversationScreenState.snackBarHostState,
onMessageButtonClicked = compositeMessagesViewModel::onButtonClicked,
pendingButtonsMap = compositeMessagesViewModel.pendingButtons,
)
}
MenuModalSheetLayout(
Expand Down Expand Up @@ -460,6 +465,8 @@ private fun ConversationScreenContent(
onChangeSelfDeletionClicked: () -> Unit,
onSearchMentionQueryChanged: (String) -> Unit,
onClearMentionSearchResult: () -> Unit,
onMessageButtonClicked: (messageId: String, buttonId: String) -> Unit,
pendingButtonsMap: Map<String, String>,
MohamadJaara marked this conversation as resolved.
Show resolved Hide resolved
tempWritableImageUri: Uri?,
tempWritableVideoUri: Uri?,
snackBarHostState: SnackbarHostState
Expand Down Expand Up @@ -491,7 +498,9 @@ private fun ConversationScreenContent(
onShowEditingOption = onShowEditingOptions,
conversationDetailsData = conversationDetailsData,
onFailedMessageCancelClicked = onFailedMessageCancelClicked,
onFailedMessageRetryClicked = onFailedMessageRetryClicked
onFailedMessageRetryClicked = onFailedMessageRetryClicked,
onMessageButtonClicked = onMessageButtonClicked,
pendingButtonsMap = pendingButtonsMap
)
},
onChangeSelfDeletionClicked = onChangeSelfDeletionClicked,
Expand Down Expand Up @@ -571,7 +580,9 @@ fun MessageList(
onSelfDeletingMessageRead: (UIMessage) -> Unit,
conversationDetailsData: ConversationDetailsData,
onFailedMessageRetryClicked: (String) -> Unit,
onFailedMessageCancelClicked: (String) -> Unit
onFailedMessageCancelClicked: (String) -> Unit,
onMessageButtonClicked: (messageId: String, buttonId: String) -> Unit,
pendingButtonsMap: Map<String, String>
MohamadJaara marked this conversation as resolved.
Show resolved Hide resolved
) {
val mostRecentMessage = lazyPagingMessages.itemCount.takeIf { it > 0 }?.let { lazyPagingMessages[0] }

Expand Down Expand Up @@ -627,7 +638,9 @@ fun MessageList(
onResetSessionClicked = onResetSessionClicked,
onSelfDeletingMessageRead = onSelfDeletingMessageRead,
onFailedMessageCancelClicked = onFailedMessageCancelClicked,
onFailedMessageRetryClicked = onFailedMessageRetryClicked
onFailedMessageRetryClicked = onFailedMessageRetryClicked,
onMessageButtonClicked = onMessageButtonClicked,
pendingButtonsMap = pendingButtonsMap
)
}

Expand Down Expand Up @@ -700,6 +713,7 @@ fun PreviewConversationScreen() {
tempWritableVideoUri = null,
onFailedMessageRetryClicked = {},
requestMentions = {},
onClearMentionSearchResult = {}
onClearMentionSearchResult = {},
compositeMessagesViewModel = hiltViewModel()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ fun MessageItem(
onResetSessionClicked: (senderUserId: UserId, clientId: String?) -> Unit,
onSelfDeletingMessageRead: (UIMessage) -> Unit,
onFailedMessageRetryClicked: (String) -> Unit = {},
onFailedMessageCancelClicked: (String) -> Unit = {}
onFailedMessageCancelClicked: (String) -> Unit = {},
onMessageButtonClicked: (messageId: String, buttonId: String) -> Unit,
pendingButtonsMap: Map<String, String> = emptyMap()
) {
with(message) {
val selfDeletionTimerState = rememberSelfDeletionTimer(header.messageStatus.expirationStatus)
Expand Down Expand Up @@ -158,7 +160,7 @@ fun MessageItem(

val isProfileRedirectEnabled =
header.userId != null &&
!(header.isSenderDeleted || header.isSenderUnavailable)
!(header.isSenderDeleted || header.isSenderUnavailable)

if (showAuthor) {
val avatarClickable = remember {
Expand Down Expand Up @@ -229,7 +231,9 @@ fun MessageItem(
onAssetClick = currentOnAssetClicked,
onImageClick = currentOnImageClick,
onLongClick = onLongClick,
onOpenProfile = onOpenProfile
onOpenProfile = onOpenProfile,
onMessageButtonClicked = onMessageButtonClicked,
pendingButtonsMap = pendingButtonsMap,
)
}
if (isMyMessage) {
Expand Down Expand Up @@ -444,6 +448,8 @@ private fun MessageContent(
audioMessagesState: Map<String, AudioState>,
onAssetClick: Clickable,
onImageClick: Clickable,
onMessageButtonClicked: (messageId: String, buttonId: String) -> Unit,
pendingButtonsMap: Map<String, String>,
onAudioClick: (String) -> Unit,
onChangeAudioPosition: (String, Int) -> Unit,
onLongClick: (() -> Unit)? = null,
Expand Down Expand Up @@ -475,12 +481,39 @@ private fun MessageContent(
messageBody = messageContent.messageBody,
isAvailable = !message.isPending && message.isAvailable,
onLongClick = onLongClick,
onOpenProfile = onOpenProfile
onOpenProfile = onOpenProfile,
buttonList = null,
onButtonClick = null,
pendingButton = null,
messageId = message.header.messageId
)
PartialDeliveryInformation(messageContent.deliveryStatus)
}
}

is UIMessageContent.Composite -> {
Column {
messageContent.messageBody?.quotedMessage?.let {
VerticalSpace.x4()
when (it) {
is UIQuotedMessage.UIQuotedData -> QuotedMessage(it)
UIQuotedMessage.UnavailableData -> QuotedUnavailable(QuotedMessageStyle.COMPLETE)
}
VerticalSpace.x4()
}
MessageBody(
messageBody = messageContent.messageBody,
isAvailable = !message.isPending && message.isAvailable,
onLongClick = onLongClick,
onOpenProfile = onOpenProfile,
buttonList = messageContent.buttonList,
onButtonClick = onMessageButtonClicked,
messageId = message.header.messageId,
pendingButton = pendingButtonsMap[message.header.messageId]
)
}
}
MohamadJaara marked this conversation as resolved.
Show resolved Hide resolved

is UIMessageContent.AssetMessage -> {
MessageGenericAsset(
assetName = messageContent.assetName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ import com.wire.android.navigation.getBackNavArg
import com.wire.android.ui.home.conversations.ConversationSnackbarMessages
import com.wire.android.ui.home.conversations.ConversationSnackbarMessages.OnResetSession
import com.wire.android.ui.home.conversations.model.AssetBundle
import com.wire.kalium.logic.data.asset.AttachmentType
import com.wire.android.ui.home.conversations.model.UIMessage
import com.wire.android.ui.home.conversations.usecase.GetMessagesForConversationUseCase
import com.wire.android.util.FileManager
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.android.util.startFileShareIntent
import com.wire.android.util.ui.UIText
import com.wire.kalium.logic.data.asset.AttachmentType
import com.wire.kalium.logic.data.conversation.ClientId
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.id.QualifiedIdMapper
Expand All @@ -68,6 +68,7 @@ import com.wire.kalium.logic.feature.sessionreset.ResetSessionUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -96,6 +97,7 @@ class ConversationMessagesViewModel @Inject constructor(
) : SavedStateViewModel(savedStateHandle) {

var conversationViewState by mutableStateOf(ConversationMessagesViewState())
private set

private val conversationId: QualifiedID = qualifiedIdMapper.fromStringToQualifiedID(
savedStateHandle.get<String>(EXTRA_CONVERSATION_ID)!!
Expand Down
Loading