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: message draft [WPB-1021] #2796

Merged
merged 11 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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 Down Expand Up @@ -1047,10 +1052,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,82 @@
/*
* 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.map { it.toUiMention(draft.text) }
)
}
}
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,37 @@
/*
* 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

Check warning on line 27 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIMention.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIMention.kt#L23-L27

Added lines #L23 - L27 were not covered by tests
) {
fun intoMessageMention() = MessageMention(start, length, userId, false) // We can never send a self mention message

Check warning on line 29 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIMention.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIMention.kt#L29

Added line #L29 was not covered by tests
}

fun MessageMention.toUiMention(originalText: String) = UIMention(
start = start,
length = length,
userId = userId,
handler = originalText.substring(start, start + length)

Check warning on line 36 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIMention.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIMention.kt#L32-L36

Added lines #L32 - L36 were not covered by tests
)
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.android.appLogger
import com.wire.android.model.ImageAsset
import com.wire.android.util.ui.UIText
import com.wire.kalium.logic.data.user.UserId

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

Check warning on line 44 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt#L42-L44

Added lines #L42 - L44 were not covered by tests
) : Content

data class DisplayableImage(
val displayable: ImageAsset.PrivateAsset

Check warning on line 48 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt#L47-L48

Added lines #L47 - L48 were not covered by tests
) : Content

data class Location(val locationName: String) : Content

Check warning on line 51 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt#L51

Added line #L51 was not covered by tests

object AudioMessage : Content

object Deleted : Content
object Invalid : Content
}
}

fun UIMessage.Regular.mapToQuotedContent(): UIQuotedMessage.UIQuotedData.Content? =
when (val messageContent = messageContent) {

Check warning on line 61 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt#L61

Added line #L61 was not covered by tests
is UIMessageContent.AssetMessage -> UIQuotedMessage.UIQuotedData.GenericAsset(
assetName = messageContent.assetName,
assetMimeType = messageContent.assetExtension

Check warning on line 64 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt#L63-L64

Added lines #L63 - L64 were not covered by tests
)

is UIMessageContent.RestrictedAsset -> UIQuotedMessage.UIQuotedData.GenericAsset(
assetName = messageContent.assetName,
assetMimeType = messageContent.mimeType

Check warning on line 69 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt#L68-L69

Added lines #L68 - L69 were not covered by tests
)

is UIMessageContent.TextMessage -> UIQuotedMessage.UIQuotedData.Text(
value = messageContent.messageBody.message.asString(null)

Check warning on line 73 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt#L73

Added line #L73 was not covered by tests
)

is UIMessageContent.AudioAssetMessage -> UIQuotedMessage.UIQuotedData.AudioMessage

is UIMessageContent.ImageMessage -> messageContent.asset?.let {
UIQuotedMessage.UIQuotedData.DisplayableImage(
displayable = messageContent.asset

Check warning on line 80 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt#L79-L80

Added lines #L79 - L80 were not covered by tests
)
}

is UIMessageContent.Location -> with(messageContent) {
UIQuotedMessage.UIQuotedData.Location(locationName = name)

Check warning on line 85 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt#L85

Added line #L85 was not covered by tests
}

else -> {
appLogger.w("Attempting to reply to an unsupported message type of content = $messageContent")
null

Check warning on line 90 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIQuotedMessage.kt#L89-L90

Added lines #L89 - L90 were not covered by tests
}
}
Loading
Loading