mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-15 23:12:48 +01:00
refactor: added isSync to MangaRestorer
Instead of overwriting it for the backup when restoring chapters we take into account if it's sync then overwrite read status, otherwise keep the old function. Where it only mark it as a read only if the dbchapter is marked as read.
This commit is contained in:
parent
18fb63cd01
commit
a462b40cad
@ -27,7 +27,7 @@ class BackupRestorer(
|
|||||||
|
|
||||||
private val categoriesRestorer: CategoriesRestorer = CategoriesRestorer(),
|
private val categoriesRestorer: CategoriesRestorer = CategoriesRestorer(),
|
||||||
private val preferenceRestorer: PreferenceRestorer = PreferenceRestorer(context),
|
private val preferenceRestorer: PreferenceRestorer = PreferenceRestorer(context),
|
||||||
private val mangaRestorer: MangaRestorer = MangaRestorer(),
|
private val mangaRestorer: MangaRestorer = MangaRestorer(isSync),
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private var restoreAmount = 0
|
private var restoreAmount = 0
|
||||||
|
@ -24,6 +24,8 @@ import java.util.Date
|
|||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
|
||||||
class MangaRestorer(
|
class MangaRestorer(
|
||||||
|
private var isSync: Boolean = false,
|
||||||
|
|
||||||
private val handler: DatabaseHandler = Injekt.get(),
|
private val handler: DatabaseHandler = Injekt.get(),
|
||||||
private val getCategories: GetCategories = Injekt.get(),
|
private val getCategories: GetCategories = Injekt.get(),
|
||||||
private val getMangaByUrlAndSourceId: GetMangaByUrlAndSourceId = Injekt.get(),
|
private val getMangaByUrlAndSourceId: GetMangaByUrlAndSourceId = Injekt.get(),
|
||||||
@ -33,7 +35,6 @@ class MangaRestorer(
|
|||||||
private val insertTrack: InsertTrack = Injekt.get(),
|
private val insertTrack: InsertTrack = Injekt.get(),
|
||||||
fetchInterval: FetchInterval = Injekt.get(),
|
fetchInterval: FetchInterval = Injekt.get(),
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private var now = ZonedDateTime.now()
|
private var now = ZonedDateTime.now()
|
||||||
private var currentFetchWindow = fetchInterval.getWindow(now)
|
private var currentFetchWindow = fetchInterval.getWindow(now)
|
||||||
|
|
||||||
@ -100,7 +101,7 @@ class MangaRestorer(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun updateManga(manga: Manga): Manga {
|
suspend fun updateManga(manga: Manga): Manga {
|
||||||
handler.await(true) {
|
handler.await(true) {
|
||||||
mangasQueries.update(
|
mangasQueries.update(
|
||||||
source = manga.source,
|
source = manga.source,
|
||||||
@ -142,36 +143,15 @@ class MangaRestorer(
|
|||||||
.associateBy { it.url }
|
.associateBy { it.url }
|
||||||
|
|
||||||
val (existingChapters, newChapters) = backupChapters
|
val (existingChapters, newChapters) = backupChapters
|
||||||
.mapNotNull {
|
.mapNotNull { backupChapter ->
|
||||||
val chapter = it.toChapterImpl().copy(mangaId = manga.id)
|
val chapter = backupChapter.toChapterImpl().copy(mangaId = manga.id)
|
||||||
|
|
||||||
val dbChapter = dbChaptersByUrl[chapter.url]
|
val dbChapter = dbChaptersByUrl[chapter.url]
|
||||||
?: // New chapter
|
|
||||||
return@mapNotNull chapter
|
|
||||||
|
|
||||||
if (chapter.forComparison() == dbChapter.forComparison()) {
|
when {
|
||||||
// Same state; skip
|
dbChapter == null -> chapter // New chapter
|
||||||
return@mapNotNull null
|
chapter.forComparison() == dbChapter.forComparison() -> null // Same state; skip
|
||||||
|
else -> updateChapterBasedOnSyncState(chapter, dbChapter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update to an existing chapter
|
|
||||||
var updatedChapter = chapter
|
|
||||||
.copyFrom(dbChapter)
|
|
||||||
.copy(
|
|
||||||
id = dbChapter.id,
|
|
||||||
bookmark = chapter.bookmark || dbChapter.bookmark,
|
|
||||||
)
|
|
||||||
if (dbChapter.read && !updatedChapter.read) {
|
|
||||||
updatedChapter = updatedChapter.copy(
|
|
||||||
read = true,
|
|
||||||
lastPageRead = dbChapter.lastPageRead,
|
|
||||||
)
|
|
||||||
} else if (updatedChapter.lastPageRead == 0L && dbChapter.lastPageRead != 0L) {
|
|
||||||
updatedChapter = updatedChapter.copy(
|
|
||||||
lastPageRead = dbChapter.lastPageRead,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
updatedChapter
|
|
||||||
}
|
}
|
||||||
.partition { it.id > 0 }
|
.partition { it.id > 0 }
|
||||||
|
|
||||||
@ -179,6 +159,25 @@ class MangaRestorer(
|
|||||||
updateExistingChapters(existingChapters)
|
updateExistingChapters(existingChapters)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun updateChapterBasedOnSyncState(chapter: Chapter, dbChapter: Chapter): Chapter {
|
||||||
|
return if (isSync) {
|
||||||
|
chapter.copy(
|
||||||
|
id = dbChapter.id,
|
||||||
|
bookmark = chapter.bookmark || dbChapter.bookmark,
|
||||||
|
read = chapter.read,
|
||||||
|
lastPageRead = chapter.lastPageRead
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
chapter.copyFrom(dbChapter).let {
|
||||||
|
when {
|
||||||
|
dbChapter.read && !it.read -> it.copy(read = true, lastPageRead = dbChapter.lastPageRead)
|
||||||
|
it.lastPageRead == 0L && dbChapter.lastPageRead != 0L -> it.copy(lastPageRead = dbChapter.lastPageRead)
|
||||||
|
else -> it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun Chapter.forComparison() =
|
private fun Chapter.forComparison() =
|
||||||
this.copy(id = 0L, mangaId = 0L, dateFetch = 0L, dateUpload = 0L, lastModifiedAt = 0L)
|
this.copy(id = 0L, mangaId = 0L, dateFetch = 0L, dateUpload = 0L, lastModifiedAt = 0L)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user