From f9ba13dc32524cf3c970d565d7594e45465c974d Mon Sep 17 00:00:00 2001 From: davidoskky Date: Fri, 30 Sep 2022 11:23:43 +0200 Subject: [PATCH] Always cache images in background --- .../readerforselfossv2/android/background/background.kt | 6 ++++-- .../apps/readerforselfossv2/repository/RepositoryImpl.kt | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) 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 74a0739..a565507 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 @@ -52,11 +52,13 @@ override fun doWork(): Result { repository.handleDBActions() + val apiItems = repository.tryToCacheItemsAndGetNewOnes() if (appSettingsService.isNotifyNewItemsEnabled()) { launch { - handleNewItemsNotification(repository.tryToCacheItemsAndGetNewOnes(), notificationManager) + handleNewItemsNotification(apiItems, notificationManager) } } + apiItems.map { it.preloadImages(context) } } } return Result.success() @@ -66,6 +68,7 @@ override fun doWork(): Result { newItems: List?, notificationManager: NotificationManager ) { + // TODO: Check if this coroutine is actually required CoroutineScope(Dispatchers.IO).launch { val apiItems = newItems.orEmpty() @@ -102,7 +105,6 @@ override fun doWork(): Result { notificationManager.notify(2, newItemsNotification.build()) } } - apiItems.map { it.preloadImages(context) } Timer("", false).schedule(4000) { notificationManager.cancel(1) } 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 c96d40b..eb6e794 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 @@ -456,16 +456,19 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap db.itemsQueries.updateItem(item.datetime, item.title.getHtmlDecoded(), item.content, item.unread, item.starred, item.thumbnail, item.icon, item.link, item.sourcetitle, item.tags.joinToString(","), item.id.toString()) // TODO: This function should check for duplicate items - suspend fun tryToCacheItemsAndGetNewOnes() { + suspend fun tryToCacheItemsAndGetNewOnes(): List { try { + val previousNewItems = getDBItems().count { it.unread } val newItems = getMaxItemsForBackground(ItemType.UNREAD) val allItems = getMaxItemsForBackground(ItemType.ALL) val starredItems = getMaxItemsForBackground(ItemType.STARRED) val fullItemsList = newItems + allItems + starredItems insertDBItems(fullItemsList) + return fullItemsList } catch (e: Throwable) { // We do nothing } + return emptyList() } // TODO: Add tests