Delete sources from DB and reload items on source deletion.

This commit is contained in:
aminecmi 2022-12-26 22:27:28 +01:00
parent 8c83a9408b
commit 27eafe4ff4
4 changed files with 20 additions and 6 deletions

View File

@ -78,9 +78,9 @@ class SourcesListAdapter(
val deleteBtn: Button = mView.findViewById(R.id.deleteBtn)
deleteBtn.setOnClickListener {
val (id) = items[bindingAdapterPosition]
val (id, title) = items[bindingAdapterPosition]
CoroutineScope(Dispatchers.IO).launch {
val successfullyDeletedSource = repository.deleteSource(id)
val successfullyDeletedSource = repository.deleteSource(id, title)
if (successfullyDeletedSource) {
items.removeAt(bindingAdapterPosition)
notifyItemRemoved(bindingAdapterPosition)

View File

@ -58,6 +58,7 @@ class RepositoryTest {
data = SelfossModel.Stats(NUMBER_ARTICLES, NUMBER_UNREAD, NUMBER_STARRED)
)
every { db.itemsQueries.deleteItemsWhereSource(any()) } returns Unit
every { db.itemsQueries.items().executeAsList() } returns generateTestDBItems()
every { db.tagsQueries.deleteAllTags() } returns Unit
every { db.tagsQueries.transaction(any(), any()) } returns Unit
@ -798,10 +799,11 @@ class RepositoryTest {
initializeRepository()
var response: Boolean
runBlocking {
response = repository.deleteSource(5)
response = repository.deleteSource(5, "src")
}
coVerify(exactly = 1) { api.deleteSource(5) }
coVerify(exactly = 1) { db.itemsQueries.deleteItemsWhereSource("src") }
assertSame(true, response)
}
@ -812,10 +814,11 @@ class RepositoryTest {
initializeRepository()
var response: Boolean
runBlocking {
response = repository.deleteSource(5)
response = repository.deleteSource(5, "src")
}
coVerify(exactly = 1) { api.deleteSource(5) }
coVerify(exactly = 0) { db.itemsQueries.deleteItemsWhereSource("src") }
assertSame(false, response)
}
@ -826,10 +829,11 @@ class RepositoryTest {
initializeRepository(MutableStateFlow(false))
var response: Boolean
runBlocking {
response = repository.deleteSource(5)
response = repository.deleteSource(5, "src")
}
coVerify(exactly = 0) { api.deleteSource(5) }
coVerify(exactly = 1) { db.itemsQueries.deleteItemsWhereSource("src") }
assertSame(false, response)
}

View File

@ -359,13 +359,20 @@ class Repository(
return response
}
suspend fun deleteSource(id: Int): Boolean {
suspend fun deleteSource(id: Int, title: String): Boolean {
var success = false
if (isNetworkAvailable()) {
val response = api.deleteSource(id)
success = response.isSuccess
}
// We filter on success or if the network isn't available
if (success || !isNetworkAvailable()) {
items = ArrayList(items.filter { it.sourcetitle != title })
setReaderItems(items)
db.itemsQueries.deleteItemsWhereSource(title)
}
return success
}

View File

@ -27,5 +27,8 @@ INSERT OR REPLACE INTO ITEM VALUES ?;
deleteItem:
DELETE FROM ITEM WHERE `id` = ?;
deleteItemsWhereSource:
DELETE FROM ITEM WHERE `sourcetitle` = ?;
updateItem:
UPDATE ITEM SET `datetime` = ?, `title` = ?, `content` = ?, `unread` = ?, `starred` = ?, `thumbnail` = ?, `icon` = ?, `link` = ?, `sourcetitle` = ?, `tags` = ? WHERE `id` = ?;