mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-29 17:13:16 -05:00
Better Upload Date to not have a single blank upload date. (#6358)
This commit is contained in:
parent
9e83130bd8
commit
a09d6c0470
3 changed files with 38 additions and 34 deletions
|
@ -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) {
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -52,49 +52,46 @@ fun syncChaptersWithSource(
|
|||
// Chapters whose metadata have changed.
|
||||
val toChange = mutableListOf<Chapter>()
|
||||
|
||||
// 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<Float>()
|
||||
val deletedReadChapterNumbers = TreeSet<Float>()
|
||||
|
||||
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()
|
||||
|
|
Loading…
Reference in a new issue