Skip to content
Merged
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 @@ -85,8 +85,8 @@ fun NavGraphBuilder.dashboardGraph(
)
}
},
onItemDelete = { deleted, firstItemId ->
if (openedItemId == deleted) {
onItemsDelete = { deleted, firstItemId ->
if (openedItemId in deleted) {
scope.launch {
if (!isSinglePaneMode && !isModifyScreenActive)
firstItemId?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ internal interface ItemDao {
@Upsert
suspend fun upsert(item: ItemEntity): Long

@Query("DELETE FROM item WHERE id = :id")
suspend fun delete(id: ItemId)
@Query("DELETE FROM item WHERE id IN (:ids)")
suspend fun delete(ids: Collection<ItemId>)

@Query("SELECT name FROM item WHERE id = :id")
suspend fun getNameById(id: ItemId): String?
Expand Down Expand Up @@ -60,11 +60,11 @@ internal interface ItemDao {
OR tm.item_id IS NOT NULL)
"""
)
suspend fun searchItem(
fun searchItem(
query: String,
normalizedQuery: String,
itemType: VaultItemType? = null,
): List<LightweightItemSearchResult>
): Flow<List<LightweightItemSearchResult>>

@Query("UPDATE item SET pinned = :pinned WHERE id = :id")
suspend fun setPinned(id: ItemId, pinned: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ internal class ItemRepositoryImpl(
private val tagDao: TagDao,
) : ItemRepository {

override suspend fun deleteItem(itemId: ItemId): Unit = database.withTransaction {
val tagIds = tagDao.tagIdsForItem(itemId)
itemDao.delete(itemId)
override suspend fun deleteItems(itemIds: Set<ItemId>): Unit = database.withTransaction {
if (itemIds.isEmpty()) return@withTransaction

// Collected before the delete: once the rows are gone the cross refs are too, so the
// orphan sweep would have nothing left to look at.
val tagIds = itemIds.flatMap { tagDao.tagIdsForItem(it) }.distinct()
itemDao.delete(itemIds)
if (tagIds.isNotEmpty()) tagDao.pruneOrphans(tagIds)
}

Expand All @@ -55,11 +59,12 @@ internal class ItemRepositoryImpl(
vaultId: VaultId?,
): Boolean = itemDao.existsName(name, excludeId, vaultId)

override suspend fun searchVaultItem(
override fun searchVaultItem(
query: String,
itemType: VaultItemType?,
): List<LiteItemSearchResult> = itemDao.searchItem(query, Tag.normalize(query), itemType)
.map(LightweightItemSearchResult::toDomain)
): Flow<List<LiteItemSearchResult>> =
itemDao.searchItem(query, Tag.normalize(query), itemType)
.map { results -> results.map(LightweightItemSearchResult::toDomain) }

override suspend fun setPinned(itemId: ItemId, pinned: Boolean) =
itemDao.setPinned(itemId, pinned)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlinx.coroutines.flow.Flow

interface ItemRepository {

suspend fun deleteItem(itemId: ItemId)
suspend fun deleteItems(itemIds: Set<ItemId>)

suspend fun createOrUpdateVaultItem(item: Item): ItemId

Expand All @@ -26,10 +26,10 @@ interface ItemRepository {
vaultId: VaultId? = null
): Boolean

suspend fun searchVaultItem(
fun searchVaultItem(
query: String,
itemType: VaultItemType? = null
): List<LiteItemSearchResult>
): Flow<List<LiteItemSearchResult>>

suspend fun setPinned(itemId: ItemId, pinned: Boolean)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import de.davis.keygo.core.item.domain.model.Vault
import de.davis.keygo.core.item.generated.domain.model.VaultItemType
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import kotlin.test.AfterTest
Expand Down Expand Up @@ -83,7 +84,7 @@ internal class ItemDaoSearchTest {
fun `searchItem matches an item by tag value only`() = runTest {
val id = insertItem(name = "Chase", note = null, tags = setOf("Banking"))

val results = itemDao.searchItem(query = "Bank", normalizedQuery = "bank")
val results = itemDao.searchItem(query = "Bank", normalizedQuery = "bank").first()

assertEquals(1, results.size)
val r = results.single()
Expand All @@ -97,7 +98,7 @@ internal class ItemDaoSearchTest {
fun `searchItem still matches by name and sets matchedName`() = runTest {
insertItem(name = "Bank of Earth")

val r = itemDao.searchItem(query = "Bank", normalizedQuery = "bank").single()
val r = itemDao.searchItem(query = "Bank", normalizedQuery = "bank").first().single()

assertTrue(r.matchedName)
assertFalse(r.matchedTag)
Expand All @@ -107,7 +108,7 @@ internal class ItemDaoSearchTest {
fun `searchItem does not return items with no name note or tag match`() = runTest {
insertItem(name = "Email", note = "personal", tags = setOf("Mail"))

assertTrue(itemDao.searchItem(query = "Bank", normalizedQuery = "bank").isEmpty())
assertTrue(itemDao.searchItem(query = "Bank", normalizedQuery = "bank").first().isEmpty())
}

@Test
Expand All @@ -120,7 +121,7 @@ internal class ItemDaoSearchTest {
query = "Bank",
normalizedQuery = "bank",
itemType = VaultItemType.Login,
).size,
).first().size,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ internal class TagDaoTest {
val item = insertItem()
tagDao.syncTags(item, setOf(tag("Work")))

db.itemDao().delete(item)
db.itemDao().delete(setOf(item))

assertEquals(emptyList(), tagDao.tagIdsForItem(item))
}
Expand All @@ -128,7 +128,7 @@ internal class TagDaoTest {
tagDao.syncTags(item, setOf(tag("Temp")))
val tagIds = tagDao.tagIdsForItem(item)

db.itemDao().delete(item)
db.itemDao().delete(setOf(item))
tagDao.pruneOrphans(tagIds)

assertNull(tagDao.findIdByNormalized("temp"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import de.davis.keygo.core.item.data.local.dao.ItemDao
import de.davis.keygo.core.item.data.local.dao.TagDao
import de.davis.keygo.core.item.data.local.datasource.ItemDatabase
import de.davis.keygo.core.item.data.local.pojo.ItemTagProjection
import de.davis.keygo.core.item.domain.alias.ItemId
import de.davis.keygo.core.item.domain.alias.newItemId
import de.davis.keygo.core.item.domain.model.Tag
import io.mockk.coEvery
Expand Down Expand Up @@ -46,29 +47,60 @@ class ItemRepositoryImplTest {
fun tearDown() = unmockkStatic("androidx.room.RoomDatabaseKt")

@Test
fun `deleteItem captures tag ids, deletes item, then prunes those tags`() = runTest {
fun `deleteItems captures tag ids, deletes items, then prunes those tags`() = runTest {
val id = newItemId()
coEvery { tagDao.tagIdsForItem(id) } returns listOf(7L, 9L)

repository.deleteItem(id)
repository.deleteItems(setOf(id))

coVerifyOrder {
tagDao.tagIdsForItem(id)
itemDao.delete(id)
itemDao.delete(setOf(id))
tagDao.pruneOrphans(listOf(7L, 9L))
}
}

@Test
fun `deleteItem skips prune when item had no tags`() = runTest {
fun `deleteItems skips prune when items had no tags`() = runTest {
val id = newItemId()
coEvery { tagDao.tagIdsForItem(id) } returns emptyList()

repository.deleteItem(id)
repository.deleteItems(setOf(id))

coVerify(exactly = 0) { tagDao.pruneOrphans(any()) }
}

@Test
fun `deleteItems removes every id in one dao call`() = runTest {
val first = newItemId()
val second = newItemId()
coEvery { tagDao.tagIdsForItem(any()) } returns emptyList()

repository.deleteItems(setOf(first, second))

coVerify(exactly = 1) { itemDao.delete(setOf(first, second)) }
}

@Test
fun `deleteItems prunes each tag once when items share a tag`() = runTest {
val first = newItemId()
val second = newItemId()
coEvery { tagDao.tagIdsForItem(first) } returns listOf(7L)
coEvery { tagDao.tagIdsForItem(second) } returns listOf(7L, 9L)

repository.deleteItems(setOf(first, second))

coVerify { tagDao.pruneOrphans(listOf(7L, 9L)) }
}

@Test
fun `deleteItems touches nothing when the set is empty`() = runTest {
repository.deleteItems(emptySet())

coVerify(exactly = 0) { itemDao.delete(any<Collection<ItemId>>()) }
coVerify(exactly = 0) { tagDao.pruneOrphans(any()) }
}

@Test
fun `observeTagsByItem groups rows into a tag set per item`() = runTest {
val a = newItemId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import de.davis.keygo.core.item.domain.repository.ItemRepository
import de.davis.keygo.core.item.generated.domain.model.VaultItemType
import de.davis.keygo.core.util.Result
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update

/**
* In-memory [ItemRepository] for tests.
Expand Down Expand Up @@ -68,7 +68,13 @@ class FakeItemRepository(
.associate { item -> item.id to item.tags.toSet() }
}

override suspend fun deleteItem(itemId: ItemId) = Unit
override suspend fun deleteItems(itemIds: Set<ItemId>) {
allStores.update { store -> store - itemIds }
itemIds.forEach { id ->
envelopes.remove(id)
itemTypes.remove(id)
}
}

override suspend fun createOrUpdateVaultItem(item: Item): ItemId = item.id

Expand All @@ -88,15 +94,36 @@ class FakeItemRepository(
(vaultId == null || it.vaultId == vaultId)
}

override suspend fun searchVaultItem(
override fun searchVaultItem(
query: String,
itemType: VaultItemType?,
): List<LiteItemSearchResult> = emptyList()
): Flow<List<LiteItemSearchResult>> = allStores.map { store ->
store.values
.filter { itemType == null || it.itemType == itemType }
.mapNotNull { item ->
val matchedName = item.name.contains(query, ignoreCase = true)
val matchedNote = item.note?.contains(query, ignoreCase = true) == true
val matchedTag = item.tags.any { it.display.contains(query, ignoreCase = true) }
if (!matchedName && !matchedNote && !matchedTag) return@mapNotNull null

LiteItemSearchResult(
id = item.id,
name = item.name,
itemType = item.itemType,
pinned = item.pinned,
matchedName = matchedName,
matchedNote = matchedNote,
matchedTag = matchedTag,
)
}
}

override suspend fun setPinned(itemId: ItemId, pinned: Boolean) = Unit

override fun observeLiteVaultItems(vaultId: VaultId?): Flow<List<LiteItem>> =
flowOf(emptyList())
allStores.map { store ->
store.values.filter { vaultId == null || it.vaultId == vaultId }
}

override suspend fun getItemKeyEnvelope(itemId: ItemId): ItemKeyEnvelope? = envelopes[itemId]

Expand Down
Loading
Loading