Integrate ItemStateChange table in Nextcloud News synchronisation

This commit is contained in:
Shinokuni 2021-07-04 19:12:29 +02:00
parent dfe225ed8d
commit c9caa0be89
3 changed files with 49 additions and 27 deletions

View File

@ -121,8 +121,9 @@ public abstract class ARepository {
item.isStarred(), item.getRemoteId(), account.getId())));
} else if (account.isLocal()) {
return database.itemDao().setReadState(item.getId(), item.isRead());
} else { // TODO nextcloud case, use only ItemStateChange table
return Completable.complete();
} else { // nextcloud case
return database.itemStateChangesDao().upsertItemReadStateChange(item, account.getId())
.andThen(database.itemDao().setReadState(item.getId(), item.isRead()));
}
}
@ -142,8 +143,9 @@ public abstract class ARepository {
item.isStarred(), item.getRemoteId(), account.getId())));
} else if (account.isLocal()) {
return database.itemDao().setStarState(item.getId(), item.isRead());
} else { // TODO nextcloud case, use only ItemStateChange table
return Completable.complete();
} else { // nextcloud case
return database.itemStateChangesDao().upsertItemReadStateChange(item, account.getId())
.andThen(database.itemDao().setStarState(item.getId(), item.isRead()));
}
}

View File

@ -23,6 +23,7 @@ import com.readrops.db.entities.Feed;
import com.readrops.db.entities.Folder;
import com.readrops.db.entities.Item;
import com.readrops.db.entities.account.Account;
import com.readrops.db.pojo.ItemReadStarState;
import org.joda.time.LocalDateTime;
import org.koin.java.KoinJavaComponent;
@ -31,6 +32,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import io.reactivex.Completable;
import io.reactivex.Observable;
@ -81,26 +83,55 @@ public class NextNewsRepository extends ARepository {
@Override
public Observable<Feed> sync(List<Feed> feeds) {
setCredentials(account);
return Observable.create(emitter -> {
try {
long lastModified = LocalDateTime.now().toDateTime().getMillis();
SyncType syncType;
if (account.getLastModified() != 0)
if (account.getLastModified() != 0) {
syncType = SyncType.CLASSIC_SYNC;
else
} else {
syncType = SyncType.INITIAL_SYNC;
}
NextNewsSyncData syncData = new NextNewsSyncData();
if (syncType == SyncType.CLASSIC_SYNC) {
syncData.setLastModified(account.getLastModified() / 1000L);
syncData.setReadItems(database.itemDao().getReadChanges(account.getId()));
syncData.setUnreadItems(database.itemDao().getUnreadChanges(account.getId()));
List<ItemReadStarState> itemStateChanges = database
.itemStateChangesDao()
.getItemStateChanges(account.getId());
syncData.setReadItems(itemStateChanges.stream()
.filter(it -> it.getReadChange() && it.getRead())
.map(ItemReadStarState::getRemoteId)
.collect(Collectors.toList()));
syncData.setUnreadItems(itemStateChanges.stream()
.filter(it -> it.getReadChange() && !it.getRead())
.map(ItemReadStarState::getRemoteId)
.collect(Collectors.toList()));
List<String> starredItemsIds = itemStateChanges.stream()
.filter(it -> it.getStarChange() && it.getStarred())
.map(ItemReadStarState::getRemoteId)
.collect(Collectors.toList());
if (!starredItemsIds.isEmpty()) {
syncData.setStarredItems(database.itemDao().getStarChanges(starredItemsIds, account.getId()));
}
List<String> unstarredItemsIds = itemStateChanges.stream()
.filter(it -> it.getStarChange() && !it.getStarred())
.map(ItemReadStarState::getRemoteId)
.collect(Collectors.toList());
if (!unstarredItemsIds.isEmpty()) {
syncData.setUnstarredItems(database.itemDao().getUnstarChanges(unstarredItemsIds, account.getId()));
}
syncData.setStarredItems(database.itemDao().getStarChanges(account.getId()));
syncData.setUnstarredItems(database.itemDao().getUnstarChanges(account.getId()));
}
TimingLogger timings = new TimingLogger(TAG, "nextcloud news " + syncType.name().toLowerCase());
@ -123,13 +154,7 @@ public class NextNewsRepository extends ARepository {
account.setLastModified(lastModified);
database.accountDao().updateLastModified(account.getId(), lastModified);
if (!syncData.getReadItems().isEmpty() || !syncData.getUnreadItems().isEmpty()) {
}
if (!syncData.getStarredItems().isEmpty() || !syncData.getUnstarredItems().isEmpty()) {
}
database.itemStateChangesDao().resetStateChanges(account.getId());
emitter.onComplete();
} else {
@ -145,6 +170,7 @@ public class NextNewsRepository extends ARepository {
@Override
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
setCredentials(account);
return Single.create(emitter -> {
List<FeedInsertionResult> feedInsertionResults = new ArrayList<>();

View File

@ -58,17 +58,11 @@ public interface ItemDao extends BaseDao<Item> {
@RawQuery(observedEntities = {Item.class, ItemState.class})
LiveData<ItemWithFeed> getItemById(SupportSQLiteQuery query);
@Query("Select Item.remoteId From Item Inner Join Feed On Item.feed_id = Feed.id Where read = 1 And account_id = :accountId")
List<String> getReadChanges(int accountId);
@Query("Select Item.guid, Feed.remoteId as feedRemoteId From Item Inner Join Feed On Item.feed_id = Feed.id Where Item.remoteId In (:remoteIds) And account_id = :accountId")
List<StarItem> getStarChanges(List<String> remoteIds, int accountId);
@Query("Select Item.remoteId From Item Inner Join Feed On Item.feed_id = Feed.id Where read = 0 And account_id = :accountId")
List<String> getUnreadChanges(int accountId);
@Query("Select Item.guid, Feed.remoteId as feedRemoteId From Item Inner Join Feed On Item.feed_id = Feed.id Where starred = 1 And account_id = :accountId")
List<StarItem> getStarChanges(int accountId);
@Query("Select Item.guid, Feed.remoteId as feedRemoteId From Item Inner Join Feed On Item.feed_id = Feed.id Where starred = 0 And account_id = :accountId")
List<StarItem> getUnstarChanges(int accountId);
@Query("Select Item.guid, Feed.remoteId as feedRemoteId From Item Inner Join Feed On Item.feed_id = Feed.id Where Item.remoteId In (:remoteIds) And account_id = :accountId")
List<StarItem> getUnstarChanges(List<String> remoteIds, int accountId);
@Query("Update Item set read = :read, starred = :starred Where remoteId = :remoteId")
void setReadAndStarState(String remoteId, boolean read, boolean starred);