Create sealed class for the ItemListingType Vault (#659)

This commit is contained in:
David Perez 2024-01-17 23:05:56 -06:00 committed by Álison Fernandes
parent 89a818aeaa
commit dfbfeda0e7
8 changed files with 181 additions and 175 deletions

View file

@ -25,14 +25,14 @@ fun VaultItemListingEmpty(
modifier: Modifier = Modifier,
) {
when (itemListingType) {
is VaultItemListingState.ItemListingType.Folder -> {
is VaultItemListingState.ItemListingType.Vault.Folder -> {
GenericNoItems(
modifier = modifier,
text = stringResource(id = R.string.no_items_folder),
)
}
is VaultItemListingState.ItemListingType.Trash -> {
is VaultItemListingState.ItemListingType.Vault.Trash -> {
GenericNoItems(
modifier = modifier,
text = stringResource(id = R.string.no_items_trash),

View file

@ -270,87 +270,79 @@ data class VaultItemListingState(
abstract val hasFab: Boolean
/**
* A Login item listing.
* Represents different types of vault item listings.
*/
data object Login : ItemListingType() {
override val titleText: Text
get() = R.string.logins.asText()
override val hasFab: Boolean
get() = true
}
sealed class Vault : ItemListingType() {
/**
* A Card item listing.
*/
data object Card : ItemListingType() {
override val titleText: Text
get() = R.string.cards.asText()
override val hasFab: Boolean
get() = true
}
/**
* A Login item listing.
*/
data object Login : Vault() {
override val titleText: Text get() = R.string.logins.asText()
override val hasFab: Boolean get() = true
}
/**
* An Identity item listing.
*/
data object Identity : ItemListingType() {
override val titleText: Text
get() = R.string.identities.asText()
override val hasFab: Boolean
get() = true
}
/**
* A Card item listing.
*/
data object Card : Vault() {
override val titleText: Text get() = R.string.cards.asText()
override val hasFab: Boolean get() = true
}
/**
* A Secure Note item listing.
*/
data object SecureNote : ItemListingType() {
override val titleText: Text
get() = R.string.secure_notes.asText()
override val hasFab: Boolean
get() = true
}
/**
* An Identity item listing.
*/
data object Identity : Vault() {
override val titleText: Text get() = R.string.identities.asText()
override val hasFab: Boolean get() = true
}
/**
* A Secure Trash item listing.
*/
data object Trash : ItemListingType() {
override val titleText: Text
get() = R.string.trash.asText()
override val hasFab: Boolean
get() = false
}
/**
* A Secure Note item listing.
*/
data object SecureNote : Vault() {
override val titleText: Text get() = R.string.secure_notes.asText()
override val hasFab: Boolean get() = true
}
/**
* A Folder item listing.
*
* @property folderId the id of the folder.
* @property folderName the name of the folder.
*/
data class Folder(
val folderId: String?,
// The folderName will always initially be an empty string
val folderName: String = "",
) : ItemListingType() {
override val titleText: Text
get() = folderName.asText()
override val hasFab: Boolean
get() = false
}
/**
* A Secure Trash item listing.
*/
data object Trash : Vault() {
override val titleText: Text get() = R.string.trash.asText()
override val hasFab: Boolean get() = false
}
/**
* A Collection item listing.
*
* @property collectionId the ID of the collection.
* @property collectionName the name of the collection.
*/
data class Collection(
val collectionId: String,
// The collectionName will always initially be an empty string
val collectionName: String = "",
) : ItemListingType() {
override val titleText: Text
get() = collectionName.asText()
override val hasFab: Boolean
get() = false
/**
* A Folder item listing.
*
* @property folderId the id of the folder.
* @property folderName the name of the folder.
*/
data class Folder(
val folderId: String?,
// The folderName will always initially be an empty string
val folderName: String = "",
) : Vault() {
override val titleText: Text get() = folderName.asText()
override val hasFab: Boolean get() = false
}
/**
* A Collection item listing.
*
* @property collectionId the ID of the collection.
* @property collectionName the name of the collection.
*/
data class Collection(
val collectionId: String,
// The collectionName will always initially be an empty string
val collectionName: String = "",
) : Vault() {
override val titleText: Text get() = collectionName.asText()
override val hasFab: Boolean get() = false
}
}
}
}

View file

@ -18,31 +18,31 @@ fun CipherView.determineListingPredicate(
itemListingType: VaultItemListingState.ItemListingType,
): Boolean =
when (itemListingType) {
is VaultItemListingState.ItemListingType.Card -> {
is VaultItemListingState.ItemListingType.Vault.Card -> {
type == CipherType.CARD && deletedDate == null
}
is VaultItemListingState.ItemListingType.Collection -> {
is VaultItemListingState.ItemListingType.Vault.Collection -> {
itemListingType.collectionId in this.collectionIds && deletedDate == null
}
is VaultItemListingState.ItemListingType.Folder -> {
is VaultItemListingState.ItemListingType.Vault.Folder -> {
folderId == itemListingType.folderId && deletedDate == null
}
is VaultItemListingState.ItemListingType.Identity -> {
is VaultItemListingState.ItemListingType.Vault.Identity -> {
type == CipherType.IDENTITY && deletedDate == null
}
is VaultItemListingState.ItemListingType.Login -> {
is VaultItemListingState.ItemListingType.Vault.Login -> {
type == CipherType.LOGIN && deletedDate == null
}
is VaultItemListingState.ItemListingType.SecureNote -> {
is VaultItemListingState.ItemListingType.Vault.SecureNote -> {
type == CipherType.SECURE_NOTE && deletedDate == null
}
is VaultItemListingState.ItemListingType.Trash -> {
is VaultItemListingState.ItemListingType.Vault.Trash -> {
deletedDate != null
}
}
@ -71,25 +71,25 @@ fun VaultItemListingState.ItemListingType.updateWithAdditionalDataIfNecessary(
collectionList: List<CollectionView>,
): VaultItemListingState.ItemListingType =
when (this) {
is VaultItemListingState.ItemListingType.Card -> this
is VaultItemListingState.ItemListingType.Collection -> copy(
is VaultItemListingState.ItemListingType.Vault.Card -> this
is VaultItemListingState.ItemListingType.Vault.Collection -> copy(
collectionName = collectionList
.find { it.id == collectionId }
?.name
.orEmpty(),
)
is VaultItemListingState.ItemListingType.Folder -> copy(
is VaultItemListingState.ItemListingType.Vault.Folder -> copy(
folderName = folderList
.find { it.id == folderId }
?.name
.orEmpty(),
)
is VaultItemListingState.ItemListingType.Identity -> this
is VaultItemListingState.ItemListingType.Login -> this
is VaultItemListingState.ItemListingType.SecureNote -> this
is VaultItemListingState.ItemListingType.Trash -> this
is VaultItemListingState.ItemListingType.Vault.Identity -> this
is VaultItemListingState.ItemListingType.Vault.Login -> this
is VaultItemListingState.ItemListingType.Vault.SecureNote -> this
is VaultItemListingState.ItemListingType.Vault.Trash -> this
}
private fun List<CipherView>.toDisplayItemList(

View file

@ -8,16 +8,16 @@ import com.x8bit.bitwarden.ui.vault.model.VaultItemListingType
*/
fun VaultItemListingType.toItemListingType(): VaultItemListingState.ItemListingType =
when (this) {
is VaultItemListingType.Card -> VaultItemListingState.ItemListingType.Card
is VaultItemListingType.Card -> VaultItemListingState.ItemListingType.Vault.Card
is VaultItemListingType.Folder -> {
VaultItemListingState.ItemListingType.Folder(folderId = folderId)
VaultItemListingState.ItemListingType.Vault.Folder(folderId = folderId)
}
is VaultItemListingType.Identity -> VaultItemListingState.ItemListingType.Identity
is VaultItemListingType.Login -> VaultItemListingState.ItemListingType.Login
is VaultItemListingType.SecureNote -> VaultItemListingState.ItemListingType.SecureNote
is VaultItemListingType.Trash -> VaultItemListingState.ItemListingType.Trash
is VaultItemListingType.Identity -> VaultItemListingState.ItemListingType.Vault.Identity
is VaultItemListingType.Login -> VaultItemListingState.ItemListingType.Vault.Login
is VaultItemListingType.SecureNote -> VaultItemListingState.ItemListingType.Vault.SecureNote
is VaultItemListingType.Trash -> VaultItemListingState.ItemListingType.Vault.Trash
is VaultItemListingType.Collection -> {
VaultItemListingState.ItemListingType.Collection(collectionId = collectionId)
VaultItemListingState.ItemListingType.Vault.Collection(collectionId = collectionId)
}
}

View file

@ -168,14 +168,18 @@ class VaultItemListingScreenTest : BaseComposeTest() {
.assertIsDisplayed()
mutableStateFlow.update {
it.copy(itemListingType = VaultItemListingState.ItemListingType.Trash)
it.copy(itemListingType = VaultItemListingState.ItemListingType.Vault.Trash)
}
composeTestRule
.onNodeWithText(text = "Add an Item")
.assertDoesNotExist()
mutableStateFlow.update {
it.copy(itemListingType = VaultItemListingState.ItemListingType.Folder(folderId = null))
it.copy(
itemListingType = VaultItemListingState.ItemListingType.Vault.Folder(
folderId = null,
),
)
}
composeTestRule
.onNodeWithText(text = "Add an Item")
@ -192,14 +196,18 @@ class VaultItemListingScreenTest : BaseComposeTest() {
.assertIsDisplayed()
mutableStateFlow.update {
it.copy(itemListingType = VaultItemListingState.ItemListingType.Trash)
it.copy(itemListingType = VaultItemListingState.ItemListingType.Vault.Trash)
}
composeTestRule
.onNodeWithText(text = "There are no items in the trash.")
.assertIsDisplayed()
mutableStateFlow.update {
it.copy(itemListingType = VaultItemListingState.ItemListingType.Folder(folderId = null))
it.copy(
itemListingType = VaultItemListingState.ItemListingType.Vault.Folder(
folderId = null,
),
)
}
composeTestRule
.onNodeWithText(text = "There are no items in this folder.")
@ -215,7 +223,7 @@ class VaultItemListingScreenTest : BaseComposeTest() {
.assertIsDisplayed()
mutableStateFlow.update {
it.copy(itemListingType = VaultItemListingState.ItemListingType.Trash)
it.copy(itemListingType = VaultItemListingState.ItemListingType.Vault.Trash)
}
composeTestRule
@ -223,7 +231,11 @@ class VaultItemListingScreenTest : BaseComposeTest() {
.assertDoesNotExist()
mutableStateFlow.update {
it.copy(itemListingType = VaultItemListingState.ItemListingType.Folder(folderId = null))
it.copy(
itemListingType = VaultItemListingState.ItemListingType.Vault.Folder(
folderId = null,
),
)
}
composeTestRule
@ -352,28 +364,28 @@ class VaultItemListingScreenTest : BaseComposeTest() {
.assertIsDisplayed()
mutableStateFlow.update {
it.copy(itemListingType = VaultItemListingState.ItemListingType.SecureNote)
it.copy(itemListingType = VaultItemListingState.ItemListingType.Vault.SecureNote)
}
composeTestRule
.onNodeWithText(text = "Secure notes")
.assertIsDisplayed()
mutableStateFlow.update {
it.copy(itemListingType = VaultItemListingState.ItemListingType.Card)
it.copy(itemListingType = VaultItemListingState.ItemListingType.Vault.Card)
}
composeTestRule
.onNodeWithText(text = "Cards")
.assertIsDisplayed()
mutableStateFlow.update {
it.copy(itemListingType = VaultItemListingState.ItemListingType.Identity)
it.copy(itemListingType = VaultItemListingState.ItemListingType.Vault.Identity)
}
composeTestRule
.onNodeWithText(text = "Identities")
.assertIsDisplayed()
mutableStateFlow.update {
it.copy(itemListingType = VaultItemListingState.ItemListingType.Trash)
it.copy(itemListingType = VaultItemListingState.ItemListingType.Vault.Trash)
}
composeTestRule
.onNodeWithText(text = "Trash")
@ -381,7 +393,7 @@ class VaultItemListingScreenTest : BaseComposeTest() {
mutableStateFlow.update {
it.copy(
itemListingType = VaultItemListingState.ItemListingType.Folder(
itemListingType = VaultItemListingState.ItemListingType.Vault.Folder(
folderId = "mockId",
folderName = "mockName",
),
@ -394,7 +406,7 @@ class VaultItemListingScreenTest : BaseComposeTest() {
}
private val DEFAULT_STATE = VaultItemListingState(
itemListingType = VaultItemListingState.ItemListingType.Login,
itemListingType = VaultItemListingState.ItemListingType.Vault.Login,
viewState = VaultItemListingState.ViewState.Loading,
isIconLoadingDisabled = false,
baseIconUrl = Environment.Us.environmentUrlData.baseIconUrl,

View file

@ -525,7 +525,7 @@ class VaultItemListingViewModelTest : BaseViewModelTest() {
@Suppress("MaxLineLength")
private fun createVaultItemListingState(
itemListingType: VaultItemListingState.ItemListingType = VaultItemListingState.ItemListingType.Login,
itemListingType: VaultItemListingState.ItemListingType = VaultItemListingState.ItemListingType.Vault.Login,
viewState: VaultItemListingState.ViewState = VaultItemListingState.ViewState.Loading,
): VaultItemListingState =
VaultItemListingState(

View file

@ -27,13 +27,13 @@ class VaultItemListingDataExtensionsTest {
)
mapOf(
VaultItemListingState.ItemListingType.Login to true,
VaultItemListingState.ItemListingType.Card to false,
VaultItemListingState.ItemListingType.SecureNote to false,
VaultItemListingState.ItemListingType.Identity to false,
VaultItemListingState.ItemListingType.Trash to false,
VaultItemListingState.ItemListingType.Folder(folderId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Collection(collectionId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Vault.Login to true,
VaultItemListingState.ItemListingType.Vault.Card to false,
VaultItemListingState.ItemListingType.Vault.SecureNote to false,
VaultItemListingState.ItemListingType.Vault.Identity to false,
VaultItemListingState.ItemListingType.Vault.Trash to false,
VaultItemListingState.ItemListingType.Vault.Folder(folderId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Vault.Collection(collectionId = "mockId-1") to true,
)
.forEach { (type, expected) ->
val result = cipherView.determineListingPredicate(
@ -56,13 +56,13 @@ class VaultItemListingDataExtensionsTest {
)
mapOf(
VaultItemListingState.ItemListingType.Login to false,
VaultItemListingState.ItemListingType.Card to false,
VaultItemListingState.ItemListingType.SecureNote to false,
VaultItemListingState.ItemListingType.Identity to false,
VaultItemListingState.ItemListingType.Trash to true,
VaultItemListingState.ItemListingType.Folder(folderId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Collection(collectionId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Vault.Login to false,
VaultItemListingState.ItemListingType.Vault.Card to false,
VaultItemListingState.ItemListingType.Vault.SecureNote to false,
VaultItemListingState.ItemListingType.Vault.Identity to false,
VaultItemListingState.ItemListingType.Vault.Trash to true,
VaultItemListingState.ItemListingType.Vault.Folder(folderId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Vault.Collection(collectionId = "mockId-1") to false,
)
.forEach { (type, expected) ->
val result = cipherView.determineListingPredicate(
@ -85,13 +85,13 @@ class VaultItemListingDataExtensionsTest {
)
mapOf(
VaultItemListingState.ItemListingType.Login to false,
VaultItemListingState.ItemListingType.Card to true,
VaultItemListingState.ItemListingType.SecureNote to false,
VaultItemListingState.ItemListingType.Identity to false,
VaultItemListingState.ItemListingType.Trash to false,
VaultItemListingState.ItemListingType.Folder(folderId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Collection(collectionId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Vault.Login to false,
VaultItemListingState.ItemListingType.Vault.Card to true,
VaultItemListingState.ItemListingType.Vault.SecureNote to false,
VaultItemListingState.ItemListingType.Vault.Identity to false,
VaultItemListingState.ItemListingType.Vault.Trash to false,
VaultItemListingState.ItemListingType.Vault.Folder(folderId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Vault.Collection(collectionId = "mockId-1") to true,
)
.forEach { (type, expected) ->
val result = cipherView.determineListingPredicate(
@ -114,13 +114,13 @@ class VaultItemListingDataExtensionsTest {
)
mapOf(
VaultItemListingState.ItemListingType.Login to false,
VaultItemListingState.ItemListingType.Card to false,
VaultItemListingState.ItemListingType.SecureNote to false,
VaultItemListingState.ItemListingType.Identity to false,
VaultItemListingState.ItemListingType.Trash to true,
VaultItemListingState.ItemListingType.Folder(folderId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Collection(collectionId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Vault.Login to false,
VaultItemListingState.ItemListingType.Vault.Card to false,
VaultItemListingState.ItemListingType.Vault.SecureNote to false,
VaultItemListingState.ItemListingType.Vault.Identity to false,
VaultItemListingState.ItemListingType.Vault.Trash to true,
VaultItemListingState.ItemListingType.Vault.Folder(folderId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Vault.Collection(collectionId = "mockId-1") to false,
)
.forEach { (type, expected) ->
val result = cipherView.determineListingPredicate(
@ -143,13 +143,13 @@ class VaultItemListingDataExtensionsTest {
)
mapOf(
VaultItemListingState.ItemListingType.Login to false,
VaultItemListingState.ItemListingType.Card to false,
VaultItemListingState.ItemListingType.SecureNote to false,
VaultItemListingState.ItemListingType.Identity to true,
VaultItemListingState.ItemListingType.Trash to false,
VaultItemListingState.ItemListingType.Folder(folderId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Collection(collectionId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Vault.Login to false,
VaultItemListingState.ItemListingType.Vault.Card to false,
VaultItemListingState.ItemListingType.Vault.SecureNote to false,
VaultItemListingState.ItemListingType.Vault.Identity to true,
VaultItemListingState.ItemListingType.Vault.Trash to false,
VaultItemListingState.ItemListingType.Vault.Folder(folderId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Vault.Collection(collectionId = "mockId-1") to true,
)
.forEach { (type, expected) ->
val result = cipherView.determineListingPredicate(
@ -172,13 +172,13 @@ class VaultItemListingDataExtensionsTest {
)
mapOf(
VaultItemListingState.ItemListingType.Login to false,
VaultItemListingState.ItemListingType.Card to false,
VaultItemListingState.ItemListingType.SecureNote to false,
VaultItemListingState.ItemListingType.Identity to false,
VaultItemListingState.ItemListingType.Trash to true,
VaultItemListingState.ItemListingType.Folder(folderId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Collection(collectionId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Vault.Login to false,
VaultItemListingState.ItemListingType.Vault.Card to false,
VaultItemListingState.ItemListingType.Vault.SecureNote to false,
VaultItemListingState.ItemListingType.Vault.Identity to false,
VaultItemListingState.ItemListingType.Vault.Trash to true,
VaultItemListingState.ItemListingType.Vault.Folder(folderId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Vault.Collection(collectionId = "mockId-1") to false,
)
.forEach { (type, expected) ->
val result = cipherView.determineListingPredicate(
@ -201,13 +201,13 @@ class VaultItemListingDataExtensionsTest {
)
mapOf(
VaultItemListingState.ItemListingType.Login to false,
VaultItemListingState.ItemListingType.Card to false,
VaultItemListingState.ItemListingType.SecureNote to true,
VaultItemListingState.ItemListingType.Identity to false,
VaultItemListingState.ItemListingType.Trash to false,
VaultItemListingState.ItemListingType.Folder(folderId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Collection(collectionId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Vault.Login to false,
VaultItemListingState.ItemListingType.Vault.Card to false,
VaultItemListingState.ItemListingType.Vault.SecureNote to true,
VaultItemListingState.ItemListingType.Vault.Identity to false,
VaultItemListingState.ItemListingType.Vault.Trash to false,
VaultItemListingState.ItemListingType.Vault.Folder(folderId = "mockId-1") to true,
VaultItemListingState.ItemListingType.Vault.Collection(collectionId = "mockId-1") to true,
)
.forEach { (type, expected) ->
val result = cipherView.determineListingPredicate(
@ -230,13 +230,13 @@ class VaultItemListingDataExtensionsTest {
)
mapOf(
VaultItemListingState.ItemListingType.Login to false,
VaultItemListingState.ItemListingType.Card to false,
VaultItemListingState.ItemListingType.SecureNote to false,
VaultItemListingState.ItemListingType.Identity to false,
VaultItemListingState.ItemListingType.Trash to true,
VaultItemListingState.ItemListingType.Folder(folderId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Collection(collectionId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Vault.Login to false,
VaultItemListingState.ItemListingType.Vault.Card to false,
VaultItemListingState.ItemListingType.Vault.SecureNote to false,
VaultItemListingState.ItemListingType.Vault.Identity to false,
VaultItemListingState.ItemListingType.Vault.Trash to true,
VaultItemListingState.ItemListingType.Vault.Folder(folderId = "mockId-1") to false,
VaultItemListingState.ItemListingType.Vault.Collection(collectionId = "mockId-1") to false,
)
.forEach { (type, expected) ->
val result = cipherView.determineListingPredicate(
@ -324,7 +324,7 @@ class VaultItemListingDataExtensionsTest {
createMockCollectionView(number = 3),
)
val result = VaultItemListingState.ItemListingType.Folder(
val result = VaultItemListingState.ItemListingType.Vault.Folder(
folderId = "mockId-1",
folderName = "wrong name",
)
@ -334,7 +334,7 @@ class VaultItemListingDataExtensionsTest {
)
assertEquals(
VaultItemListingState.ItemListingType.Folder(
VaultItemListingState.ItemListingType.Vault.Folder(
folderId = "mockId-1",
folderName = "mockName-1",
),
@ -355,7 +355,7 @@ class VaultItemListingDataExtensionsTest {
createMockCollectionView(number = 3),
)
val result = VaultItemListingState.ItemListingType.Collection(
val result = VaultItemListingState.ItemListingType.Vault.Collection(
collectionId = "mockId-1",
collectionName = "wrong name",
)
@ -365,7 +365,7 @@ class VaultItemListingDataExtensionsTest {
)
assertEquals(
VaultItemListingState.ItemListingType.Collection(
VaultItemListingState.ItemListingType.Vault.Collection(
collectionId = "mockId-1",
collectionName = "mockName-1",
),
@ -387,14 +387,14 @@ class VaultItemListingDataExtensionsTest {
createMockCollectionView(number = 3),
)
val result = VaultItemListingState.ItemListingType.Login
val result = VaultItemListingState.ItemListingType.Vault.Login
.updateWithAdditionalDataIfNecessary(
folderList = folderViewList,
collectionList = collectionViewList,
)
assertEquals(
VaultItemListingState.ItemListingType.Login,
VaultItemListingState.ItemListingType.Vault.Login,
result,
)
}

View file

@ -20,9 +20,11 @@ class VaultItemListingTypeExtensionsTest {
assertEquals(
listOf(
VaultItemListingState.ItemListingType.Folder(folderId = "mock"),
VaultItemListingState.ItemListingType.Trash,
VaultItemListingState.ItemListingType.Collection(collectionId = "collectionId"),
VaultItemListingState.ItemListingType.Vault.Folder(folderId = "mock"),
VaultItemListingState.ItemListingType.Vault.Trash,
VaultItemListingState.ItemListingType.Vault.Collection(
collectionId = "collectionId",
),
),
result,
)