-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: message draft [WPB-1021] (#2796)
- Loading branch information
Showing
22 changed files
with
853 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ain/kotlin/com/wire/android/ui/home/conversations/messages/draft/MessageDraftViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -> {} | ||
} | ||
} | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
app/src/main/kotlin/com/wire/android/ui/home/conversations/model/UIMention.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.