From a09d6c0470311762bb790233463491e6f51412ae Mon Sep 17 00:00:00 2001 From: FourTOne5 <59261191+FourTOne5@users.noreply.github.com> Date: Sun, 19 Dec 2021 01:47:07 +0600 Subject: [PATCH] Better Upload Date to not have a single blank upload date. (#6358) --- .../tachiyomi/data/database/DbOpenCallback.kt | 5 +- .../data/database/tables/ChapterTable.kt | 3 + .../util/chapter/ChapterSourceSync.kt | 64 +++++++++---------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenCallback.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenCallback.kt index 9a7d7dd4f4..95a20e5571 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenCallback.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/DbOpenCallback.kt @@ -20,7 +20,7 @@ class DbOpenCallback : SupportSQLiteOpenHelper.Callback(DATABASE_VERSION) { /** * Version of the database. */ - const val DATABASE_VERSION = 13 + const val DATABASE_VERSION = 14 } override fun onCreate(db: SupportSQLiteDatabase) = with(db) { @@ -91,6 +91,9 @@ class DbOpenCallback : SupportSQLiteOpenHelper.Callback(DATABASE_VERSION) { db.execSQL(TrackTable.insertFromTempTable) db.execSQL(TrackTable.dropTempTable) } + if (oldVersion < 14) { + db.execSQL(ChapterTable.fixDateUploadIfNeeded) + } } override fun onConfigure(db: SupportSQLiteDatabase) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/tables/ChapterTable.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/tables/ChapterTable.kt index 67047cc004..793349119a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/tables/ChapterTable.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/tables/ChapterTable.kt @@ -62,4 +62,7 @@ object ChapterTable { val addScanlator: String get() = "ALTER TABLE $TABLE ADD COLUMN $COL_SCANLATOR TEXT DEFAULT NULL" + + val fixDateUploadIfNeeded: String + get() = "UPDATE $TABLE SET $COL_DATE_UPLOAD = $COL_DATE_FETCH WHERE $COL_DATE_UPLOAD = 0" } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt index a0748ea317..3a42c4eb92 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt @@ -52,49 +52,46 @@ fun syncChaptersWithSource( // Chapters whose metadata have changed. val toChange = mutableListOf() + // Chapters from the db not in source. + val toDelete = dbChapters.filterNot { dbChapter -> + sourceChapters.any { sourceChapter -> + dbChapter.url == sourceChapter.url + } + } + for (sourceChapter in sourceChapters) { + // This forces metadata update for the main viewable things in the chapter list. + if (source is HttpSource) { + source.prepareNewChapter(sourceChapter, manga) + } + // Recognize chapter number for the chapter. + ChapterRecognition.parseChapterNumber(sourceChapter, manga) + 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) { + if (sourceChapter.date_upload == 0L) { + sourceChapter.date_upload = Date().time + } toAdd.add(sourceChapter) } else { - // this forces metadata update for the main viewable things in the chapter list - if (source is HttpSource) { - source.prepareNewChapter(sourceChapter, manga) - } - - ChapterRecognition.parseChapterNumber(sourceChapter, manga) - if (shouldUpdateDbChapter(dbChapter, sourceChapter)) { if (dbChapter.name != sourceChapter.name && downloadManager.isChapterDownloaded(dbChapter, manga)) { downloadManager.renameChapter(source, manga, dbChapter, sourceChapter) } dbChapter.scanlator = sourceChapter.scanlator dbChapter.name = sourceChapter.name - dbChapter.date_upload = sourceChapter.date_upload dbChapter.chapter_number = sourceChapter.chapter_number dbChapter.source_order = sourceChapter.source_order + if (sourceChapter.date_upload != 0L) { + dbChapter.date_upload = sourceChapter.date_upload + } toChange.add(dbChapter) } } } - // Recognize number for new chapters. - toAdd.forEach { - if (source is HttpSource) { - source.prepareNewChapter(it, manga) - } - ChapterRecognition.parseChapterNumber(it, manga) - } - - // Chapters from the db not in the source. - val toDelete = dbChapters.filterNot { dbChapter -> - sourceChapters.any { sourceChapter -> - dbChapter.url == sourceChapter.url - } - } - // 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()) @@ -105,12 +102,13 @@ fun syncChaptersWithSource( db.inTransaction { val deletedChapterNumbers = TreeSet() val deletedReadChapterNumbers = TreeSet() + if (toDelete.isNotEmpty()) { - for (c in toDelete) { - if (c.read) { - deletedReadChapterNumbers.add(c.chapter_number) + for (chapter in toDelete) { + if (chapter.read) { + deletedReadChapterNumbers.add(chapter.chapter_number) } - deletedChapterNumbers.add(c.chapter_number) + deletedChapterNumbers.add(chapter.chapter_number) } db.deleteChapters(toDelete).executeAsBlocking() } @@ -121,14 +119,14 @@ fun syncChaptersWithSource( var now = Date().time for (i in toAdd.indices.reversed()) { - val c = toAdd[i] - c.date_fetch = now++ + val chapter = toAdd[i] + chapter.date_fetch = now++ // Try to mark already read chapters as read when the source deletes them - if (c.isRecognizedNumber && c.chapter_number in deletedReadChapterNumbers) { - c.read = true + if (chapter.isRecognizedNumber && chapter.chapter_number in deletedReadChapterNumbers) { + chapter.read = true } - if (c.isRecognizedNumber && c.chapter_number in deletedChapterNumbers) { - readded.add(c) + if (chapter.isRecognizedNumber && chapter.chapter_number in deletedChapterNumbers) { + readded.add(chapter) } } val chapters = db.insertChapters(toAdd).executeAsBlocking()