Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -10,16 +10,30 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.bitwarden.ui.platform.base.util.cardStyle
import com.bitwarden.ui.platform.components.button.BitwardenStandardIconButton
import com.bitwarden.ui.platform.components.dialog.BitwardenSelectionDialog
import com.bitwarden.ui.platform.components.dialog.row.BitwardenBasicDialogRow
import com.bitwarden.ui.platform.components.icon.BitwardenIcon
import com.bitwarden.ui.platform.components.icon.model.IconData
import com.bitwarden.ui.platform.components.model.CardStyle
import com.bitwarden.ui.platform.resource.BitwardenDrawable
import com.bitwarden.ui.platform.resource.BitwardenString
import com.bitwarden.ui.platform.theme.BitwardenTheme
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf

/**
* A reusable composable function that displays a group item.
Expand All @@ -33,6 +47,7 @@ import com.bitwarden.ui.platform.theme.BitwardenTheme
* @param modifier The [Modifier] to be applied to the [Row] composable that holds the list item.
* @param subLabel The secondary text label to be displayed in the group item.
* @param endIcon The [IconData] object used to draw the icon at the end of the group item.
* @param selectionDataList Optional overflow menu options shown via a more-options button.
*/
@Composable
fun BitwardenGroupItem(
Expand All @@ -44,14 +59,27 @@ fun BitwardenGroupItem(
modifier: Modifier = Modifier,
subLabel: String? = null,
endIcon: IconData.Local? = null,
selectionDataList: ImmutableList<SelectionItemData> = persistentListOf(),
) {
var shouldShowDialog by rememberSaveable { mutableStateOf(false) }
Row(
modifier = modifier
.defaultMinSize(minHeight = 60.dp)
.cardStyle(
cardStyle = cardStyle,
onClick = onClick,
paddingHorizontal = 16.dp,
.then(
if (selectionDataList.isEmpty()) {
Modifier.cardStyle(
cardStyle = cardStyle,
onClick = onClick,
paddingHorizontal = 16.dp,
)
} else {
Modifier.cardStyle(
cardStyle = cardStyle,
onClick = onClick,
paddingStart = 16.dp,
paddingEnd = 4.dp,
)
},
),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(16.dp),
Expand Down Expand Up @@ -96,6 +124,35 @@ fun BitwardenGroupItem(
.size(size = 24.dp),
)
}
if (selectionDataList.isNotEmpty()) {
BitwardenStandardIconButton(
vectorIconRes = BitwardenDrawable.ic_ellipsis_horizontal,
contentDescription = stringResource(id = BitwardenString.more_options),
onClick = { shouldShowDialog = true },
modifier = Modifier.testTag(tag = "FolderMoreOptionsButton"),
)
}
}

if (shouldShowDialog) {
BitwardenSelectionDialog(
title = label,
onDismissRequest = { shouldShowDialog = false },
selectionItems = {
selectionDataList.forEach { itemData ->
BitwardenBasicDialogRow(
modifier = Modifier
.semantics { contentDescription = itemData.contentDescription }
.testTag(tag = "AlertSelectionOption"),
text = itemData.text,
onClick = {
shouldShowDialog = false
itemData.onClick()
},
)
}
},
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class FolderAddEditViewModel @Inject constructor(
},
) {
init {
vaultRepository
.foldersStateFlow
.onEach { sendAction(FolderAddEditAction.Internal.FoldersReceive(it)) }
.launchIn(viewModelScope)

state
.folderAddEditType
.folderId
Expand All @@ -76,6 +81,7 @@ class FolderAddEditViewModel @Inject constructor(
is FolderAddEditAction.NameTextChange -> handleNameTextChange(action)
is FolderAddEditAction.SaveClick -> handleSaveClick()
is FolderAddEditAction.Internal.VaultDataReceive -> handleVaultDataReceive(action)
is FolderAddEditAction.Internal.FoldersReceive -> handleFoldersReceive(action)
is FolderAddEditAction.Internal.CreateFolderResultReceive ->
handleCreateFolderResultReceive(action)

Expand All @@ -92,7 +98,7 @@ class FolderAddEditViewModel @Inject constructor(
}

private fun handleSaveClick() = onContent { content ->
if (content.folderName.isEmpty()) {
if (content.folderName.isBlank()) {
mutableStateFlow.update {
it.copy(
dialog = FolderAddEditState.DialogState.Error(
Expand All @@ -104,11 +110,46 @@ class FolderAddEditViewModel @Inject constructor(
return@onContent
}

val folderName = content.folderName.trim()
val resolvedFolderName = state
.parentFolderName
?.let { "$it/" }
.orEmpty() + folderName

if (!state.hasLoadedExistingFolders) {
mutableStateFlow.update {
it.copy(
dialog = FolderAddEditState.DialogState.Loading(
BitwardenString.saving.asText(),
),
pendingSaveFolderName = resolvedFolderName,
)
}
return@onContent
}

if (isDuplicateFolderName(folderName = resolvedFolderName)) {
mutableStateFlow.update {
it.copy(
dialog = FolderAddEditState.DialogState.Error(
message = BitwardenString.a_folder_with_this_name_already_exists.asText(),
),
pendingSaveFolderName = null,
)
}
return@onContent
}

createOrUpdateFolder(resolvedFolderName = resolvedFolderName)
}

private fun createOrUpdateFolder(resolvedFolderName: String) = onContent { content ->
mutableStateFlow.update {
it.copy(
dialog = FolderAddEditState.DialogState.Loading(
BitwardenString.saving.asText(),
),
pendingSaveFolderName = null,
)
}

Expand All @@ -117,13 +158,7 @@ class FolderAddEditViewModel @Inject constructor(
FolderAddEditType.AddItem -> {
val result = vaultRepository.createFolder(
FolderView(
name = state
.parentFolderName
?.let {
"$it/"
}
.orEmpty() +
content.folderName,
name = resolvedFolderName,
id = folderAddEditType.folderId,
revisionDate = clock.instant(),
),
Expand All @@ -135,7 +170,7 @@ class FolderAddEditViewModel @Inject constructor(
val result = vaultRepository.updateFolder(
folderAddEditType.folderId,
FolderView(
name = content.folderName,
name = content.folderName.trim(),
id = folderAddEditType.folderId,
revisionDate = clock.instant(),
),
Expand All @@ -146,6 +181,51 @@ class FolderAddEditViewModel @Inject constructor(
}
}

private fun isDuplicateFolderName(folderName: String): Boolean {
val currentFolderId = state.folderAddEditType.folderId
return state.existingFolders.any { folder ->
folder.id != currentFolderId &&
folder.name.equals(folderName, ignoreCase = true)
}
}

private fun handleFoldersReceive(action: FolderAddEditAction.Internal.FoldersReceive) {
val folders = action.foldersState.data
?.map { folder ->
FolderAddEditState.ExistingFolder(
id = folder.id,
name = folder.name,
)
}
.orEmpty()
val hasLoaded = action.foldersState !is DataState.Loading
val pendingSaveFolderName = state.pendingSaveFolderName

mutableStateFlow.update {
it.copy(
existingFolders = folders,
hasLoadedExistingFolders = hasLoaded || folders.isNotEmpty(),
)
}

if (pendingSaveFolderName != null && (hasLoaded || folders.isNotEmpty())) {
if (isDuplicateFolderName(folderName = pendingSaveFolderName)) {
mutableStateFlow.update {
it.copy(
dialog = FolderAddEditState.DialogState.Error(
message = BitwardenString
.a_folder_with_this_name_already_exists
.asText(),
),
pendingSaveFolderName = null,
)
}
} else {
createOrUpdateFolder(resolvedFolderName = pendingSaveFolderName)
}
}
}

private fun handleDeleteClick() {
val folderId = state.folderAddEditType.folderId ?: return

Expand Down Expand Up @@ -361,8 +441,20 @@ data class FolderAddEditState(
val viewState: ViewState,
val dialog: DialogState?,
val parentFolderName: String?,
val existingFolders: List<ExistingFolder> = emptyList(),
val hasLoadedExistingFolders: Boolean = false,
val pendingSaveFolderName: String? = null,
) : Parcelable {

/**
* A previously created folder used for duplicate-name validation.
*/
@Parcelize
data class ExistingFolder(
val id: String?,
val name: String,
) : Parcelable

/**
* Helper to determine whether we show the overflow menu.
*/
Expand Down Expand Up @@ -499,5 +591,12 @@ sealed class FolderAddEditAction {
data class VaultDataReceive(
val vaultDataState: DataState<FolderView?>,
) : Internal()

/**
* Indicates that the list of existing folders has been received.
*/
data class FoldersReceive(
val foldersState: DataState<List<FolderView>>,
) : Internal()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ fun NavGraphBuilder.vaultUnlockedGraph(
parentFolderName = it,
)
},
onNavigateToEditFolderScreen = {
navController.navigateToFolderAddEdit(
folderAddEditType = FolderAddEditType.EditItem(folderId = it),
)
},
onNavigateToFlightRecorder = {
navController.navigateToFlightRecorder(isPreAuth = false)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fun NavGraphBuilder.vaultUnlockedNavBarDestination(
onNavigateToRecordedLogs: () -> Unit,
onNavigateToImportLogins: () -> Unit,
onNavigateToAddFolderScreen: (selectedFolderName: String?) -> Unit,
onNavigateToEditFolderScreen: (folderId: String) -> Unit,
onNavigateToAboutPrivilegedApps: () -> Unit,
onNavigateToManageDevices: () -> Unit,
onNavigateToPlan: () -> Unit,
Expand All @@ -75,6 +76,7 @@ fun NavGraphBuilder.vaultUnlockedNavBarDestination(
onNavigateToSetupBrowserAutofill = onNavigateToSetupBrowserAutofill,
onNavigateToImportLogins = onNavigateToImportLogins,
onNavigateToAddFolderScreen = onNavigateToAddFolderScreen,
onNavigateToEditFolderScreen = onNavigateToEditFolderScreen,
onNavigateToFlightRecorder = onNavigateToFlightRecorder,
onNavigateToRecordedLogs = onNavigateToRecordedLogs,
onNavigateToAboutPrivilegedApps = onNavigateToAboutPrivilegedApps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fun VaultUnlockedNavBarScreen(
onNavigateToRecordedLogs: () -> Unit,
onNavigateToImportLogins: () -> Unit,
onNavigateToAddFolderScreen: (selectedFolderId: String?) -> Unit,
onNavigateToEditFolderScreen: (folderId: String) -> Unit,
onNavigateToAboutPrivilegedApps: () -> Unit,
onNavigateToManageDevices: () -> Unit,
onNavigateToPlan: () -> Unit,
Expand Down Expand Up @@ -109,6 +110,7 @@ fun VaultUnlockedNavBarScreen(
onNavigateToSetupBrowserAutofill = onNavigateToSetupBrowserAutofill,
onNavigateToImportLogins = onNavigateToImportLogins,
onNavigateToAddFolderScreen = onNavigateToAddFolderScreen,
onNavigateToEditFolderScreen = onNavigateToEditFolderScreen,
onNavigateToFlightRecorder = onNavigateToFlightRecorder,
onNavigateToRecordedLogs = onNavigateToRecordedLogs,
onNavigateToAboutPrivilegedApps = onNavigateToAboutPrivilegedApps,
Expand Down Expand Up @@ -149,6 +151,7 @@ private fun VaultUnlockedNavBarScaffold(
onNavigateToRecordedLogs: () -> Unit,
onNavigateToImportLogins: () -> Unit,
onNavigateToAddFolderScreen: (selectedFolderId: String?) -> Unit,
onNavigateToEditFolderScreen: (folderId: String) -> Unit,
onNavigateToAboutPrivilegedApps: () -> Unit,
onNavigateToManageDevices: () -> Unit,
onNavigateToPlan: () -> Unit,
Expand Down Expand Up @@ -203,6 +206,7 @@ private fun VaultUnlockedNavBarScaffold(
onDimBottomNavBarRequest = { shouldDim -> shouldDimNavBar = shouldDim },
onNavigateToImportLogins = onNavigateToImportLogins,
onNavigateToAddFolderScreen = onNavigateToAddFolderScreen,
onNavigateToEditFolderScreen = onNavigateToEditFolderScreen,
onNavigateToAboutScreen = {
navController.navigateToSettingsGraphRoot()
navController.navigateToAbout(isPreAuth = false)
Expand Down
Loading