From 27eafe4ff404b23dfac5734a9d39bf082a13dd14 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Mon, 26 Dec 2022 22:27:28 +0100 Subject: [PATCH] Delete sources from DB and reload items on source deletion. --- .../android/adapters/SourcesListAdapter.kt | 4 ++-- androidApp/src/test/kotlin/RepositoryTest.kt | 10 +++++++--- .../readerforselfossv2/repository/RepositoryImpl.kt | 9 ++++++++- .../bou/amine/apps/readerforselfossv2/dao/Items.sq | 3 +++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt index 906057d..e3e5857 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt @@ -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) diff --git a/androidApp/src/test/kotlin/RepositoryTest.kt b/androidApp/src/test/kotlin/RepositoryTest.kt index 84dee6f..28091c4 100644 --- a/androidApp/src/test/kotlin/RepositoryTest.kt +++ b/androidApp/src/test/kotlin/RepositoryTest.kt @@ -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) } diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index ce3808b..3979733 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -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 } diff --git a/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Items.sq b/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Items.sq index f85ab2b..ed9fa8e 100644 --- a/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Items.sq +++ b/shared/src/commonMain/sqldelight/bou/amine/apps/readerforselfossv2/dao/Items.sq @@ -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` = ?; \ No newline at end of file