Skip to content

Commit

Permalink
feat: message draft [WPB-1021] (#2796)
Browse files Browse the repository at this point in the history
  • Loading branch information
Garzas authored Mar 22, 2024
1 parent ec46b08 commit 06d1399
Show file tree
Hide file tree
Showing 22 changed files with 853 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ import com.wire.kalium.logic.feature.message.SendLocationUseCase
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.draft.GetMessageDraftUseCase
import com.wire.kalium.logic.feature.message.draft.RemoveMessageDraftUseCase
import com.wire.kalium.logic.feature.message.draft.SaveMessageDraftUseCase
import com.wire.kalium.logic.feature.message.ephemeral.EnqueueMessageSelfDeletionUseCase
import com.wire.kalium.logic.feature.message.getPaginatedFlowOfAssetMessageByConversationId
import com.wire.kalium.logic.feature.message.getPaginatedFlowOfMessagesByConversation
Expand Down Expand Up @@ -198,4 +201,19 @@ class MessageModule {
@Provides
fun provideObserveAssetStatusesUseCase(messageScope: MessageScope): ObserveAssetStatusesUseCase =
messageScope.observeAssetStatuses

@ViewModelScoped
@Provides
fun provideSaveMessageDraftUseCase(messageScope: MessageScope): SaveMessageDraftUseCase =
messageScope.saveMessageDraftUseCase

@ViewModelScoped
@Provides
fun provideGetMessageDraftUseCase(messageScope: MessageScope): GetMessageDraftUseCase =
messageScope.getMessageDraftUseCase

@ViewModelScoped
@Provides
fun provideRemoveMessageDraftUseCase(messageScope: MessageScope): RemoveMessageDraftUseCase =
messageScope.removeMessageDraftUseCase
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ import com.wire.android.ui.home.conversations.info.ConversationInfoViewModel
import com.wire.android.ui.home.conversations.info.ConversationInfoViewState
import com.wire.android.ui.home.conversations.messages.ConversationMessagesViewModel
import com.wire.android.ui.home.conversations.messages.ConversationMessagesViewState
import com.wire.android.ui.home.conversations.messages.draft.MessageDraftViewModel
import com.wire.android.ui.home.conversations.migration.ConversationMigrationViewModel
import com.wire.android.ui.home.conversations.model.ExpirationStatus
import com.wire.android.ui.home.conversations.model.UIMessage
Expand All @@ -132,6 +133,7 @@ import com.wire.android.ui.home.conversations.selfdeletion.SelfDeletionMenuItems
import com.wire.android.ui.home.gallery.MediaGalleryActionType
import com.wire.android.ui.home.gallery.MediaGalleryNavBackArgs
import com.wire.android.ui.home.messagecomposer.MessageComposer
import com.wire.android.ui.home.messagecomposer.model.MessageComposition
import com.wire.android.ui.home.messagecomposer.state.MessageBundle
import com.wire.android.ui.home.messagecomposer.state.MessageComposerStateHolder
import com.wire.android.ui.home.messagecomposer.state.rememberMessageComposerStateHolder
Expand Down Expand Up @@ -187,6 +189,7 @@ fun ConversationScreen(
conversationMessagesViewModel: ConversationMessagesViewModel = hiltViewModel(),
messageComposerViewModel: MessageComposerViewModel = hiltViewModel(),
conversationMigrationViewModel: ConversationMigrationViewModel = hiltViewModel(),
messageDraftViewModel: MessageDraftViewModel = hiltViewModel(),
groupDetailsScreenResultRecipient: ResultRecipient<GroupConversationDetailsScreenDestination, GroupConversationDetailsNavBackArgs>,
mediaGalleryScreenResultRecipient: ResultRecipient<MediaGalleryScreenDestination, MediaGalleryNavBackArgs>,
resultNavigator: ResultBackNavigator<GroupConversationDetailsNavBackArgs>,
Expand All @@ -199,7 +202,9 @@ fun ConversationScreen(
val messageComposerViewState = messageComposerViewModel.messageComposerViewState
val messageComposerStateHolder = rememberMessageComposerStateHolder(
messageComposerViewState = messageComposerViewState,
modalBottomSheetState = conversationScreenState.modalBottomSheetState
modalBottomSheetState = conversationScreenState.modalBottomSheetState,
messageComposition = messageDraftViewModel.state,
onSaveDraft = messageComposerViewModel::saveDraft
)
val permissionPermanentlyDeniedDialogState =
rememberVisibilityState<PermissionPermanentlyDeniedDialogState>()
Expand All @@ -214,6 +219,17 @@ fun ConversationScreen(
}
}

// set message composer input to edit mode when editMessage is not null from MessageDraft
LaunchedEffect(messageDraftViewModel.state.value.editMessageId) {
val compositionState = messageDraftViewModel.state.value
if (compositionState.editMessageId != null) {
messageComposerStateHolder.toEdit(
messageId = compositionState.editMessageId,
editMessageText = compositionState.messageText,
mentions = compositionState.selectedMentions.map { it.intoMessageMention() })
}
}

conversationMigrationViewModel.migratedConversationId?.let { migratedConversationId ->
navigator.navigate(
NavigationCommand(
Expand Down Expand Up @@ -1047,10 +1063,13 @@ private fun CoroutineScope.withSmoothScreenLoad(block: () -> Unit) = launch {
@Composable
fun PreviewConversationScreen() {
val messageComposerViewState = remember { mutableStateOf(MessageComposerViewState()) }
val messageCompositionState = remember { mutableStateOf(MessageComposition.DEFAULT) }
val conversationScreenState = rememberConversationScreenState()
val messageComposerStateHolder = rememberMessageComposerStateHolder(
messageComposerViewState = messageComposerViewState,
modalBottomSheetState = conversationScreenState.modalBottomSheetState
modalBottomSheetState = conversationScreenState.modalBottomSheetState,
messageComposition = messageCompositionState,
onSaveDraft = {}
)
ConversationScreen(
bannerMessage = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import com.wire.kalium.logic.data.asset.KaliumFileSystem
import com.wire.kalium.logic.data.conversation.Conversation.TypingIndicatorMode
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.message.SelfDeletionTimer
import com.wire.kalium.logic.data.message.draft.MessageDraft
import com.wire.kalium.logic.data.user.OtherUser
import com.wire.kalium.logic.failure.LegalHoldEnabledForConversationFailure
import com.wire.kalium.logic.feature.asset.GetAssetSizeLimitUseCase
Expand All @@ -73,6 +74,8 @@ import com.wire.kalium.logic.feature.message.SendEditTextMessageUseCase
import com.wire.kalium.logic.feature.message.SendKnockUseCase
import com.wire.kalium.logic.feature.message.SendLocationUseCase
import com.wire.kalium.logic.feature.message.SendTextMessageUseCase
import com.wire.kalium.logic.feature.message.draft.RemoveMessageDraftUseCase
import com.wire.kalium.logic.feature.message.draft.SaveMessageDraftUseCase
import com.wire.kalium.logic.feature.message.ephemeral.EnqueueMessageSelfDeletionUseCase
import com.wire.kalium.logic.feature.selfDeletingMessages.ObserveSelfDeletionTimerSettingsForConversationUseCase
import com.wire.kalium.logic.feature.selfDeletingMessages.PersistNewSelfDeletionTimerUseCase
Expand Down Expand Up @@ -120,6 +123,8 @@ class MessageComposerViewModel @Inject constructor(
private val setNotifiedAboutConversationUnderLegalHold: SetNotifiedAboutConversationUnderLegalHoldUseCase,
private val observeConversationUnderLegalHoldNotified: ObserveConversationUnderLegalHoldNotifiedUseCase,
private val sendLocation: SendLocationUseCase,
private val saveMessageDraft: SaveMessageDraftUseCase,
private val removeMessageDraft: RemoveMessageDraftUseCase
) : SavedStateViewModel(savedStateHandle) {

var messageComposerViewState = mutableStateOf(MessageComposerViewState())
Expand Down Expand Up @@ -240,6 +245,7 @@ class MessageComposerViewModel @Inject constructor(
mentions = newMentions.map { it.intoMessageMention() },
).handleLegalHoldFailureAfterSendingMessage()
}
removeMessageDraft(conversationId)
sendTypingEvent(conversationId, TypingIndicatorMode.STOPPED)
}

Expand All @@ -265,6 +271,7 @@ class MessageComposerViewModel @Inject constructor(
quotedMessageId = quotedMessageId
).handleLegalHoldFailureAfterSendingMessage()
}
removeMessageDraft(conversationId)
sendTypingEvent(conversationId, TypingIndicatorMode.STOPPED)
}

Expand Down Expand Up @@ -506,6 +513,12 @@ class MessageComposerViewModel @Inject constructor(
}
}

fun saveDraft(messageDraft: MessageDraft) {
viewModelScope.launch {
saveMessageDraft(conversationId, messageDraft)
}
}

fun acceptSureAboutSendingMessage() {
(sureAboutMessagingDialogState as? SureAboutMessagingDialogState.Visible)?.let {
viewModelScope.launch {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Wire
* Copyright (C) 2024 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.messages.draft

import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.wire.android.navigation.SavedStateViewModel
import com.wire.android.ui.home.conversations.ConversationNavArgs
import com.wire.android.ui.home.conversations.model.UIQuotedMessage
import com.wire.android.ui.home.conversations.model.toUiMention
import com.wire.android.ui.home.conversations.usecase.GetQuoteMessageForConversationUseCase
import com.wire.android.ui.home.messagecomposer.model.MessageComposition
import com.wire.android.ui.home.messagecomposer.model.update
import com.wire.android.ui.navArgs
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.feature.message.draft.GetMessageDraftUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MessageDraftViewModel @Inject constructor(
override val savedStateHandle: SavedStateHandle,
private val getMessageDraft: GetMessageDraftUseCase,
private val getQuotedMessage: GetQuoteMessageForConversationUseCase,
) : SavedStateViewModel(savedStateHandle) {

private val conversationNavArgs: ConversationNavArgs = savedStateHandle.navArgs()
val conversationId: QualifiedID = conversationNavArgs.conversationId

var state = mutableStateOf(MessageComposition.DEFAULT.copy(messageTextFieldValue = TextFieldValue("")))
private set

init {
loadMessageDraft()
}

private fun loadMessageDraft() {
viewModelScope.launch {
val draftResult = getMessageDraft(conversationId)

draftResult?.let { draft ->
state.update { messageComposition ->
messageComposition.copy(
messageTextFieldValue = TextFieldValue(draft.text),
selectedMentions = draft.selectedMentionList.mapNotNull { it.toUiMention(draft.text) },
editMessageId = draft.editMessageId
)
}
}
draftResult?.quotedMessageId?.let { quotedMessageId ->
when (val quotedData = getQuotedMessage(conversationId, quotedMessageId)) {
is UIQuotedMessage.UIQuotedData -> {
state.update {
it.copy(
quotedMessage = quotedData,
quotedMessageId = quotedMessageId
)
}
}

UIQuotedMessage.UnavailableData -> {}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Wire
* Copyright (C) 2024 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.model

import com.wire.kalium.logic.data.message.mention.MessageMention
import com.wire.kalium.logic.data.user.UserId

data class UIMention(
val start: Int,
val length: Int,
val userId: UserId,
val handler: String // name that should be displayed in a message
) {
fun intoMessageMention() = MessageMention(start, length, userId, false) // We can never send a self mention message
}

fun MessageMention.toUiMention(originalText: String): UIMention? =
if (start + length <= originalText.length && originalText.elementAt(start) == '@') {
UIMention(
start = start,
length = length,
userId = userId,
handler = originalText.substring(start, start + length)
)
} else {
null
}
Original file line number Diff line number Diff line change
Expand Up @@ -580,41 +580,6 @@ data class MessageBody(
val quotedMessage: UIQuotedMessage? = null
)

sealed class UIQuotedMessage {

object UnavailableData : UIQuotedMessage()

data class UIQuotedData(
val messageId: String,
val senderId: UserId,
val senderName: UIText,
val originalMessageDateDescription: UIText,
val editedTimeDescription: UIText?,
val quotedContent: Content
) : UIQuotedMessage() {

sealed interface Content

data class Text(val value: String) : Content

data class GenericAsset(
val assetName: String?,
val assetMimeType: String
) : Content

data class DisplayableImage(
val displayable: ImageAsset.PrivateAsset
) : Content

data class Location(val locationName: String) : Content

object AudioMessage : Content

object Deleted : Content
object Invalid : Content
}
}

enum class MessageSource {
Self, OtherUser
}
Expand Down
Loading

0 comments on commit 06d1399

Please sign in to comment.