Avoid replacing online manga wthin local in database
This commit is contained in:
@@ -11,6 +11,7 @@ import org.koitharu.kotatsu.core.db.entity.toEntities
|
|||||||
import org.koitharu.kotatsu.core.db.entity.toEntity
|
import org.koitharu.kotatsu.core.db.entity.toEntity
|
||||||
import org.koitharu.kotatsu.core.db.entity.toManga
|
import org.koitharu.kotatsu.core.db.entity.toManga
|
||||||
import org.koitharu.kotatsu.core.db.entity.toMangaTags
|
import org.koitharu.kotatsu.core.db.entity.toMangaTags
|
||||||
|
import org.koitharu.kotatsu.core.model.isLocal
|
||||||
import org.koitharu.kotatsu.core.prefs.ReaderMode
|
import org.koitharu.kotatsu.core.prefs.ReaderMode
|
||||||
import org.koitharu.kotatsu.parsers.model.Manga
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||||
@@ -77,10 +78,18 @@ class MangaDataRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun storeManga(manga: Manga) {
|
suspend fun storeManga(manga: Manga) {
|
||||||
val tags = manga.tags.toEntities()
|
|
||||||
db.withTransaction {
|
db.withTransaction {
|
||||||
db.getTagsDao().upsert(tags)
|
// avoid storing local manga if remote one is already stored
|
||||||
db.getMangaDao().upsert(manga.toEntity(), tags)
|
val existing = if (manga.isLocal) {
|
||||||
|
db.getMangaDao().find(manga.id)?.manga
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
if (existing == null || existing.source == manga.source.name) {
|
||||||
|
val tags = manga.tags.toEntities()
|
||||||
|
db.getTagsDao().upsert(tags)
|
||||||
|
db.getMangaDao().upsert(manga.toEntity(), tags)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import okhttp3.Response
|
|||||||
import okhttp3.internal.closeQuietly
|
import okhttp3.internal.closeQuietly
|
||||||
import okio.IOException
|
import okio.IOException
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
|
import org.jsoup.HttpStatusException
|
||||||
import java.net.HttpURLConnection
|
import java.net.HttpURLConnection
|
||||||
|
|
||||||
private val TYPE_JSON = "application/json".toMediaType()
|
private val TYPE_JSON = "application/json".toMediaType()
|
||||||
@@ -34,9 +35,8 @@ val HttpUrl.isHttpOrHttps: Boolean
|
|||||||
|
|
||||||
fun Response.ensureSuccess() = apply {
|
fun Response.ensureSuccess() = apply {
|
||||||
if (!isSuccessful || code == HttpURLConnection.HTTP_NO_CONTENT) {
|
if (!isSuccessful || code == HttpURLConnection.HTTP_NO_CONTENT) {
|
||||||
val message = "Invalid response: $code $message at ${request.url}"
|
|
||||||
closeQuietly()
|
closeQuietly()
|
||||||
throw IllegalStateException(message)
|
throw HttpStatusException(message, code, request.url.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import org.koitharu.kotatsu.core.db.entity.toMangaTags
|
|||||||
import org.koitharu.kotatsu.core.model.MangaHistory
|
import org.koitharu.kotatsu.core.model.MangaHistory
|
||||||
import org.koitharu.kotatsu.core.model.findById
|
import org.koitharu.kotatsu.core.model.findById
|
||||||
import org.koitharu.kotatsu.core.model.isLocal
|
import org.koitharu.kotatsu.core.model.isLocal
|
||||||
|
import org.koitharu.kotatsu.core.parser.MangaDataRepository
|
||||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
import org.koitharu.kotatsu.core.ui.util.ReversibleHandle
|
import org.koitharu.kotatsu.core.ui.util.ReversibleHandle
|
||||||
import org.koitharu.kotatsu.core.util.ext.mapItems
|
import org.koitharu.kotatsu.core.util.ext.mapItems
|
||||||
@@ -36,6 +37,7 @@ class HistoryRepository @Inject constructor(
|
|||||||
private val trackingRepository: TrackingRepository,
|
private val trackingRepository: TrackingRepository,
|
||||||
private val settings: AppSettings,
|
private val settings: AppSettings,
|
||||||
private val scrobblers: Set<@JvmSuppressWildcards Scrobbler>,
|
private val scrobblers: Set<@JvmSuppressWildcards Scrobbler>,
|
||||||
|
private val mangaRepository: MangaDataRepository,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
suspend fun getList(offset: Int, limit: Int): List<Manga> {
|
suspend fun getList(offset: Int, limit: Int): List<Manga> {
|
||||||
@@ -92,13 +94,8 @@ class HistoryRepository @Inject constructor(
|
|||||||
if (shouldSkip(manga)) {
|
if (shouldSkip(manga)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val tags = manga.tags.toEntities()
|
|
||||||
db.withTransaction {
|
db.withTransaction {
|
||||||
val existing = db.getMangaDao().find(manga.id)?.manga
|
mangaRepository.storeManga(manga)
|
||||||
if (existing == null || existing.source == manga.source.name) {
|
|
||||||
db.getTagsDao().upsert(tags)
|
|
||||||
db.getMangaDao().upsert(manga.toEntity(), tags)
|
|
||||||
}
|
|
||||||
db.getHistoryDao().upsert(
|
db.getHistoryDao().upsert(
|
||||||
HistoryEntity(
|
HistoryEntity(
|
||||||
mangaId = manga.id,
|
mangaId = manga.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user