Allow to update chapter metadata

pull/966/head
len 7 years ago
parent f3f7aa9e1d
commit c0d7b16ee6

@ -37,8 +37,27 @@ fun syncChaptersWithSource(db: DatabaseHelper,
} }
} }
// Chapters from the source not in db. // Chapters from the db not in the source.
val toAdd = sourceChapters.filterNot { it in dbChapters } 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. // Recognize number for new chapters.
toAdd.forEach { toAdd.forEach {
@ -49,10 +68,14 @@ fun syncChaptersWithSource(db: DatabaseHelper,
} }
// Chapters from the db not in the source. // 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. // Return if there's nothing to add, delete or change, avoiding unnecessary db transactions.
if (toAdd.isEmpty() && toDelete.isEmpty()) { if (toAdd.isEmpty() && toDelete.isEmpty() && toChange.isEmpty()) {
return Pair(emptyList(), emptyList()) return Pair(emptyList(), emptyList())
} }
@ -90,6 +113,10 @@ fun syncChaptersWithSource(db: DatabaseHelper,
db.insertChapters(toAdd).executeAsBlocking() db.insertChapters(toAdd).executeAsBlocking()
} }
if (!toChange.isEmpty()) {
db.insertChapters(toChange).executeAsBlocking()
}
// Fix order in source. // Fix order in source.
db.fixChaptersSourceOrder(sourceChapters).executeAsBlocking() db.fixChaptersSourceOrder(sourceChapters).executeAsBlocking()
} }

Loading…
Cancel
Save