Skip to content

Commit

Permalink
fix: handling group creation errors (#2016)
Browse files Browse the repository at this point in the history
  • Loading branch information
saleniuk committed Jul 27, 2023
1 parent 346ab45 commit b278248
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fun NewConversationRouter() {
NewGroupScreen(
onBackPressed = newConversationNavController::popBackStack,
newGroupState = newConversationViewModel.newGroupState,
createGroupState = newConversationViewModel.createGroupState,
onGroupNameChange = newConversationViewModel::onGroupNameChange,
onContinuePressed = {
if (newConversationViewModel.newGroupState.isSelfTeamMember == true) {
Expand All @@ -91,7 +92,16 @@ fun NewConversationRouter() {
newConversationViewModel.createGroup()
}
},
onGroupNameErrorAnimated = newConversationViewModel::onGroupNameErrorAnimated
onGroupNameErrorAnimated = newConversationViewModel::onGroupNameErrorAnimated,
onEditParticipantsClick = {
newConversationViewModel.onCreateGroupErrorDismiss()
newConversationNavController.popBackStack(
route = NewConversationNavigationItem.SearchListNavHostScreens.route,
inclusive = false
)
},
onDiscardGroupCreationClick = newConversationViewModel::onDiscardGroupCreationClick,
onErrorDismissed = newConversationViewModel::onCreateGroupErrorDismiss
)
}
)
Expand All @@ -102,21 +112,22 @@ fun NewConversationRouter() {
onBackPressed = newConversationNavController::popBackStack,
onCreateGroup = newConversationViewModel::createGroup,
groupOptionState = newConversationViewModel.groupOptionsState,
createGroupState = newConversationViewModel.createGroupState,
onAllowGuestChanged = newConversationViewModel::onAllowGuestStatusChanged,
onAllowServicesChanged = newConversationViewModel::onAllowServicesStatusChanged,
onReadReceiptChanged = newConversationViewModel::onReadReceiptStatusChanged,
onAllowGuestsDialogDismissed = newConversationViewModel::onAllowGuestsDialogDismissed,
onAllowGuestsClicked = newConversationViewModel::onAllowGuestsClicked,
onNotAllowGuestsClicked = newConversationViewModel::onNotAllowGuestClicked,
onEditParticipantsClick = {
newConversationViewModel.onGroupOptionsErrorDismiss()
newConversationViewModel.onCreateGroupErrorDismiss()
newConversationNavController.popBackStack(
route = NewConversationNavigationItem.SearchListNavHostScreens.route,
inclusive = false
)
},
onDiscardGroupCreationClick = newConversationViewModel::onDiscardGroupCreationClick,
onErrorDismissed = newConversationViewModel::onGroupOptionsErrorDismiss
onErrorDismissed = newConversationViewModel::onCreateGroupErrorDismiss
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.wire.android.ui.common.groupname.GroupMetadataState
import com.wire.android.ui.common.groupname.GroupNameValidator
import com.wire.android.ui.home.conversations.search.SearchAllPeopleViewModel
import com.wire.android.ui.home.conversationslist.model.Membership
import com.wire.android.ui.home.newconversation.common.CreateGroupState
import com.wire.android.ui.home.newconversation.groupOptions.GroupOptionState
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.kalium.logic.data.conversation.Conversation
Expand Down Expand Up @@ -84,6 +85,7 @@ class NewConversationViewModel @Inject constructor(
)

var groupOptionsState: GroupOptionState by mutableStateOf(GroupOptionState())
var createGroupState: CreateGroupState by mutableStateOf(CreateGroupState())

init {
viewModelScope.launch {
Expand All @@ -96,12 +98,12 @@ class NewConversationViewModel @Inject constructor(
newGroupState = GroupNameValidator.onGroupNameChange(newText, newGroupState)
}

fun onGroupOptionsErrorDismiss() {
groupOptionsState = groupOptionsState.copy(error = null)
fun onCreateGroupErrorDismiss() {
createGroupState = createGroupState.copy(error = null)
}

fun onDiscardGroupCreationClick() {
groupOptionsState = groupOptionsState.copy(error = null)
createGroupState = createGroupState.copy(error = null)
viewModelScope.launch {
navigationManager.navigate(NavigationCommand(NavigationItem.Home.getRouteWithArgs(), BackStackMode.CLEAR_WHOLE))
}
Expand Down Expand Up @@ -192,7 +194,7 @@ class NewConversationViewModel @Inject constructor(
private fun createGroupForTeamAccounts(shouldCheckGuests: Boolean = true) {
if (shouldCheckGuests && checkIfGuestAdded()) return
viewModelScope.launch {
newGroupState = newGroupState.copy(isLoading = true)
groupOptionsState = groupOptionsState.copy(isLoading = true)
val result = createGroupConversation(
name = newGroupState.groupName.text,
// TODO: change the id in Contact to UserId instead of String
Expand Down Expand Up @@ -226,19 +228,22 @@ class NewConversationViewModel @Inject constructor(

CreateGroupConversationUseCase.Result.SyncFailure -> {
appLogger.d("Can't create group due to SyncFailure")
groupOptionsState = groupOptionsState.copy(isLoading = false, error = GroupOptionState.Error.LackingConnection)
groupOptionsState = groupOptionsState.copy(isLoading = false)
newGroupState = newGroupState.copy(isLoading = false)
createGroupState = createGroupState.copy(error = CreateGroupState.Error.LackingConnection)
}

is CreateGroupConversationUseCase.Result.UnknownFailure -> {
appLogger.w("Error while creating a group ${result.cause}")
groupOptionsState = groupOptionsState.copy(isLoading = false, error = GroupOptionState.Error.Unknown)
groupOptionsState = groupOptionsState.copy(isLoading = false)
newGroupState = newGroupState.copy(isLoading = false)
createGroupState = createGroupState.copy(error = CreateGroupState.Error.Unknown)
}

is CreateGroupConversationUseCase.Result.BackendConflictFailure -> {
groupOptionsState = groupOptionsState.copy(
isLoading = false,
error = GroupOptionState.Error.ConflictedBackends(result.domains)
)
groupOptionsState = groupOptionsState.copy(isLoading = false)
newGroupState = newGroupState.copy(isLoading = false)
createGroupState = createGroupState.copy(error = CreateGroupState.Error.ConflictedBackends(result.domains))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Wire
* Copyright (C) 2023 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.newconversation.common

import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withStyle
import com.wire.android.R
import com.wire.android.ui.common.WireDialog
import com.wire.android.ui.common.WireDialogButtonProperties
import com.wire.android.ui.common.WireDialogButtonType
import com.wire.android.ui.common.colorsScheme
import com.wire.android.ui.markdown.MarkdownConstants
import com.wire.android.ui.theme.WireTheme
import com.wire.android.util.DialogAnnotatedErrorStrings
import com.wire.android.util.ui.PreviewMultipleThemes

@Composable
fun CreateGroupErrorDialog(
error: CreateGroupState.Error,
onDismiss: () -> Unit,
onAccept: () -> Unit,
onCancel: () -> Unit
) {
val dialogStrings = when (error) {
is CreateGroupState.Error.LackingConnection -> DialogAnnotatedErrorStrings(
stringResource(R.string.error_no_network_title),
buildAnnotatedString { append(stringResource(R.string.error_no_network_message)) }
)

is CreateGroupState.Error.Unknown -> DialogAnnotatedErrorStrings(
stringResource(R.string.error_unknown_title),
buildAnnotatedString { append(stringResource(R.string.error_unknown_message)) }
)

is CreateGroupState.Error.ConflictedBackends -> DialogAnnotatedErrorStrings(
title = stringResource(id = R.string.group_can_not_be_created_title),
annotatedMessage = buildAnnotatedString {
val description = stringResource(
id = R.string.group_can_not_be_created_federation_conflict_description,
error.domains.dropLast(1).joinToString(", "),
error.domains.last()
)
val learnMore = stringResource(id = R.string.label_learn_more)

append(description)
append(' ')

withStyle(
style = SpanStyle(
color = colorsScheme().primary,
textDecoration = TextDecoration.Underline
)
) {
append(learnMore)
}
addStringAnnotation(
tag = MarkdownConstants.TAG_URL,
annotation = stringResource(id = R.string.url_support),
start = description.length + 1,
end = description.length + 1 + learnMore.length
)
}
)
}

WireDialog(
dialogStrings.title,
dialogStrings.annotatedMessage,
onDismiss = onDismiss,
buttonsHorizontalAlignment = false,
optionButton1Properties = WireDialogButtonProperties(
onClick = if (error.isConflictedBackends) onAccept else onDismiss,
text = stringResource(
id = if (error.isConflictedBackends) {
R.string.group_can_not_be_created_edit_participiant_list
} else {
R.string.label_ok
}
),
type = WireDialogButtonType.Primary,
),
optionButton2Properties = if (error.isConflictedBackends) {
WireDialogButtonProperties(
onClick = onCancel,
text = stringResource(R.string.group_can_not_be_created_discard_group_creation),
type = WireDialogButtonType.Secondary,
)
} else {
null
},
)
}

@PreviewMultipleThemes
@Composable
private fun PreviewCreateGroupErrorDialogLackingConnection() {
WireTheme {
CreateGroupErrorDialog(CreateGroupState.Error.LackingConnection, {}, {}, {})
}
}
@PreviewMultipleThemes
@Composable
private fun PreviewCreateGroupErrorDialogUnknown() {
WireTheme {
CreateGroupErrorDialog(CreateGroupState.Error.Unknown, {}, {}, {})
}
}
@PreviewMultipleThemes
@Composable
private fun PreviewCreateGroupErrorDialogConflictedBackends() {
WireTheme {
CreateGroupErrorDialog(CreateGroupState.Error.ConflictedBackends(listOf("some.com", "other.com")), {}, {}, {})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Wire
* Copyright (C) 2023 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.newconversation.common

data class CreateGroupState(
val error: Error? = null
) {
sealed interface Error {
object Unknown : Error
object LackingConnection : Error
data class ConflictedBackends(val domains: List<String>) : Error

val isConflictedBackends get() = this is ConflictedBackends
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,5 @@ data class GroupOptionState(
val isAllowGuestEnabled: Boolean = true,
val isAllowServicesEnabled: Boolean = true,
val isReadReceiptEnabled: Boolean = true,
val showAllowGuestsDialog: Boolean = false,
val error: Error? = null
) {
sealed interface Error {
object Unknown : Error
object LackingConnection : Error
data class ConflictedBackends(val domains: List<String>) : Error

val isConflictedBackends get() = this is ConflictedBackends
}
}
val showAllowGuestsDialog: Boolean = false
)

0 comments on commit b278248

Please sign in to comment.