From 3b8f4991e9992b8c761b4e966b81ae2345d52948 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Tue, 23 Aug 2022 16:56:04 +0200 Subject: [PATCH] Inserting items in the DB. --- .../apps/readerforselfossv2/android/HomeActivity.kt | 10 +++------- .../android/background/background.kt | 7 +------ .../readerforselfossv2/repository/RepositoryImpl.kt | 12 ++++++++++++ 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index 217f667..184129a 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -168,14 +168,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar getElementsAccordingToTab() + CoroutineScope(Dispatchers.Main).launch { + repository.tryToCacheItemsAndGetNewOnes() + } - - // TODO: items caching should be done in the background when starting the app, like background.kt does -// fetchedItems?.let { -// CoroutineScope(Dispatchers.Main).launch { -// insertDBItems(it) -// } -// } } private fun handleSwipeRefreshLayout() { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt index fe51e23..93f16be 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt @@ -81,12 +81,7 @@ override fun doWork(): Result { } launch { - try { - val newItems = repository.getMaxItemsForBackground(ItemType.UNREAD) - handleNewItemsNotification(newItems, notifyNewItems, notificationManager) - repository.getMaxItemsForBackground(ItemType.ALL) - repository.getMaxItemsForBackground(ItemType.STARRED) - } catch (e: Throwable) {} + handleNewItemsNotification(repository.tryToCacheItemsAndGetNewOnes(), notifyNewItems, notificationManager) } } } 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 511b7d8..96269cb 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 @@ -413,4 +413,16 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails private fun updateDBItem(item: SelfossModel.Item) = db.itemsQueries.updateItem(item.datetime, item.getTitleDecoded(), item.content, item.unread, item.starred, item.thumbnail, item.icon, item.link, item.sourcetitle, item.tags.joinToString(","), item.id.toString()) + + + suspend fun tryToCacheItemsAndGetNewOnes(): List? { + try { + val newItems = getMaxItemsForBackground(ItemType.UNREAD) + val allItems = getMaxItemsForBackground(ItemType.ALL) + val starredItems = getMaxItemsForBackground(ItemType.STARRED) + insertDBItems(newItems.orEmpty() + allItems.orEmpty() + starredItems.orEmpty()) + return newItems + } catch (e: Throwable) {} + return emptyList() + } } \ No newline at end of file