Adapt new state changes management for Nextcloud News

This commit is contained in:
Shinokuni 2021-07-06 22:04:53 +02:00
parent 8c9b2b2ab1
commit 3d0ff2619f
5 changed files with 34 additions and 17 deletions

View File

@ -45,8 +45,9 @@ public class ItemViewModel extends ViewModel {
}
public Completable setStarState(Item item) {
return KoinJavaComponent.get(ARepository.class, null, () -> DefinitionParametersKt.parametersOf(account))
.setItemStarState(item);
ARepository repository = KoinJavaComponent.get(ARepository.class, null, () -> DefinitionParametersKt.parametersOf(account));
return repository.setItemStarState(item);
}
public Uri saveImageInCache(Bitmap bitmap, Context context) throws IOException {

View File

@ -116,13 +116,13 @@ public abstract class ARepository {
public Completable setItemReadState(Item item) {
if (account.getConfig().useSeparateState()) {
return database.itemStateChangesDao().upsertItemReadStateChange(item, account.getId())
return database.itemStateChangesDao().upsertItemReadStateChange(item, account.getId(), true)
.andThen(database.itemStateDao().upsertItemReadState(new ItemState(0, item.isRead(),
item.isStarred(), item.getRemoteId(), account.getId())));
} else if (account.isLocal()) {
return database.itemDao().setReadState(item.getId(), item.isRead());
} else { // nextcloud case
return database.itemStateChangesDao().upsertItemReadStateChange(item, account.getId())
return database.itemStateChangesDao().upsertItemReadStateChange(item, account.getId(), false)
.andThen(database.itemDao().setReadState(item.getId(), item.isRead()));
}
@ -138,14 +138,14 @@ public abstract class ARepository {
public Completable setItemStarState(Item item) {
if (account.getConfig().useSeparateState()) {
return database.itemStateChangesDao().upsertItemStarStateChange(item, account.getId())
return database.itemStateChangesDao().upsertItemStarStateChange(item, account.getId(), true)
.andThen(database.itemStateDao().upsertItemStarState(new ItemState(0, item.isRead(),
item.isStarred(), item.getRemoteId(), account.getId())));
} else if (account.isLocal()) {
return database.itemDao().setStarState(item.getId(), item.isRead());
} else { // nextcloud case
return database.itemStateChangesDao().upsertItemReadStateChange(item, account.getId())
.andThen(database.itemDao().setStarState(item.getId(), item.isRead()));
return database.itemStateChangesDao().upsertItemStarStateChange(item, account.getId(), false)
.andThen(database.itemDao().setStarState(item.getId(), item.isStarred()));
}
}

View File

@ -102,7 +102,7 @@ public class NextNewsRepository extends ARepository {
List<ItemReadStarState> itemStateChanges = database
.itemStateChangesDao()
.getItemStateChanges(account.getId());
.getNextcloudNewsStateChanges(account.getId());
syncData.setReadItems(itemStateChanges.stream()
.filter(it -> it.getReadChange() && it.getRead())
@ -129,7 +129,7 @@ public class NextNewsRepository extends ARepository {
.collect(Collectors.toList());
if (!unstarredItemsIds.isEmpty()) {
syncData.setUnstarredItems(database.itemDao().getUnstarChanges(unstarredItemsIds, account.getId()));
syncData.setUnstarredItems(database.itemDao().getStarChanges(unstarredItemsIds, account.getId()));
}
}

View File

@ -61,9 +61,6 @@ public interface ItemDao extends BaseDao<Item> {
@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.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);
}

View File

@ -28,15 +28,25 @@ interface ItemStateChangeDao : BaseDao<ItemStateChange> {
"Left Join ItemState On ItemState.remote_id = Item.remoteId Where ItemStateChange.account_id = :accountId")
fun getItemStateChanges(accountId: Int): List<ItemReadStarState>
@Query("Select Item.read, Item.starred," +
"ItemStateChange.read_change, ItemStateChange.star_change, Item.remoteId " +
"From ItemStateChange Inner Join Item On ItemStateChange.id = Item.id " +
"Where ItemStateChange.account_id = :accountId")
fun getNextcloudNewsStateChanges(accountId: Int): List<ItemReadStarState>
@Query("Select Case When :itemId In (Select id From ItemStateChange Where read_change = 1) Then 1 Else 0 End")
fun readStateChangeExists(itemId: Int): Boolean
@Query("Select Case When :itemId In (Select id From ItemStateChange Where star_change = 1) Then 1 Else 0 End")
fun starStateChangeExists(itemId: Int): Boolean
fun upsertItemReadStateChange(item: Item, accountId: Int) = Completable.create {
fun upsertItemReadStateChange(item: Item, accountId: Int, useSeparateState: Boolean) = Completable.create {
if (itemStateChangeExists(item.id, accountId)) {
val oldItemReadState = getItemReadState(item.remoteId, accountId)
val oldItemReadState = if (useSeparateState)
getItemReadState(item.remoteId, accountId)
else
getStandardItemReadState(item.remoteId, accountId)
val readChange = item.isRead != oldItemReadState
if (readChange) {
@ -56,9 +66,13 @@ interface ItemStateChangeDao : BaseDao<ItemStateChange> {
it.onComplete()
}
fun upsertItemStarStateChange(item: Item, accountId: Int) = Completable.create {
fun upsertItemStarStateChange(item: Item, accountId: Int, useSeparateState: Boolean) = Completable.create {
if (itemStateChangeExists(item.id, accountId)) {
val oldItemStarState = getItemStarState(item.remoteId, accountId)
val oldItemStarState = if (useSeparateState)
getItemStarState(item.remoteId, accountId)
else
getStandardItemStarState(item.remoteId, accountId)
val starChange = item.isStarred != oldItemStarState
if (starChange) {
@ -75,7 +89,6 @@ interface ItemStateChangeDao : BaseDao<ItemStateChange> {
insertItemStateChange(ItemStateChange(id = item.id, starChange = true, accountId = accountId))
}
it.onComplete()
}
@ -88,9 +101,15 @@ interface ItemStateChangeDao : BaseDao<ItemStateChange> {
@Query("Select read From ItemState Where remote_id = :remoteId And account_id = :accountId")
fun getItemReadState(remoteId: String, accountId: Int): Boolean
@Query("Select read From Item Inner Join Feed On Item.feed_id = Feed.id Where Item.remoteId = :remoteId And account_id = :accountId")
fun getStandardItemReadState(remoteId: String, accountId: Int): Boolean
@Query("Select starred From ItemState Where remote_id = :remoteId And account_id = :accountId")
fun getItemStarState(remoteId: String, accountId: Int): Boolean
@Query("Select starred From Item Inner Join Feed On Item.feed_id = Feed.id Where Item.remoteId = :remoteId And account_id = :accountId")
fun getStandardItemStarState(remoteId: String, accountId: Int): Boolean
@Query("Update ItemStateChange set read_change = :readChange Where id = :id")
fun updateItemReadStateChange(readChange: Boolean, id: Int)