Always cache images in background

This commit is contained in:
davidoskky 2022-09-30 11:23:43 +02:00
parent 6f60ef4346
commit f9ba13dc32
2 changed files with 8 additions and 3 deletions

View File

@ -52,11 +52,13 @@ override fun doWork(): Result {
repository.handleDBActions() repository.handleDBActions()
val apiItems = repository.tryToCacheItemsAndGetNewOnes()
if (appSettingsService.isNotifyNewItemsEnabled()) { if (appSettingsService.isNotifyNewItemsEnabled()) {
launch { launch {
handleNewItemsNotification(repository.tryToCacheItemsAndGetNewOnes(), notificationManager) handleNewItemsNotification(apiItems, notificationManager)
} }
} }
apiItems.map { it.preloadImages(context) }
} }
} }
return Result.success() return Result.success()
@ -66,6 +68,7 @@ override fun doWork(): Result {
newItems: List<SelfossModel.Item>?, newItems: List<SelfossModel.Item>?,
notificationManager: NotificationManager notificationManager: NotificationManager
) { ) {
// TODO: Check if this coroutine is actually required
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val apiItems = newItems.orEmpty() val apiItems = newItems.orEmpty()
@ -102,7 +105,6 @@ override fun doWork(): Result {
notificationManager.notify(2, newItemsNotification.build()) notificationManager.notify(2, newItemsNotification.build())
} }
} }
apiItems.map { it.preloadImages(context) }
Timer("", false).schedule(4000) { Timer("", false).schedule(4000) {
notificationManager.cancel(1) notificationManager.cancel(1)
} }

View File

@ -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()) 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 // TODO: This function should check for duplicate items
suspend fun tryToCacheItemsAndGetNewOnes() { suspend fun tryToCacheItemsAndGetNewOnes(): List<SelfossModel.Item> {
try { try {
val previousNewItems = getDBItems().count { it.unread }
val newItems = getMaxItemsForBackground(ItemType.UNREAD) val newItems = getMaxItemsForBackground(ItemType.UNREAD)
val allItems = getMaxItemsForBackground(ItemType.ALL) val allItems = getMaxItemsForBackground(ItemType.ALL)
val starredItems = getMaxItemsForBackground(ItemType.STARRED) val starredItems = getMaxItemsForBackground(ItemType.STARRED)
val fullItemsList = newItems + allItems + starredItems val fullItemsList = newItems + allItems + starredItems
insertDBItems(fullItemsList) insertDBItems(fullItemsList)
return fullItemsList
} catch (e: Throwable) { } catch (e: Throwable) {
// We do nothing // We do nothing
} }
return emptyList()
} }
// TODO: Add tests // TODO: Add tests