2016-05-20 08:22:10 -04:00
|
|
|
package eu.kanade.tachiyomi.util
|
|
|
|
|
|
|
|
import eu.kanade.tachiyomi.data.database.DatabaseHelper
|
|
|
|
import eu.kanade.tachiyomi.data.database.models.Chapter
|
|
|
|
import eu.kanade.tachiyomi.data.database.models.Manga
|
2016-05-27 07:21:21 -04:00
|
|
|
import eu.kanade.tachiyomi.data.source.Source
|
|
|
|
import eu.kanade.tachiyomi.data.source.online.OnlineSource
|
2016-05-20 08:22:10 -04:00
|
|
|
import java.util.*
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method for syncing the list of chapters from the source with the ones from the database.
|
|
|
|
*
|
|
|
|
* @param db the database.
|
|
|
|
* @param sourceChapters a list of chapters from the source.
|
|
|
|
* @param manga the manga of the chapters.
|
|
|
|
* @param source the source of the chapters.
|
|
|
|
* @return a pair of new insertions and deletions.
|
|
|
|
*/
|
|
|
|
fun syncChaptersWithSource(db: DatabaseHelper,
|
|
|
|
sourceChapters: List<Chapter>,
|
|
|
|
manga: Manga,
|
2016-12-06 11:22:03 -05:00
|
|
|
source: Source) : Pair<List<Chapter>, List<Chapter>> {
|
2016-05-20 08:22:10 -04:00
|
|
|
|
|
|
|
// Chapters from db.
|
|
|
|
val dbChapters = db.getChapters(manga).executeAsBlocking()
|
|
|
|
|
|
|
|
// Fix manga id and order in source.
|
|
|
|
sourceChapters.forEachIndexed { i, chapter ->
|
|
|
|
chapter.manga_id = manga.id
|
|
|
|
chapter.source_order = i
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chapters from the source not in db.
|
|
|
|
val toAdd = sourceChapters.filterNot { it in dbChapters }
|
|
|
|
|
|
|
|
// Recognize number for new chapters.
|
|
|
|
toAdd.forEach {
|
2016-03-22 14:00:05 -04:00
|
|
|
if (source is OnlineSource) {
|
2016-11-12 08:04:25 -05:00
|
|
|
source.prepareNewChapter(it, manga)
|
2016-03-22 14:00:05 -04:00
|
|
|
}
|
2016-05-20 08:22:10 -04:00
|
|
|
ChapterRecognition.parseChapterNumber(it, manga)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chapters from the db not in the source.
|
|
|
|
val toDelete = dbChapters.filterNot { it in sourceChapters }
|
|
|
|
|
2017-01-01 14:54:41 -05:00
|
|
|
// Return if there's nothing to add or delete, avoiding unnecessary db transactions.
|
|
|
|
if (toAdd.isEmpty() && toDelete.isEmpty()) {
|
|
|
|
return Pair(emptyList(), emptyList())
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:22:03 -05:00
|
|
|
val readded = mutableListOf<Chapter>()
|
2016-05-20 08:22:10 -04:00
|
|
|
|
|
|
|
db.inTransaction {
|
2016-12-06 11:22:03 -05:00
|
|
|
val deletedChapterNumbers = TreeSet<Float>()
|
2016-05-20 08:22:10 -04:00
|
|
|
val deletedReadChapterNumbers = TreeSet<Float>()
|
|
|
|
if (!toDelete.isEmpty()) {
|
|
|
|
for (c in toDelete) {
|
|
|
|
if (c.read) {
|
|
|
|
deletedReadChapterNumbers.add(c.chapter_number)
|
|
|
|
}
|
2016-12-06 11:22:03 -05:00
|
|
|
deletedChapterNumbers.add(c.chapter_number)
|
2016-05-20 08:22:10 -04:00
|
|
|
}
|
2016-12-06 11:22:03 -05:00
|
|
|
db.deleteChapters(toDelete).executeAsBlocking()
|
2016-05-20 08:22:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
var now = Date().time
|
|
|
|
|
|
|
|
for (i in toAdd.indices.reversed()) {
|
|
|
|
val c = toAdd[i]
|
|
|
|
c.date_fetch = now++
|
|
|
|
// Try to mark already read chapters as read when the source deletes them
|
2016-05-26 10:03:55 -04:00
|
|
|
if (c.isRecognizedNumber && c.chapter_number in deletedReadChapterNumbers) {
|
2016-05-20 08:22:10 -04:00
|
|
|
c.read = true
|
2016-12-06 11:22:03 -05:00
|
|
|
}
|
|
|
|
if (c.isRecognizedNumber && c.chapter_number in deletedChapterNumbers) {
|
|
|
|
readded.add(c)
|
2016-05-20 08:22:10 -04:00
|
|
|
}
|
|
|
|
}
|
2016-12-06 11:22:03 -05:00
|
|
|
db.insertChapters(toAdd).executeAsBlocking()
|
2016-05-20 08:22:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fix order in source.
|
|
|
|
db.fixChaptersSourceOrder(sourceChapters).executeAsBlocking()
|
|
|
|
}
|
2016-12-06 11:22:03 -05:00
|
|
|
return Pair(toAdd.subtract(readded).toList(), toDelete.subtract(readded).toList())
|
2016-05-20 08:22:10 -04:00
|
|
|
}
|