diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaDataRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaDataRepository.kt index bf10e6b3c..8a365547c 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaDataRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaDataRepository.kt @@ -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.toManga 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.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaSource @@ -77,10 +78,18 @@ class MangaDataRepository @Inject constructor( } suspend fun storeManga(manga: Manga) { - val tags = manga.tags.toEntities() db.withTransaction { - db.getTagsDao().upsert(tags) - db.getMangaDao().upsert(manga.toEntity(), tags) + // avoid storing local manga if remote one is already stored + 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) + } } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Http.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Http.kt index 04c456b9b..988e06920 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Http.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Http.kt @@ -8,6 +8,7 @@ import okhttp3.Response import okhttp3.internal.closeQuietly import okio.IOException import org.json.JSONObject +import org.jsoup.HttpStatusException import java.net.HttpURLConnection private val TYPE_JSON = "application/json".toMediaType() @@ -34,9 +35,8 @@ val HttpUrl.isHttpOrHttps: Boolean fun Response.ensureSuccess() = apply { if (!isSuccessful || code == HttpURLConnection.HTTP_NO_CONTENT) { - val message = "Invalid response: $code $message at ${request.url}" closeQuietly() - throw IllegalStateException(message) + throw HttpStatusException(message, code, request.url.toString()) } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt index 29d7aae76..d039d5a87 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/history/data/HistoryRepository.kt @@ -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.findById 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.ui.util.ReversibleHandle import org.koitharu.kotatsu.core.util.ext.mapItems @@ -36,6 +37,7 @@ class HistoryRepository @Inject constructor( private val trackingRepository: TrackingRepository, private val settings: AppSettings, private val scrobblers: Set<@JvmSuppressWildcards Scrobbler>, + private val mangaRepository: MangaDataRepository, ) { suspend fun getList(offset: Int, limit: Int): List { @@ -92,13 +94,8 @@ class HistoryRepository @Inject constructor( if (shouldSkip(manga)) { return } - val tags = manga.tags.toEntities() db.withTransaction { - val existing = db.getMangaDao().find(manga.id)?.manga - if (existing == null || existing.source == manga.source.name) { - db.getTagsDao().upsert(tags) - db.getMangaDao().upsert(manga.toEntity(), tags) - } + mangaRepository.storeManga(manga) db.getHistoryDao().upsert( HistoryEntity( mangaId = manga.id,