Fetch feed colors on synchronization

This commit is contained in:
Shinokuni 2024-05-23 21:45:46 +02:00
parent 133d8e6992
commit bc3015ab95
5 changed files with 39 additions and 15 deletions

View File

@ -8,6 +8,7 @@ data class SyncResult(
var items: List<Item> = mutableListOf(),
var starredItems: List<Item> = mutableListOf(),
var feeds: List<Feed> = listOf(),
var newFeedIds: List<Long> = listOf(),
var folders: List<Folder> = listOf(),
var unreadIds: List<String> = listOf(),
var readIds: List<String> = listOf(),

View File

@ -11,6 +11,7 @@ import com.readrops.db.entities.Folder;
import com.readrops.db.entities.Item;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -100,7 +101,7 @@ public class FreshRSSDataSource {
getItemsIds(null, GOOGLE_STARRED, MAX_STARRED_ITEMS), // starred items ids
getStarredItems(MAX_STARRED_ITEMS),
(readState, starState, folders, feeds, items, unreadItemsIds, starredItemsIds, starredItems) ->
new SyncResult(items, starredItems, feeds, folders, unreadItemsIds, Collections.emptyList(), starredItemsIds, false)
new SyncResult(items, starredItems, feeds, new ArrayList<>(), folders, unreadItemsIds, Collections.emptyList(), starredItemsIds, false)
);
} else {
return Single.zip(setItemsReadState(syncData, writeToken).toSingleDefault(""),
@ -112,7 +113,7 @@ public class FreshRSSDataSource {
getItemsIds(GOOGLE_UNREAD, GOOGLE_READING_LIST, MAX_ITEMS), // read items ids
getItemsIds(null, GOOGLE_STARRED, MAX_STARRED_ITEMS), // starred items ids
(readState, starState, folders, feeds, items, unreadItemsIds, readItemsIds, starredItemsIds) ->
new SyncResult(items, Collections.emptyList(), feeds, folders, unreadItemsIds, readItemsIds, starredItemsIds, false)
new SyncResult(items, Collections.emptyList(), feeds, new ArrayList<>(), folders, unreadItemsIds, readItemsIds, starredItemsIds, false)
);
}

View File

@ -68,7 +68,7 @@ class FreshRSSRepository(
return dataSource.synchronize(syncType, syncData, account.writeToken!!).apply {
insertFolders(folders)
insertFeeds(feeds)
newFeedIds = insertFeeds(feeds)
insertItems(items, false)
insertItems(starredItems, true)
@ -123,9 +123,9 @@ class FreshRSSRepository(
super.deleteFolder(folder)
}
private suspend fun insertFeeds(feeds: List<Feed>) {
private suspend fun insertFeeds(feeds: List<Feed>): List<Long> {
feeds.forEach { it.accountId = account.id }
database.newFeedDao().upsertFeeds(feeds, account)
return database.newFeedDao().upsertFeeds(feeds, account)
}
private suspend fun insertFolders(folders: List<Folder>) {

View File

@ -2,6 +2,7 @@ package com.readrops.app.compose.timelime
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.compose.runtime.Stable
import androidx.paging.Pager
import androidx.paging.PagingConfig
@ -11,6 +12,7 @@ import cafe.adriel.voyager.core.model.screenModelScope
import com.readrops.app.compose.base.TabScreenModel
import com.readrops.app.compose.repositories.ErrorResult
import com.readrops.app.compose.repositories.GetFoldersWithFeeds
import com.readrops.app.compose.util.FeedColors
import com.readrops.db.Database
import com.readrops.db.entities.Feed
import com.readrops.db.entities.Folder
@ -108,8 +110,21 @@ class TimelineScreenModel(
_timelineState.update { it.copy(isRefreshing = true) }
try {
repository?.synchronize()
val result = repository!!.synchronize()
// TODO put this in a foreground worker
for (feedId in result.newFeedIds) {
val color = try {
FeedColors.getFeedColor(result.feeds.first { it.id == feedId.toInt() }.iconUrl!!)
} catch (e: Exception) {
0
}
database.newFeedDao().updateFeedColor(feedId.toInt(), color)
}
} catch (e: Exception) {
Log.e("TimelineScreenModel", "${e.message}")
_timelineState.update { it.copy(syncError = e, isRefreshing = false) }
return@launch
}

View File

@ -53,6 +53,9 @@ abstract class NewFeedDao : NewBaseDao<Feed> {
@Query("Delete from Feed Where remoteId in (:ids) And account_id = :accountId")
abstract fun deleteByIds(ids: List<String>, accountId: Int)
@Query("Update Feed set background_color = :color Where id = :feedId")
abstract fun updateFeedColor(feedId: Int, color: Int)
/**
* Insert, update and delete feeds by account
*
@ -68,20 +71,24 @@ abstract class NewFeedDao : NewBaseDao<Feed> {
val feedsToDelete = localFeedIds.filter { localFeedId -> feeds.none { feed -> localFeedId == feed.remoteId } }
feeds.forEach { feed ->
feed.folderId = if (feed.remoteFolderId == null) {
null
} else {
selectRemoteFolderLocalId(feed.remoteFolderId!!, account.id)
}
// works only for already existing feeds
updateFeedNameAndFolder(feed.remoteId!!, account.id, feed.name!!, feed.folderId)
feed.folderId = if (feed.remoteFolderId == null) {
null
} else {
selectRemoteFolderLocalId(feed.remoteFolderId!!, account.id)
}
// works only for already existing feeds
updateFeedNameAndFolder(feed.remoteId!!, account.id, feed.name!!, feed.folderId)
}
if (feedsToDelete.isNotEmpty()) {
deleteByIds(feedsToDelete, account.id)
}
return insert(feedsToInsert)
return if (feedsToInsert.isNotEmpty()) {
insert(feedsToInsert).apply {
feedsToInsert.zip(this).forEach { (feed, id) -> feed.id = id.toInt() }
}
} else listOf()
}
}