From 34c5f0b7ba31cc685589403a150842a99229fa78 Mon Sep 17 00:00:00 2001 From: inorichi Date: Fri, 5 Feb 2016 22:08:54 +0100 Subject: [PATCH] Try to mark readded chapters as read. #119 --- .../data/database/DatabaseHelper.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java b/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java index 2ae8791a97..5c2c0c1ed7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java @@ -19,6 +19,7 @@ import com.pushtorefresh.storio.sqlite.queries.RawQuery; import java.util.Date; import java.util.List; +import java.util.TreeSet; import eu.kanade.tachiyomi.data.database.models.Category; import eu.kanade.tachiyomi.data.database.models.CategorySQLiteTypeMapping; @@ -259,21 +260,32 @@ public class DatabaseHelper { int deleted = 0; db.internal().beginTransaction(); try { + TreeSet deletedReadChapterNumbers = new TreeSet<>(); + if (!toDelete.isEmpty()) { + for (Chapter c : toDelete) { + if (c.read) { + deletedReadChapterNumbers.add(c.chapter_number); + } + } + deleted = deleteChapters(toDelete).executeAsBlocking().results().size(); + } + if (!toAdd.isEmpty()) { // Set the date fetch for new items in reverse order to allow another sorting method. // Sources MUST return the chapters from most to less recent, which is common. long now = new Date().getTime(); for (int i = toAdd.size() - 1; i >= 0; i--) { - toAdd.get(i).date_fetch = now++; + Chapter c = toAdd.get(i); + c.date_fetch = now++; + // Try to mark already read chapters as read when the source deletes them + if (c.chapter_number != -1 && deletedReadChapterNumbers.contains(c.chapter_number)) { + c.read = true; + } } added = insertChapters(toAdd).executeAsBlocking().numberOfInserts(); } - if (!toDelete.isEmpty()) { - deleted = deleteChapters(toDelete).executeAsBlocking().results().size(); - } - db.internal().setTransactionSuccessful(); } finally { db.internal().endTransaction();