|
@@ -96,6 +96,24 @@ fun syncChaptersWithSource(
|
|
|
|
|
|
// Return if there's nothing to add, delete or change, avoiding unnecessary db transactions.
|
|
|
if (toAdd.isEmpty() && toDelete.isEmpty() && toChange.isEmpty()) {
|
|
|
+ val topChapters = dbChapters.sortedByDescending { it.date_upload }.take(4)
|
|
|
+ val newestDate = topChapters.getOrNull(0)?.date_upload ?: 0L
|
|
|
+
|
|
|
+ // Recalculate update rate if unset and enough chapters are present
|
|
|
+ if (manga.next_update == 0L && topChapters.size > 1) {
|
|
|
+ var delta = 0L
|
|
|
+ for (i in 0 until topChapters.size - 1) {
|
|
|
+ delta += (topChapters[i].date_upload - topChapters[i + 1].date_upload)
|
|
|
+ }
|
|
|
+ delta /= topChapters.size - 1
|
|
|
+ manga.next_update = newestDate + delta
|
|
|
+ db.updateNextUpdated(manga).executeAsBlocking()
|
|
|
+ }
|
|
|
+
|
|
|
+ if (newestDate != 0L && newestDate != manga.last_update) {
|
|
|
+ manga.last_update = newestDate
|
|
|
+ db.updateLastUpdated(manga).executeAsBlocking()
|
|
|
+ }
|
|
|
return Pair(emptyList(), emptyList())
|
|
|
}
|
|
|
|
|
@@ -140,11 +158,29 @@ fun syncChaptersWithSource(
|
|
|
db.insertChapters(toChange).executeAsBlocking()
|
|
|
}
|
|
|
|
|
|
+ val topChapters = db.getChapters(manga).executeAsBlocking().sortedByDescending { it.date_upload }.take(4)
|
|
|
+ // Recalculate next update since chapters were changed
|
|
|
+ if (topChapters.size > 1) {
|
|
|
+ var delta = 0L
|
|
|
+ for (i in 0 until topChapters.size - 1) {
|
|
|
+ delta += (topChapters[i].date_upload - topChapters[i + 1].date_upload)
|
|
|
+ }
|
|
|
+ delta /= topChapters.size - 1
|
|
|
+ manga.next_update = topChapters[0].date_upload + delta
|
|
|
+ db.updateNextUpdated(manga).executeAsBlocking()
|
|
|
+ }
|
|
|
+
|
|
|
// Fix order in source.
|
|
|
db.fixChaptersSourceOrder(sourceChapters).executeAsBlocking()
|
|
|
|
|
|
// Set this manga as updated since chapters were changed
|
|
|
- manga.last_update = Date().time
|
|
|
+ val newestChapter = topChapters.getOrNull(0)
|
|
|
+ val dateFetch = newestChapter?.date_upload ?: manga.last_update
|
|
|
+ if (dateFetch == 0L) {
|
|
|
+ if (toAdd.isNotEmpty()) {
|
|
|
+ manga.last_update = Date().time
|
|
|
+ }
|
|
|
+ } else manga.last_update = dateFetch
|
|
|
db.updateLastUpdated(manga).executeAsBlocking()
|
|
|
}
|
|
|
|