mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-07 11:17:25 +01:00
Allow to update chapter metadata
This commit is contained in:
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…
Reference in New Issue
Block a user