-
Notifications
You must be signed in to change notification settings - Fork 26
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
chore: extract send message to viemodel #2824
Changes from 4 commits
b22b6ea
4af6060
72399fa
1dd135d
bdac706
23e02bf
94cb684
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
/* | ||
* 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.composer | ||
|
||
import android.net.Uri | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.viewModelScope | ||
import com.wire.android.mapper.ContactMapper | ||
import com.wire.android.navigation.SavedStateViewModel | ||
import com.wire.android.ui.home.conversations.ConversationNavArgs | ||
import com.wire.android.ui.home.conversations.InvalidLinkDialogState | ||
import com.wire.android.ui.home.conversations.MessageComposerViewState | ||
import com.wire.android.ui.home.conversations.VisitLinkDialogState | ||
import com.wire.android.ui.home.conversations.model.UIMessage | ||
import com.wire.android.ui.navArgs | ||
import com.wire.android.util.dispatchers.DispatcherProvider | ||
import com.wire.kalium.logic.configuration.FileSharingStatus | ||
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.feature.conversation.InteractionAvailability | ||
import com.wire.kalium.logic.feature.conversation.IsInteractionAvailableResult | ||
import com.wire.kalium.logic.feature.conversation.MembersToMentionUseCase | ||
import com.wire.kalium.logic.feature.conversation.ObserveConversationInteractionAvailabilityUseCase | ||
import com.wire.kalium.logic.feature.conversation.SendTypingEventUseCase | ||
import com.wire.kalium.logic.feature.conversation.UpdateConversationReadDateUseCase | ||
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 | ||
import com.wire.kalium.logic.feature.user.IsFileSharingEnabledUseCase | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import kotlinx.datetime.Instant | ||
import javax.inject.Inject | ||
|
||
@Suppress("LongParameterList", "TooManyFunctions") | ||
@HiltViewModel | ||
class MessageComposerViewModel @Inject constructor( | ||
override val savedStateHandle: SavedStateHandle, | ||
private val dispatchers: DispatcherProvider, | ||
private val isFileSharingEnabled: IsFileSharingEnabledUseCase, | ||
private val observeConversationInteractionAvailability: ObserveConversationInteractionAvailabilityUseCase, | ||
private val updateConversationReadDate: UpdateConversationReadDateUseCase, | ||
private val contactMapper: ContactMapper, | ||
private val membersToMention: MembersToMentionUseCase, | ||
private val enqueueMessageSelfDeletion: EnqueueMessageSelfDeletionUseCase, | ||
private val observeSelfDeletingMessages: ObserveSelfDeletionTimerSettingsForConversationUseCase, | ||
private val persistNewSelfDeletingStatus: PersistNewSelfDeletionTimerUseCase, | ||
private val sendTypingEvent: SendTypingEventUseCase, | ||
private val saveMessageDraft: SaveMessageDraftUseCase | ||
) : SavedStateViewModel(savedStateHandle) { | ||
|
||
var messageComposerViewState = mutableStateOf(MessageComposerViewState()) | ||
private set | ||
|
||
var tempWritableVideoUri: Uri? = null | ||
Check warning on line 78 in app/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt Codecov / codecov/patchapp/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt#L78
|
||
private set | ||
|
||
var tempWritableImageUri: Uri? = null | ||
Check warning on line 81 in app/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt Codecov / codecov/patchapp/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt#L81
|
||
private set | ||
|
||
private val conversationNavArgs: ConversationNavArgs = savedStateHandle.navArgs() | ||
val conversationId: QualifiedID = conversationNavArgs.conversationId | ||
|
||
var visitLinkDialogState: VisitLinkDialogState by mutableStateOf( | ||
VisitLinkDialogState.Hidden | ||
) | ||
|
||
var invalidLinkDialogState: InvalidLinkDialogState by mutableStateOf( | ||
InvalidLinkDialogState.Hidden | ||
) | ||
|
||
init { | ||
observeIsTypingAvailable() | ||
observeSelfDeletingMessagesStatus() | ||
setFileSharingStatus() | ||
} | ||
|
||
private fun observeIsTypingAvailable() = viewModelScope.launch { | ||
observeConversationInteractionAvailability(conversationId).collect { result -> | ||
messageComposerViewState.value = messageComposerViewState.value.copy( | ||
interactionAvailability = when (result) { | ||
is IsInteractionAvailableResult.Failure -> InteractionAvailability.DISABLED | ||
is IsInteractionAvailableResult.Success -> result.interactionAvailability | ||
} | ||
) | ||
} | ||
} | ||
|
||
private fun observeSelfDeletingMessagesStatus() = viewModelScope.launch { | ||
observeSelfDeletingMessages( | ||
conversationId, | ||
considerSelfUserSettings = true | ||
).collect { selfDeletingStatus -> | ||
messageComposerViewState.value = | ||
messageComposerViewState.value.copy(selfDeletionTimer = selfDeletingStatus) | ||
} | ||
} | ||
|
||
fun searchMembersToMention(searchQuery: String) { | ||
viewModelScope.launch(dispatchers.io()) { | ||
val members = membersToMention(conversationId, searchQuery).map { | ||
contactMapper.fromOtherUser(it.user as OtherUser) | ||
Check warning on line 125 in app/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt Codecov / codecov/patchapp/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt#L123-L125
|
||
} | ||
|
||
messageComposerViewState.value = | ||
messageComposerViewState.value.copy(mentionSearchResult = members) | ||
Check warning on line 129 in app/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt Codecov / codecov/patchapp/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt#L128-L129
|
||
} | ||
} | ||
|
||
fun clearMentionSearchResult() { | ||
messageComposerViewState.value = | ||
messageComposerViewState.value.copy(mentionSearchResult = emptyList()) | ||
Check warning on line 135 in app/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt Codecov / codecov/patchapp/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt#L134-L135
|
||
} | ||
|
||
private fun setFileSharingStatus() { | ||
// TODO: handle restriction when sending assets | ||
viewModelScope.launch { | ||
messageComposerViewState.value = when (isFileSharingEnabled().state) { | ||
FileSharingStatus.Value.Disabled, | ||
is FileSharingStatus.Value.EnabledSome -> | ||
messageComposerViewState.value.copy(isFileSharingEnabled = false) | ||
Check warning on line 144 in app/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt Codecov / codecov/patchapp/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt#L144
|
||
|
||
FileSharingStatus.Value.EnabledAll -> | ||
messageComposerViewState.value.copy(isFileSharingEnabled = true) | ||
} | ||
} | ||
} | ||
|
||
fun updateConversationReadDate(utcISO: String) { | ||
viewModelScope.launch(dispatchers.io()) { | ||
updateConversationReadDate(conversationId, Instant.parse(utcISO)) | ||
Check warning on line 154 in app/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt Codecov / codecov/patchapp/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt#L153-L154
|
||
} | ||
} | ||
|
||
fun startSelfDeletion(uiMessage: UIMessage) { | ||
enqueueMessageSelfDeletion(conversationId, uiMessage.header.messageId) | ||
Check warning on line 159 in app/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt Codecov / codecov/patchapp/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt#L159
|
||
} | ||
|
||
fun updateSelfDeletingMessages(newSelfDeletionTimer: SelfDeletionTimer) = | ||
viewModelScope.launch { | ||
messageComposerViewState.value = | ||
messageComposerViewState.value.copy(selfDeletionTimer = newSelfDeletionTimer) | ||
persistNewSelfDeletingStatus(conversationId, newSelfDeletionTimer) | ||
} | ||
|
||
fun hideVisitLinkDialog() { | ||
visitLinkDialogState = VisitLinkDialogState.Hidden | ||
Check warning on line 170 in app/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt Codecov / codecov/patchapp/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt#L170
|
||
} | ||
|
||
fun hideInvalidLinkError() { | ||
invalidLinkDialogState = InvalidLinkDialogState.Hidden | ||
Check warning on line 174 in app/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt Codecov / codecov/patchapp/src/main/kotlin/com/wire/android/ui/home/conversations/composer/MessageComposerViewModel.kt#L174
|
||
} | ||
|
||
fun sendTypingEvent(typingIndicatorMode: TypingIndicatorMode) { | ||
viewModelScope.launch { | ||
sendTypingEvent(conversationId, typingIndicatorMode) | ||
} | ||
} | ||
|
||
fun saveDraft(messageDraft: MessageDraft) { | ||
viewModelScope.launch { | ||
saveMessageDraft(conversationId, messageDraft) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we be passing a shared flow variable here correctly so it can be listened to? this just seems like a new MutableSharedFlow but no one is emitting to it? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used in 2 places where in one place it doesn't have second info message. I will try to figure better way to do it 💪
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe just make this parameter nullable 😄