Fix foreign key error

This commit is contained in:
Koitharu
2020-03-29 17:53:30 +03:00
parent 7ee486e4f2
commit 450daf17fd
4 changed files with 28 additions and 17 deletions

View File

@@ -15,7 +15,7 @@ android {
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 29 targetSdkVersion 29
versionCode gitCommits versionCode gitCommits
versionName '0.1.2' versionName '0.1.3'
buildConfigField 'String', 'GIT_BRANCH', "\"${gitBranch}\"" buildConfigField 'String', 'GIT_BRANCH', "\"${gitBranch}\""

View File

@@ -1,5 +1,6 @@
package org.koitharu.kotatsu.domain package org.koitharu.kotatsu.domain
import androidx.room.withTransaction
import org.koin.core.KoinComponent import org.koin.core.KoinComponent
import org.koin.core.inject import org.koin.core.inject
import org.koitharu.kotatsu.core.db.MangaDatabase import org.koitharu.kotatsu.core.db.MangaDatabase
@@ -31,6 +32,10 @@ class MangaDataRepository : KoinComponent {
} }
suspend fun storeManga(manga: Manga) { suspend fun storeManga(manga: Manga) {
db.mangaDao().upsert(MangaEntity.from(manga), manga.tags.map(TagEntity.Companion::fromMangaTag)) val tags = manga.tags.map(TagEntity.Companion::fromMangaTag)
db.withTransaction {
db.tagsDao().upsert(tags)
db.mangaDao().upsert(MangaEntity.from(manga), tags)
}
} }
} }

View File

@@ -1,5 +1,6 @@
package org.koitharu.kotatsu.domain.favourites package org.koitharu.kotatsu.domain.favourites
import androidx.room.withTransaction
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.koin.core.KoinComponent import org.koin.core.KoinComponent
@@ -48,10 +49,12 @@ class FavouritesRepository : KoinComponent {
suspend fun addToCategory(manga: Manga, categoryId: Long) { suspend fun addToCategory(manga: Manga, categoryId: Long) {
val tags = manga.tags.map(TagEntity.Companion::fromMangaTag) val tags = manga.tags.map(TagEntity.Companion::fromMangaTag)
db.tagsDao().upsert(tags) db.withTransaction {
db.mangaDao().upsert(MangaEntity.from(manga), tags) db.tagsDao().upsert(tags)
val entity = FavouriteEntity(manga.id, categoryId, System.currentTimeMillis()) db.mangaDao().upsert(MangaEntity.from(manga), tags)
db.favouritesDao().add(entity) val entity = FavouriteEntity(manga.id, categoryId, System.currentTimeMillis())
db.favouritesDao().add(entity)
}
notifyFavouritesChanged(manga.id) notifyFavouritesChanged(manga.id)
} }

View File

@@ -1,5 +1,6 @@
package org.koitharu.kotatsu.domain.history package org.koitharu.kotatsu.domain.history
import androidx.room.withTransaction
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.koin.core.KoinComponent import org.koin.core.KoinComponent
@@ -23,18 +24,20 @@ class HistoryRepository : KoinComponent {
suspend fun addOrUpdate(manga: Manga, chapterId: Long, page: Int, scroll: Float) { suspend fun addOrUpdate(manga: Manga, chapterId: Long, page: Int, scroll: Float) {
val tags = manga.tags.map(TagEntity.Companion::fromMangaTag) val tags = manga.tags.map(TagEntity.Companion::fromMangaTag)
db.tagsDao().upsert(tags) db.withTransaction {
db.mangaDao().upsert(MangaEntity.from(manga), tags) db.tagsDao().upsert(tags)
db.historyDao().upsert( db.mangaDao().upsert(MangaEntity.from(manga), tags)
HistoryEntity( db.historyDao().upsert(
mangaId = manga.id, HistoryEntity(
createdAt = System.currentTimeMillis(), mangaId = manga.id,
updatedAt = System.currentTimeMillis(), createdAt = System.currentTimeMillis(),
chapterId = chapterId, updatedAt = System.currentTimeMillis(),
page = page, chapterId = chapterId,
scroll = scroll page = page,
scroll = scroll
)
) )
) }
notifyHistoryChanged() notifyHistoryChanged()
} }