Skip to content

Commit

Permalink
feat: add and remove conversation favorite
Browse files Browse the repository at this point in the history
  • Loading branch information
Garzas committed Nov 21, 2024
1 parent ccd6ef4 commit 7e2785c
Show file tree
Hide file tree
Showing 22 changed files with 341 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,4 @@ class ConversationModule {
@Provides
fun provideRemoveConversationFromFavoritesUseCase(conversationScope: ConversationScope) =
conversationScope.removeConversationFromFavorites

}
7 changes: 7 additions & 0 deletions app/src/main/kotlin/com/wire/android/model/SnackBarMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ interface SnackBarMessage {
val uiText: UIText
val actionLabel: UIText? get() = null
}

data class DefaultSnackBarMessage(
override val uiText: UIText,
override val actionLabel: UIText? = null
) : SnackBarMessage

fun UIText.asSnackBarMessage(actionLabel: UIText? = null): SnackBarMessage = DefaultSnackBarMessage(this, actionLabel)
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.wire.kalium.logic.data.user.UserId
fun ConversationSheetContent(
conversationSheetState: ConversationSheetState,
onMutingConversationStatusChange: () -> Unit,
changeFavoriteState: (conversationId: ConversationId, isFavorite: Boolean) -> Unit,
changeFavoriteState: (GroupDialogState, addToFavorite: Boolean) -> Unit,
moveConversationToFolder: () -> Unit,
updateConversationArchiveStatus: (DialogState) -> Unit,
clearConversationContent: (DialogState) -> Unit,
Expand Down Expand Up @@ -148,8 +148,8 @@ data class ConversationSheetContent(
conversationTypeDetail is ConversationTypeDetail.Private && conversationTypeDetail.blockingState == BlockingState.BLOCKED

fun canAddToFavourite(): Boolean = isFavorite != null &&
((conversationTypeDetail is ConversationTypeDetail.Private && conversationTypeDetail.blockingState != BlockingState.BLOCKED)
|| conversationTypeDetail is ConversationTypeDetail.Group)
((conversationTypeDetail is ConversationTypeDetail.Private && conversationTypeDetail.blockingState != BlockingState.BLOCKED)
|| conversationTypeDetail is ConversationTypeDetail.Group)

fun isAbandonedOneOnOneConversation(participantsCount: Int): Boolean = title.isEmpty() && participantsCount == 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ import com.wire.android.ui.home.conversationslist.model.getMutedStatusTextResour
import com.wire.android.ui.theme.wireColorScheme
import com.wire.android.ui.theme.wireTypography
import com.wire.kalium.logic.data.conversation.MutedConversationStatus
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.user.ConnectionState

// items cannot be simplified
@Suppress("CyclomaticComplexMethod")
@Composable
internal fun ConversationMainSheetContent(
conversationSheetContent: ConversationSheetContent,
// TODO(profile): enable when implemented
changeFavoriteState: (conversationId: ConversationId, isFavorite: Boolean) -> Unit,
changeFavoriteState: (dialogState: GroupDialogState, addToFavorite: Boolean) -> Unit,
// moveConversationToFolder: () -> Unit,
updateConversationArchiveStatus: (DialogState) -> Unit,
clearConversationContent: (DialogState) -> Unit,
Expand Down Expand Up @@ -110,7 +111,7 @@ internal fun ConversationMainSheetContent(
}
}

if (conversationSheetContent.canAddToFavourite() && !conversationSheetContent.isArchived)
if (conversationSheetContent.canAddToFavourite() && !conversationSheetContent.isArchived) {
conversationSheetContent.isFavorite?.let { isFavorite ->
if (isFavorite) {
add {
Expand All @@ -122,7 +123,15 @@ internal fun ConversationMainSheetContent(
contentDescription = stringResource(R.string.content_description_remove_from_favourites),
)
},
onItemClick = { changeFavoriteState(conversationSheetContent.conversationId, true) }
onItemClick = {
changeFavoriteState(
GroupDialogState(
conversationSheetContent.conversationId,
conversationSheetContent.title
),
false
)
}
)
}
} else {
Expand All @@ -135,11 +144,20 @@ internal fun ConversationMainSheetContent(
contentDescription = stringResource(R.string.content_description_add_to_favourite),
)
},
onItemClick = { changeFavoriteState(conversationSheetContent.conversationId, false) }
onItemClick = {
changeFavoriteState(
GroupDialogState(
conversationSheetContent.conversationId,
conversationSheetContent.title
),
true
)
}
)
}
}
}
}
// TODO(profile): enable when implemented
// add {
// MenuBottomSheetItem(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.common.bottomsheet.folder

import com.wire.android.di.ScopedArgs
import kotlinx.serialization.Serializable

@Serializable
object ChangeConversationFavoriteStateArgs : ScopedArgs {
override val key = "ConnectionActionButtonArgsKey"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.common.bottomsheet.folder

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wire.android.R
import com.wire.android.di.ViewModelScopedPreview
import com.wire.android.model.SnackBarMessage
import com.wire.android.model.asSnackBarMessage
import com.wire.android.ui.home.conversationslist.model.GroupDialogState
import com.wire.android.util.ui.UIText
import com.wire.kalium.logic.feature.conversation.folder.AddConversationToFavoritesUseCase
import com.wire.kalium.logic.feature.conversation.folder.RemoveConversationFromFavoritesUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@ViewModelScopedPreview
interface ChangeConversationFavoriteVM {
val infoMessage: SharedFlow<SnackBarMessage>
get() = MutableSharedFlow()

fun changeFavoriteState(dialogState: GroupDialogState, addToFavorite: Boolean) {}
}

@HiltViewModel
class ChangeConversationFavoriteVMImpl @Inject constructor(
private val addConversationToFavorites: AddConversationToFavoritesUseCase,
private val removeConversationFromFavorites: RemoveConversationFromFavoritesUseCase,
) : ChangeConversationFavoriteVM, ViewModel() {

private val _infoMessage = MutableSharedFlow<SnackBarMessage>()
override val infoMessage = _infoMessage.asSharedFlow()

override fun changeFavoriteState(dialogState: GroupDialogState, addToFavorite: Boolean) {
viewModelScope.launch {
val messageResource = if (addToFavorite) {
when (addConversationToFavorites(dialogState.conversationId)) {
is AddConversationToFavoritesUseCase.Result.Failure -> R.string.error_adding_to_favorite
AddConversationToFavoritesUseCase.Result.Success -> R.string.success_adding_to_favorite
}
} else {
when (removeConversationFromFavorites(dialogState.conversationId)) {
is RemoveConversationFromFavoritesUseCase.Result.Failure -> R.string.error_removing_from_favorite
RemoveConversationFromFavoritesUseCase.Result.Success -> R.string.success_removing_from_favorite
}
}
_infoMessage.emit(UIText.StringResource(messageResource, dialogState.conversationName).asSnackBarMessage())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.result.ResultRecipient
import com.wire.android.R
import com.wire.android.appLogger
import com.wire.android.di.hiltViewModelScoped
import com.wire.android.navigation.NavigationCommand
import com.wire.android.navigation.Navigator
import com.wire.android.navigation.WireDestination
Expand All @@ -78,6 +79,9 @@ import com.wire.android.ui.common.bottomsheet.WireModalSheetLayout
import com.wire.android.ui.common.bottomsheet.conversation.ConversationSheetContent
import com.wire.android.ui.common.bottomsheet.conversation.ConversationTypeDetail
import com.wire.android.ui.common.bottomsheet.conversation.rememberConversationSheetState
import com.wire.android.ui.common.bottomsheet.folder.ChangeConversationFavoriteStateArgs
import com.wire.android.ui.common.bottomsheet.folder.ChangeConversationFavoriteVM
import com.wire.android.ui.common.bottomsheet.folder.ChangeConversationFavoriteVMImpl
import com.wire.android.ui.common.bottomsheet.rememberWireModalSheetState
import com.wire.android.ui.common.bottomsheet.show
import com.wire.android.ui.common.button.WirePrimaryButton
Expand Down Expand Up @@ -285,7 +289,11 @@ private fun GroupConversationDetailsContent(
isLoading: Boolean,
isAbandonedOneOnOneConversation: Boolean,
onSearchConversationMessagesClick: () -> Unit,
onConversationMediaClick: () -> Unit
onConversationMediaClick: () -> Unit,
changeConversationFavoriteStateViewModel: ChangeConversationFavoriteVM =
hiltViewModelScoped<ChangeConversationFavoriteVMImpl, ChangeConversationFavoriteVM, ChangeConversationFavoriteStateArgs>(
ChangeConversationFavoriteStateArgs
),
) {
val scope = rememberCoroutineScope()
val resources = LocalContext.current.resources
Expand Down Expand Up @@ -461,7 +469,7 @@ private fun GroupConversationDetailsContent(
)
}
},
changeFavoriteState = bottomSheetEventsHandler::changeFavoriteState,
changeFavoriteState = changeConversationFavoriteStateViewModel::changeFavoriteState,
moveConversationToFolder = bottomSheetEventsHandler::onMoveConversationToFolder,
updateConversationArchiveStatus = {
// Only show the confirmation dialog if the conversation is not archived
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ import com.wire.kalium.logic.feature.conversation.UpdateConversationAccessRoleUs
import com.wire.kalium.logic.feature.conversation.UpdateConversationArchivedStatusUseCase
import com.wire.kalium.logic.feature.conversation.UpdateConversationMutedStatusUseCase
import com.wire.kalium.logic.feature.conversation.UpdateConversationReceiptModeUseCase
import com.wire.kalium.logic.feature.conversation.folder.AddConversationToFavoritesUseCase
import com.wire.kalium.logic.feature.conversation.folder.RemoveConversationFromFavoritesUseCase
import com.wire.kalium.logic.feature.publicuser.RefreshUsersWithoutMetadataUseCase
import com.wire.kalium.logic.feature.selfDeletingMessages.ObserveSelfDeletionTimerSettingsForConversationUseCase
import com.wire.kalium.logic.feature.team.DeleteTeamConversationUseCase
Expand Down Expand Up @@ -102,8 +100,6 @@ class GroupConversationDetailsViewModel @Inject constructor(
override val savedStateHandle: SavedStateHandle,
private val isMLSEnabled: IsMLSEnabledUseCase,
private val getDefaultProtocol: GetDefaultProtocolUseCase,
private val addConversationToFavorites: AddConversationToFavoritesUseCase,
private val removeConversationFromFavorites: RemoveConversationFromFavoritesUseCase,
refreshUsersWithoutMetadata: RefreshUsersWithoutMetadataUseCase,
) : GroupConversationParticipantsViewModel(
savedStateHandle, observeConversationMembers, refreshUsersWithoutMetadata
Expand Down Expand Up @@ -379,16 +375,6 @@ class GroupConversationDetailsViewModel @Inject constructor(
}
}

override fun changeFavoriteState(conversationId: ConversationId, isFavorite: Boolean) {
viewModelScope.launch {
if (isFavorite) {
removeConversationFromFavorites(conversationId)
} else {
addConversationToFavorites(conversationId)
}
}
}

@Suppress("EmptyFunctionBlock")
override fun onMoveConversationToFolder(conversationId: ConversationId?) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.wire.kalium.util.DateTimeUtil
@Suppress("TooManyFunctions")
interface GroupConversationDetailsBottomSheetEventsHandler {
fun onMutingConversationStatusChange(conversationId: ConversationId?, status: MutedConversationStatus, onMessage: (UIText) -> Unit)
fun changeFavoriteState(conversationId: ConversationId, isFavorite: Boolean)
fun onMoveConversationToFolder(conversationId: ConversationId? = null)
fun updateConversationArchiveStatus(
dialogState: DialogState,
Expand All @@ -47,7 +46,6 @@ interface GroupConversationDetailsBottomSheetEventsHandler {
) {
}

override fun changeFavoriteState(conversationId: ConversationId, isFavorite: Boolean) {}
override fun onMoveConversationToFolder(conversationId: ConversationId?) {}
override fun updateConversationArchiveStatus(
dialogState: DialogState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ import com.wire.kalium.logic.feature.conversation.RefreshConversationsWithoutMet
import com.wire.kalium.logic.feature.conversation.RemoveMemberFromConversationUseCase
import com.wire.kalium.logic.feature.conversation.UpdateConversationArchivedStatusUseCase
import com.wire.kalium.logic.feature.conversation.UpdateConversationMutedStatusUseCase
import com.wire.kalium.logic.feature.conversation.folder.AddConversationToFavoritesUseCase
import com.wire.kalium.logic.feature.conversation.folder.RemoveConversationFromFavoritesUseCase
import com.wire.kalium.logic.feature.publicuser.RefreshUsersWithoutMetadataUseCase
import com.wire.kalium.logic.feature.team.DeleteTeamConversationUseCase
import com.wire.kalium.logic.feature.team.Result
Expand Down Expand Up @@ -108,7 +106,6 @@ interface ConversationListViewModel {
fun leaveGroup(leaveGroupState: GroupDialogState) {}
fun clearConversationContent(dialogState: DialogState) {}
fun muteConversation(conversationId: ConversationId?, mutedConversationStatus: MutedConversationStatus) {}
fun changeConversationFavoriteState(conversationId: ConversationId, isFavorite: Boolean) {}
fun moveConversationToFolder() {}
fun searchQueryChanged(searchQuery: String) {}
}
Expand All @@ -135,8 +132,6 @@ class ConversationListViewModelImpl @AssistedInject constructor(
private val refreshUsersWithoutMetadata: RefreshUsersWithoutMetadataUseCase,
private val refreshConversationsWithoutMetadata: RefreshConversationsWithoutMetadataUseCase,
private val updateConversationArchivedStatus: UpdateConversationArchivedStatusUseCase,
private val addConversationToFavorites: AddConversationToFavoritesUseCase,
private val removeConversationFromFavorites: RemoveConversationFromFavoritesUseCase,
@CurrentAccount val currentAccount: UserId,
private val wireSessionImageLoader: WireSessionImageLoader,
private val userTypeMapper: UserTypeMapper,
Expand Down Expand Up @@ -358,16 +353,6 @@ class ConversationListViewModelImpl @AssistedInject constructor(
}
}

override fun changeConversationFavoriteState(conversationId: ConversationId, isFavorite: Boolean) {
viewModelScope.launch {
if(isFavorite) {
removeConversationFromFavorites(conversationId)
} else {
addConversationToFavorites(conversationId)
}
}
}

// TODO: needs to be implemented
@Suppress("EmptyFunctionBlock")
override fun moveConversationToFolder() {
Expand Down
Loading

0 comments on commit 7e2785c

Please sign in to comment.