diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenListItem.kt b/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenListItem.kt index f6c9bc167..6f50a0570 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenListItem.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenListItem.kt @@ -31,6 +31,8 @@ import androidx.compose.ui.unit.dp import com.x8bit.bitwarden.R import com.x8bit.bitwarden.ui.platform.components.model.IconResource import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme +import kotlinx.collections.immutable.PersistentList +import kotlinx.collections.immutable.persistentListOf /** * A Composable function that displays a row item. @@ -51,7 +53,7 @@ fun BitwardenListItem( label: String, startIcon: Painter, onClick: () -> Unit, - selectionDataList: List, + selectionDataList: PersistentList, modifier: Modifier = Modifier, supportingLabel: String? = null, trailingLabelIcons: List = emptyList(), @@ -158,7 +160,7 @@ private fun BitwardenListItem_preview() { supportingLabel = "Jan 3, 2024, 10:35 AM", startIcon = painterResource(id = R.drawable.ic_send_text), onClick = {}, - selectionDataList = emptyList(), + selectionDataList = persistentListOf(), ) } } diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/platform/util/PersistentListExtensions.kt b/app/src/main/java/com/x8bit/bitwarden/ui/platform/util/PersistentListExtensions.kt new file mode 100644 index 000000000..eef37a292 --- /dev/null +++ b/app/src/main/java/com/x8bit/bitwarden/ui/platform/util/PersistentListExtensions.kt @@ -0,0 +1,10 @@ +package com.x8bit.bitwarden.ui.platform.util + +import kotlinx.collections.immutable.PersistentList +import kotlinx.collections.immutable.toPersistentList + +/** + * Creates an immutable [PersistentList] of the given [elements] excluding the null ones. + */ +fun persistentListOfNotNull(vararg elements: T?): PersistentList = + elements.filterNotNull().toPersistentList() diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/SendListItem.kt b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/SendListItem.kt index a8506bd3d..12382ffea 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/SendListItem.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/SendListItem.kt @@ -12,6 +12,7 @@ import com.x8bit.bitwarden.ui.platform.components.SelectionItemData import com.x8bit.bitwarden.ui.platform.components.model.IconResource import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme import com.x8bit.bitwarden.ui.tools.feature.send.model.SendStatusIcon +import kotlinx.collections.immutable.persistentListOf /** * A Composable function that displays a row send item. @@ -50,7 +51,7 @@ fun SendListItem( ) }, onClick = onClick, - selectionDataList = listOf( + selectionDataList = persistentListOf( SelectionItemData( text = stringResource(id = R.string.edit), onClick = onEditClick, diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendScreen.kt b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendScreen.kt index c6eecb418..c2ad3a7ad 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendScreen.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/send/addsend/AddSendScreen.kt @@ -31,8 +31,8 @@ import com.x8bit.bitwarden.ui.platform.components.BitwardenTextButton import com.x8bit.bitwarden.ui.platform.components.BitwardenTopAppBar import com.x8bit.bitwarden.ui.platform.components.LoadingDialogState import com.x8bit.bitwarden.ui.platform.components.OverflowMenuItemData +import com.x8bit.bitwarden.ui.platform.util.persistentListOfNotNull import com.x8bit.bitwarden.ui.tools.feature.send.addsend.handlers.AddSendHandlers -import kotlinx.collections.immutable.toPersistentList /** * Displays new send UX. @@ -92,7 +92,7 @@ fun AddSendScreen( ) if (!state.isAddMode) { BitwardenOverflowActionItem( - menuItemDataList = listOfNotNull( + menuItemDataList = persistentListOfNotNull( OverflowMenuItemData( text = stringResource(id = R.string.remove_password), onClick = remember(viewModel) { @@ -122,8 +122,7 @@ fun AddSendScreen( { viewModel.trySendAction(AddSendAction.DeleteClick) } }, ), - ) - .toPersistentList(), + ), ) } }, diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/vault/VaultEntryListItem.kt b/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/vault/VaultEntryListItem.kt index 9837a9a7b..dbcbb5f5a 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/vault/VaultEntryListItem.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/vault/VaultEntryListItem.kt @@ -11,6 +11,7 @@ import com.x8bit.bitwarden.R import com.x8bit.bitwarden.ui.platform.components.BitwardenListItem import com.x8bit.bitwarden.ui.platform.components.SelectionItemData import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme +import kotlinx.collections.immutable.persistentListOf /** * A Composable function that displays a row item for different types of vault entries. @@ -36,7 +37,7 @@ fun VaultEntryListItem( supportingLabel = supportingLabel, startIcon = startIcon, onClick = onClick, - selectionDataList = listOf( + selectionDataList = persistentListOf( SelectionItemData( text = "Not yet implemented", onClick = { diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/platform/util/PersistentListExtensionsTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/platform/util/PersistentListExtensionsTest.kt new file mode 100644 index 000000000..41ba2d1f2 --- /dev/null +++ b/app/src/test/java/com/x8bit/bitwarden/ui/platform/util/PersistentListExtensionsTest.kt @@ -0,0 +1,23 @@ +package com.x8bit.bitwarden.ui.platform.util + +import kotlinx.collections.immutable.persistentListOf +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class PersistentListExtensionsTest { + + @Test + fun `PersistentListOfNotNull should filter out null values`() { + val expected = persistentListOf("Hello", "World") + + val result = persistentListOfNotNull( + "Hello", + null, + "World", + null, + null, + ) + + assertEquals(expected, result) + } +}