|
@@ -37,8 +37,27 @@ fun syncChaptersWithSource(db: DatabaseHelper,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // Chapters from the source not in db.
|
|
|
- val toAdd = sourceChapters.filterNot { it in dbChapters }
|
|
|
+ // Chapters from the db not in the source.
|
|
|
+ val toAdd = mutableListOf<Chapter>()
|
|
|
+
|
|
|
+ // Chapters whose metadata have changed.
|
|
|
+ val toChange = mutableListOf<Chapter>()
|
|
|
+
|
|
|
+ for (sourceChapter in sourceChapters) {
|
|
|
+ val dbChapter = dbChapters.find { it.url == sourceChapter.url }
|
|
|
+
|
|
|
+ // Add the chapter if not in db already, or update if the metadata changed.
|
|
|
+ if (dbChapter == null) {
|
|
|
+ toAdd.add(sourceChapter)
|
|
|
+ } else if (dbChapter.scanlator != sourceChapter.scanlator ||
|
|
|
+ dbChapter.name != sourceChapter.name) {
|
|
|
+
|
|
|
+ dbChapter.scanlator = sourceChapter.scanlator
|
|
|
+ dbChapter.name = sourceChapter.name
|
|
|
+
|
|
|
+ toChange.add(dbChapter)
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
// Recognize number for new chapters.
|
|
|
toAdd.forEach {
|
|
@@ -49,10 +68,14 @@ fun syncChaptersWithSource(db: DatabaseHelper,
|
|
|
}
|
|
|
|
|
|
// Chapters from the db not in the source.
|
|
|
- val toDelete = dbChapters.filterNot { it in sourceChapters }
|
|
|
+ val toDelete = dbChapters.filterNot { dbChapter ->
|
|
|
+ sourceChapters.any { sourceChapter ->
|
|
|
+ dbChapter.url == sourceChapter.url
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // Return if there's nothing to add or delete, avoiding unnecessary db transactions.
|
|
|
- if (toAdd.isEmpty() && toDelete.isEmpty()) {
|
|
|
+ // Return if there's nothing to add, delete or change, avoiding unnecessary db transactions.
|
|
|
+ if (toAdd.isEmpty() && toDelete.isEmpty() && toChange.isEmpty()) {
|
|
|
return Pair(emptyList(), emptyList())
|
|
|
}
|
|
|
|
|
@@ -90,6 +113,10 @@ fun syncChaptersWithSource(db: DatabaseHelper,
|
|
|
db.insertChapters(toAdd).executeAsBlocking()
|
|
|
}
|
|
|
|
|
|
+ if (!toChange.isEmpty()) {
|
|
|
+ db.insertChapters(toChange).executeAsBlocking()
|
|
|
+ }
|
|
|
+
|
|
|
// Fix order in source.
|
|
|
db.fixChaptersSourceOrder(sourceChapters).executeAsBlocking()
|
|
|
}
|