Update read state in ItemState table

This commit is contained in:
Shinokuni 2021-04-22 18:49:45 +02:00
parent b7b4ae9dc4
commit 3be0447584
5 changed files with 31 additions and 32 deletions

View File

@ -504,7 +504,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
allItemsSelected = false; allItemsSelected = false;
} else { } else {
viewModel.setItemsReadState(adapter.getSelectedItems()) viewModel.setItemsReadState(adapter.getSelectedItems(), read)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.doOnError(throwable -> Utils.showSnackbar(binding.mainRoot, throwable.getMessage())) .doOnError(throwable -> Utils.showSnackbar(binding.mainRoot, throwable.getMessage()))

View File

@ -232,10 +232,11 @@ public class MainViewModel extends ViewModel {
return repository.setItemReadState(item); return repository.setItemReadState(item);
} }
public Completable setItemsReadState(List<ItemWithFeed> items) { public Completable setItemsReadState(List<ItemWithFeed> items, boolean read) {
List<Completable> completableList = new ArrayList<>(); List<Completable> completableList = new ArrayList<>();
for (ItemWithFeed itemWithFeed : items) { for (ItemWithFeed itemWithFeed : items) {
itemWithFeed.getItem().setRead(read);
completableList.add(setItemReadState(itemWithFeed)); completableList.add(setItemReadState(itemWithFeed));
} }

View File

@ -17,6 +17,7 @@ import com.readrops.db.Database;
import com.readrops.db.entities.Feed; import com.readrops.db.entities.Feed;
import com.readrops.db.entities.Folder; import com.readrops.db.entities.Folder;
import com.readrops.db.entities.Item; import com.readrops.db.entities.Item;
import com.readrops.db.entities.ItemStateId;
import com.readrops.db.entities.ReadStarStateChange; import com.readrops.db.entities.ReadStarStateChange;
import com.readrops.db.entities.account.Account; import com.readrops.db.entities.account.Account;
@ -115,7 +116,7 @@ public abstract class ARepository {
} }
public Completable setItemReadState(Item item) { public Completable setItemReadState(Item item) {
return database.itemDao().setReadState(item.getId(), item.isRead()) return database.itemsIdsDao().upsertItemReadState(new ItemStateId(0, item.isRead(), item.isStarred(), item.getRemoteId(), account.getId()))
.andThen(database.itemsIdsDao().upsertReadStarStateChange(new ReadStarStateChange(item.getId(), .andThen(database.itemsIdsDao().upsertReadStarStateChange(new ReadStarStateChange(item.getId(),
true, false, account.getId()))); true, false, account.getId())));
} }

View File

@ -275,7 +275,7 @@ public class FreshRSSRepository extends ARepository {
database.itemDao().updateReadState(account.getId());*/ database.itemDao().updateReadState(account.getId());*/
database.itemsIdsDao().deleteItemsIds(account.getId()); database.itemsIdsDao().deleteItemsIds(account.getId());
database.itemsIdsDao().insertItemsIds(unreadIds.stream().map(id -> database.itemsIdsDao().insertItemStateId(unreadIds.stream().map(id ->
new ItemStateId(0, false, starredIds.stream() new ItemStateId(0, false, starredIds.stream()
.anyMatch(starredId -> starredId.equals(id)), id, account.getId())) .anyMatch(starredId -> starredId.equals(id)), id, account.getId()))
.collect(Collectors.toList())); .collect(Collectors.toList()));

View File

@ -13,46 +13,21 @@ import io.reactivex.Completable
@Dao @Dao
interface ItemsIdsDao { interface ItemsIdsDao {
@Insert
fun insertUnreadItemsIds(unreadItemsIds: List<UnreadItemsIds>)
@Insert
fun insertUnreadItemId(unreadItemId: UnreadItemsIds)
@Query("Delete From UnreadItemsIds Where remote_id = :remoteId And account_id = :accountId")
fun deleteUnreadItemId(remoteId: String, accountId: Int)
fun upsertUnreadItemId(unreadItemId: UnreadItemsIds) = Completable.create {
if (unreadItemIdExists(unreadItemId.remoteId, unreadItemId.accountId)) {
deleteUnreadItemId(unreadItemId.remoteId, unreadItemId.accountId)
} else {
insertUnreadItemId(unreadItemId)
}
it.onComplete()
}
@Query("Select case When Exists (Select remote_id, account_id From UnreadItemsIds Where remote_id = :remoteId And account_id = :accountId) Then 1 else 0 End")
fun unreadItemIdExists(remoteId: String, accountId: Int): Boolean
@Insert @Insert
fun insertReadStarStateChange(readStarStateChange: ReadStarStateChange) fun insertReadStarStateChange(readStarStateChange: ReadStarStateChange)
@Delete @Delete
fun deleteReadStarStateChange(readStarStateChange: ReadStarStateChange) fun deleteReadStarStateChange(readStarStateChange: ReadStarStateChange)
@Query("Delete From UnreadItemsIds Where account_id = :accountId")
fun deleteUnreadItemsIds(accountId: Int)
@Query("Delete From ReadStarStateChange Where account_id = :accountId") @Query("Delete From ReadStarStateChange Where account_id = :accountId")
fun deleteReadStarStateChanges(accountId: Int) fun deleteReadStarStateChanges(accountId: Int)
@Query("Delete From ReadStarStateChange Where account_id = :accountId") @Query("Delete From ReadStarStateChange Where account_id = :accountId")
fun deleteStateChanges(accountId: Int) fun deleteStateChanges(accountId: Int)
@Query("Select case When UnreadItemsIds.remote_id is NULL 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, Item.remoteId, ReadStarStateChange.read_change, Item.starred, ReadStarStateChange.star_change " +
"From ReadStarStateChange Inner Join Item On ReadStarStateChange.id = Item.id " + "From ReadStarStateChange Inner Join Item On ReadStarStateChange.id = Item.id " +
"Left Join UnreadItemsIds On UnreadItemsIds.remote_id = Item.remoteId Where ReadStarStateChange.account_id = :accountId") "Left Join ItemStateId On ItemStateId.remote_id = Item.remoteId Where ReadStarStateChange.account_id = :accountId")
fun getItemStateChanges(accountId: Int): List<ItemReadStarState> 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, " + @Query("Select StarredItem.remoteId, Case When StarredItem.read = 1 then 0 else 1 end read, StarredItem.starred, ReadStarStateChange.read_change, " +
@ -79,6 +54,28 @@ interface ItemsIdsDao {
@Query("Delete From ItemStateId Where account_id = :accountId") @Query("Delete From ItemStateId Where account_id = :accountId")
fun deleteItemsIds(accountId: Int) fun deleteItemsIds(accountId: Int)
@Query("Delete From ItemStateId Where remote_id = :remoteId And account_id = :accountId")
fun deleteItemStateId(remoteId: String, accountId: Int)
@Insert @Insert
fun insertItemsIds(itemsIds: List<ItemStateId>) fun insertItemStateId(itemsIds: List<ItemStateId>)
@Insert
fun insertItemStateId(itemStateId: ItemStateId)
@Query("Update ItemStateId set read = :read Where remote_id = :remoteId And account_id = :accountId")
fun updateItemReadState(read: Boolean, remoteId: String, accountId: Int)
@Query("Select case When Exists (Select remote_id, account_id From ItemStateId Where remote_id = :remoteId And account_id = :accountId) Then 1 else 0 End")
fun itemStateExists(remoteId: String, accountId: Int): Boolean
fun upsertItemReadState(itemStateId: ItemStateId) = Completable.create {
if (itemStateExists(itemStateId.remoteId, itemStateId.accountId)) {
updateItemReadState(itemStateId.read, itemStateId.remoteId, itemStateId.accountId)
} else {
insertItemStateId(itemStateId)
}
it.onComplete()
}
} }