Send read/star state update with new tables

This commit is contained in:
Shinokuni 2021-04-22 22:30:24 +02:00
parent 3be0447584
commit 90468faefc
2 changed files with 27 additions and 51 deletions

View File

@ -18,8 +18,8 @@ import com.readrops.db.entities.Feed;
import com.readrops.db.entities.Folder;
import com.readrops.db.entities.Item;
import com.readrops.db.entities.ItemStateId;
import com.readrops.db.entities.StarredItem;
import com.readrops.db.entities.account.Account;
import com.readrops.db.pojo.ItemReadStarState;
import org.joda.time.DateTime;
@ -93,11 +93,27 @@ public class FreshRSSRepository extends ARepository {
TimingLogger logger = new TimingLogger(TAG, "FreshRSS sync timer");
return Single.<FreshRSSSyncData>create(emitter -> {
syncData.setReadItemsIds(/*database.itemDao().getReadChanges(account.getId()*/Collections.emptyList());
syncData.setUnreadItemsIds(/*database.itemDao().getUnreadChanges(account.getId())*/Collections.emptyList());
List<ItemReadStarState> itemStateChanges = database.itemsIdsDao().getItemStateChanges(account.getId());
syncData.setStarredItemsIds(/*database.itemDao().getFreshRSSStarChanges(account.getId())*/Collections.emptyList());
syncData.setUnstarredItemsIds(/*database.itemDao().getFreshRSSUnstarChanges(account.getId())*/Collections.emptyList());
syncData.setReadItemsIds(itemStateChanges.stream()
.filter(it -> it.getReadChange() && it.getRead())
.map(ItemReadStarState::getRemoteId)
.collect(Collectors.toList()));
syncData.setUnreadItemsIds(itemStateChanges.stream()
.filter(it -> it.getReadChange() && !it.getRead())
.map(ItemReadStarState::getRemoteId)
.collect(Collectors.toList()));
syncData.setStarredItemsIds(itemStateChanges.stream()
.filter(it -> it.getStarChange() && it.getStarred())
.map(ItemReadStarState::getRemoteId)
.collect(Collectors.toList()));
syncData.setUnstarredItemsIds(itemStateChanges.stream()
.filter(it -> it.getStarChange() && !it.getStarred())
.map(ItemReadStarState::getRemoteId)
.collect(Collectors.toList()));
emitter.onSuccess(syncData);
}).flatMap(syncData1 -> dataSource.sync(syncType, syncData1, account.getWriteToken()))
@ -112,15 +128,14 @@ public class FreshRSSRepository extends ARepository {
insertItems(syncResult.getItems());
logger.addSplit("items insertion");
insertStarredItems(syncResult.getStarredItems());
logger.addSplit("starred items insertion");
insertItemsIds(syncResult.getUnreadIds(), syncResult.getStarredIds());
logger.addSplit("insert and update items ids");
account.setLastModified(newLastModified);
database.accountDao().updateLastModified(account.getId(), newLastModified);
database.itemsIdsDao().resetStateChanges(account.getId());
logger.dumpToLog();
this.syncResult = syncResult;
@ -237,43 +252,7 @@ public class FreshRSSRepository extends ARepository {
}
}
private void insertStarredItems(List<Item> items) {
List<StarredItem> starredItems = items.stream().map(StarredItem::new).collect(Collectors.toList());
List<StarredItem> itemsToInsert = new ArrayList<>();
Map<String, Integer> itemsFeedsIds = new HashMap<>();
for (StarredItem item : starredItems) {
int feedId;
if (itemsFeedsIds.containsKey(item.getFeedRemoteId())) {
feedId = itemsFeedsIds.get(item.getFeedRemoteId());
} else {
feedId = database.feedDao().getFeedIdByRemoteId(item.getFeedRemoteId(), account.getId());
itemsFeedsIds.put(item.getFeedRemoteId(), feedId);
}
item.setFeedId(feedId);
item.setReadTime(Utils.readTimeFromString(item.getContent()));
itemsToInsert.add(item);
}
if (!itemsToInsert.isEmpty()) {
Collections.sort(itemsToInsert, Item::compareTo);
database.starredItemDao().deleteStarredItems(account.getId());
database.starredItemDao().insert(itemsToInsert);
}
}
private void insertItemsIds(List<String> unreadIds, List<String> starredIds) {
/*database.itemsIdsDao().deleteUnreadItemsIds(account.getId());
database.itemsIdsDao().insertUnreadItemsIds(unreadIds.stream().map(id ->
new UnreadItemsIds(0, id, account.getId())).collect(Collectors.toList()));
database.itemDao().updateUnreadState(account.getId());
database.itemDao().updateReadState(account.getId());*/
database.itemsIdsDao().deleteItemsIds(account.getId());
database.itemsIdsDao().insertItemStateId(unreadIds.stream().map(id ->
new ItemStateId(0, false, starredIds.stream()

View File

@ -6,7 +6,6 @@ import androidx.room.Insert
import androidx.room.Query
import com.readrops.db.entities.ItemStateId
import com.readrops.db.entities.ReadStarStateChange
import com.readrops.db.entities.UnreadItemsIds
import com.readrops.db.pojo.ItemReadStarState
import io.reactivex.Completable
@ -23,17 +22,15 @@ interface ItemsIdsDao {
fun deleteReadStarStateChanges(accountId: Int)
@Query("Delete From ReadStarStateChange Where account_id = :accountId")
fun deleteStateChanges(accountId: Int)
fun resetStateChanges(accountId: Int)
@Query("Select case When ItemStateId.remote_id is NULL Or ItemStateId.read = 1 Then 1 else 0 End read, Item.remoteId, ReadStarStateChange.read_change, Item.starred, ReadStarStateChange.star_change " +
@Query("Select case When ItemStateId.remote_id is NULL Or ItemStateId.read = 1 Then 1 else 0 End read, " +
"case When ItemStateId.remote_id is NULL Or ItemStateId.starred = 1 Then 1 else 0 End starred," +
"ReadStarStateChange.read_change, ReadStarStateChange.star_change, Item.remoteId " +
"From ReadStarStateChange Inner Join Item On ReadStarStateChange.id = Item.id " +
"Left Join ItemStateId On ItemStateId.remote_id = Item.remoteId Where ReadStarStateChange.account_id = :accountId")
fun getItemStateChanges(accountId: Int): List<ItemReadStarState>
@Query("Select StarredItem.remoteId, Case When StarredItem.read = 1 then 0 else 1 end read, StarredItem.starred, ReadStarStateChange.read_change, " +
"ReadStarStateChange.star_change From StarredItem Inner Join ReadStarStateChange On StarredItem.id = ReadStarStateChange.id Where account_id = :accountId")
fun getStarredItemStateChanges(accountId: Int): List<ItemReadStarState>
fun upsertReadStarStateChange(readStarStateChange: ReadStarStateChange) = Completable.create {
if (readStarStateChange.readChange && readStateChangeExists(readStarStateChange.id) ||
readStarStateChange.starChange && starStateChangeExists(readStarStateChange.id)) {