From 559e5464629428c1b3461bdab9826909c27a641e Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 23 Mar 2024 10:05:01 +0200 Subject: [PATCH] Fix favorites migration --- .../kotatsu/alternatives/domain/MigrateUseCase.kt | 15 ++++----------- .../kotatsu/favourites/data/FavouritesDao.kt | 3 +++ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/domain/MigrateUseCase.kt b/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/domain/MigrateUseCase.kt index a3183fe09..ddc28c2c1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/domain/MigrateUseCase.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/domain/MigrateUseCase.kt @@ -5,9 +5,7 @@ import org.koitharu.kotatsu.core.db.MangaDatabase import org.koitharu.kotatsu.core.model.getPreferredBranch import org.koitharu.kotatsu.core.parser.MangaDataRepository import org.koitharu.kotatsu.core.parser.MangaRepository -import org.koitharu.kotatsu.details.domain.DetailsLoadUseCase import org.koitharu.kotatsu.details.domain.ProgressUpdateUseCase -import org.koitharu.kotatsu.favourites.data.FavouriteEntity import org.koitharu.kotatsu.history.data.HistoryEntity import org.koitharu.kotatsu.history.data.PROGRESS_NONE import org.koitharu.kotatsu.history.data.toMangaHistory @@ -21,7 +19,6 @@ class MigrateUseCase @Inject constructor( private val mangaDataRepository: MangaDataRepository, private val database: MangaDatabase, private val progressUpdateUseCase: ProgressUpdateUseCase, - private val useCase: DetailsLoadUseCase ) { suspend operator fun invoke(oldManga: Manga, newManga: Manga) { @@ -41,16 +38,12 @@ class MigrateUseCase @Inject constructor( database.withTransaction { // replace favorites val favoritesDao = database.getFavouritesDao() - val oldFavourite = favoritesDao.find(oldDetails.id) - if (oldFavourite != null) { + val oldFavourites = favoritesDao.findAllRaw(oldDetails.id) + if (oldFavourites.isNotEmpty()) { favoritesDao.delete(oldManga.id) - for (f in oldFavourite.categories) { - val e = FavouriteEntity( + for (f in oldFavourites) { + val e = f.copy( mangaId = newManga.id, - categoryId = f.categoryId.toLong(), - sortKey = f.sortKey, - createdAt = f.createdAt, - deletedAt = 0, ) favoritesDao.upsert(e) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/favourites/data/FavouritesDao.kt b/app/src/main/kotlin/org/koitharu/kotatsu/favourites/data/FavouritesDao.kt index df73a790d..53f5f5bb1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/favourites/data/FavouritesDao.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/favourites/data/FavouritesDao.kt @@ -118,6 +118,9 @@ abstract class FavouritesDao { @Query("SELECT * FROM favourites WHERE manga_id = :id AND deleted_at = 0 GROUP BY manga_id") abstract suspend fun find(id: Long): FavouriteManga? + @Query("SELECT * FROM favourites WHERE manga_id = :mangaId AND deleted_at = 0") + abstract suspend fun findAllRaw(mangaId: Long): List + @Transaction @Deprecated("Ignores order") @Query("SELECT * FROM favourites WHERE manga_id = :id AND deleted_at = 0 GROUP BY manga_id")