From 5bed854b9c90c0cc89a560d6ca02e842cb4c06e1 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sun, 10 Apr 2022 11:00:05 +0300 Subject: [PATCH] Refactor entity mapping --- .../base/domain/MangaDataRepository.kt | 17 ++--- .../kotatsu/core/db/entity/EntityMapping.kt | 76 +++++++++++++++++++ .../kotatsu/core/db/entity/MangaEntity.kt | 43 +---------- .../core/db/entity/MangaPrefsEntity.kt | 6 +- .../kotatsu/core/db/entity/MangaTagsEntity.kt | 3 +- .../kotatsu/core/db/entity/MangaWithTags.kt | 8 +- .../kotatsu/core/db/entity/TagEntity.kt | 23 +----- .../kotatsu/core/db/entity/TrackEntity.kt | 3 +- .../kotatsu/core/db/entity/TrackLogEntity.kt | 5 +- .../core/db/entity/TrackLogWithManga.kt | 16 +--- .../kotatsu/favourites/data/EntityMapping.kt | 14 ++++ .../data/FavouriteCategoryEntity.kt | 14 +--- .../favourites/domain/FavouritesRepository.kt | 19 +++-- .../kotatsu/history/data/EntityMapping.kt | 12 +++ .../kotatsu/history/data/HistoryEntity.kt | 16 +--- .../kotatsu/history/data/HistoryWithManga.kt | 2 +- .../history/domain/HistoryRepository.kt | 23 +++--- .../search/domain/MangaSearchRepository.kt | 2 + .../suggestions/data/SuggestionEntity.kt | 2 +- .../suggestions/data/SuggestionWithManga.kt | 2 +- .../domain/SuggestionRepository.kt | 13 ++-- .../tracker/domain/TrackingRepository.kt | 6 +- 22 files changed, 166 insertions(+), 159 deletions(-) create mode 100644 app/src/main/java/org/koitharu/kotatsu/core/db/entity/EntityMapping.kt create mode 100644 app/src/main/java/org/koitharu/kotatsu/favourites/data/EntityMapping.kt create mode 100644 app/src/main/java/org/koitharu/kotatsu/history/data/EntityMapping.kt diff --git a/app/src/main/java/org/koitharu/kotatsu/base/domain/MangaDataRepository.kt b/app/src/main/java/org/koitharu/kotatsu/base/domain/MangaDataRepository.kt index 21f9c9989..179d87868 100644 --- a/app/src/main/java/org/koitharu/kotatsu/base/domain/MangaDataRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/base/domain/MangaDataRepository.kt @@ -2,22 +2,19 @@ package org.koitharu.kotatsu.base.domain import androidx.room.withTransaction import org.koitharu.kotatsu.core.db.MangaDatabase -import org.koitharu.kotatsu.core.db.entity.MangaEntity -import org.koitharu.kotatsu.core.db.entity.MangaPrefsEntity -import org.koitharu.kotatsu.core.db.entity.TagEntity +import org.koitharu.kotatsu.core.db.entity.* import org.koitharu.kotatsu.core.prefs.ReaderMode import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.model.MangaTag -import org.koitharu.kotatsu.parsers.util.mapToSet class MangaDataRepository(private val db: MangaDatabase) { suspend fun savePreferences(manga: Manga, mode: ReaderMode) { - val tags = manga.tags.map(TagEntity.Companion::fromMangaTag) + val tags = manga.tags.toEntities() db.withTransaction { db.tagsDao.upsert(tags) - db.mangaDao.upsert(MangaEntity.from(manga), tags) + db.mangaDao.upsert(manga.toEntity(), tags) db.preferencesDao.upsert( MangaPrefsEntity( mangaId = manga.id, @@ -42,16 +39,14 @@ class MangaDataRepository(private val db: MangaDatabase) { } suspend fun storeManga(manga: Manga) { - val tags = manga.tags.map(TagEntity.Companion::fromMangaTag) + val tags = manga.tags.toEntities() db.withTransaction { db.tagsDao.upsert(tags) - db.mangaDao.upsert(MangaEntity.from(manga), tags) + db.mangaDao.upsert(manga.toEntity(), tags) } } suspend fun findTags(source: MangaSource): Set { - return db.tagsDao.findTags(source.name).mapToSet { - it.toMangaTag() - } + return db.tagsDao.findTags(source.name).toMangaTags() } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/EntityMapping.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/EntityMapping.kt new file mode 100644 index 000000000..51cd77b2e --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/EntityMapping.kt @@ -0,0 +1,76 @@ +package org.koitharu.kotatsu.core.db.entity + +import java.util.* +import org.koitharu.kotatsu.core.model.TrackingLogItem +import org.koitharu.kotatsu.parsers.model.* +import org.koitharu.kotatsu.parsers.util.longHashCode +import org.koitharu.kotatsu.parsers.util.mapToSet +import org.koitharu.kotatsu.parsers.util.toTitleCase + +// Entity to model + +fun TagEntity.toMangaTag() = MangaTag( + key = this.key, + title = this.title.toTitleCase(), + source = MangaSource.valueOf(this.source), +) + +fun Collection.toMangaTags() = mapToSet(TagEntity::toMangaTag) + +fun MangaEntity.toManga(tags: Set) = Manga( + id = this.id, + title = this.title, + altTitle = this.altTitle, + state = this.state?.let { MangaState.valueOf(it) }, + rating = this.rating, + isNsfw = this.isNsfw, + url = this.url, + publicUrl = this.publicUrl, + coverUrl = this.coverUrl, + largeCoverUrl = this.largeCoverUrl, + author = this.author, + source = MangaSource.valueOf(this.source), + tags = tags +) + +fun MangaWithTags.toManga() = manga.toManga(tags.toMangaTags()) + +fun TrackLogWithManga.toTrackingLogItem() = TrackingLogItem( + id = trackLog.id, + chapters = trackLog.chapters.split('\n').filterNot { x -> x.isEmpty() }, + manga = manga.toManga(tags.toMangaTags()), + createdAt = Date(trackLog.createdAt) +) + +// Model to entity + +fun Manga.toEntity() = MangaEntity( + id = id, + url = url, + publicUrl = publicUrl, + source = source.name, + largeCoverUrl = largeCoverUrl, + coverUrl = coverUrl, + altTitle = altTitle, + rating = rating, + isNsfw = isNsfw, + state = state?.name, + title = title, + author = author, +) + +fun MangaTag.toEntity() = TagEntity( + title = title, + key = key, + source = source.name, + id = "${key}_${source.name}".longHashCode() +) + +fun Collection.toEntities() = map(MangaTag::toEntity) + +// Other + +@Suppress("FunctionName") +fun SortOrder(name: String, fallback: SortOrder): SortOrder = runCatching { + SortOrder.valueOf(name) +}.getOrDefault(fallback) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaEntity.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaEntity.kt index 6716cfafd..c425f0e68 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaEntity.kt @@ -3,10 +3,6 @@ package org.koitharu.kotatsu.core.db.entity import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.koitharu.kotatsu.parsers.model.Manga -import org.koitharu.kotatsu.parsers.model.MangaSource -import org.koitharu.kotatsu.parsers.model.MangaState -import org.koitharu.kotatsu.parsers.model.MangaTag @Entity(tableName = "manga") class MangaEntity( @@ -16,46 +12,11 @@ class MangaEntity( @ColumnInfo(name = "alt_title") val altTitle: String?, @ColumnInfo(name = "url") val url: String, @ColumnInfo(name = "public_url") val publicUrl: String, - @ColumnInfo(name = "rating") val rating: Float, //normalized value [0..1] or -1 + @ColumnInfo(name = "rating") val rating: Float, // normalized value [0..1] or -1 @ColumnInfo(name = "nsfw") val isNsfw: Boolean, @ColumnInfo(name = "cover_url") val coverUrl: String, @ColumnInfo(name = "large_cover_url") val largeCoverUrl: String?, @ColumnInfo(name = "state") val state: String?, @ColumnInfo(name = "author") val author: String?, @ColumnInfo(name = "source") val source: String -) { - - fun toManga(tags: Set = emptySet()) = Manga( - id = this.id, - title = this.title, - altTitle = this.altTitle, - state = this.state?.let { MangaState.valueOf(it) }, - rating = this.rating, - isNsfw = this.isNsfw, - url = this.url, - publicUrl = this.publicUrl, - coverUrl = this.coverUrl, - largeCoverUrl = this.largeCoverUrl, - author = this.author, - source = MangaSource.valueOf(this.source), - tags = tags - ) - - companion object { - - fun from(manga: Manga) = MangaEntity( - id = manga.id, - url = manga.url, - publicUrl = manga.publicUrl, - source = manga.source.name, - largeCoverUrl = manga.largeCoverUrl, - coverUrl = manga.coverUrl, - altTitle = manga.altTitle, - rating = manga.rating, - isNsfw = manga.isNsfw, - state = manga.state?.name, - title = manga.title, - author = manga.author - ) - } -} \ No newline at end of file +) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaPrefsEntity.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaPrefsEntity.kt index 99e94b25a..a09ccd884 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaPrefsEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaPrefsEntity.kt @@ -6,13 +6,15 @@ import androidx.room.ForeignKey import androidx.room.PrimaryKey @Entity( - tableName = "preferences", foreignKeys = [ + tableName = "preferences", + foreignKeys = [ ForeignKey( entity = MangaEntity::class, parentColumns = ["manga_id"], childColumns = ["manga_id"], onDelete = ForeignKey.CASCADE - )] + ) + ] ) class MangaPrefsEntity( @PrimaryKey(autoGenerate = false) diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaTagsEntity.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaTagsEntity.kt index ea7f0b3d2..d3ee401a6 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaTagsEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaTagsEntity.kt @@ -5,7 +5,8 @@ import androidx.room.Entity import androidx.room.ForeignKey @Entity( - tableName = "manga_tags", primaryKeys = ["manga_id", "tag_id"], foreignKeys = [ + tableName = "manga_tags", primaryKeys = ["manga_id", "tag_id"], + foreignKeys = [ ForeignKey( entity = MangaEntity::class, parentColumns = ["manga_id"], diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaWithTags.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaWithTags.kt index 524fe906d..8c35c376e 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaWithTags.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/MangaWithTags.kt @@ -3,7 +3,6 @@ package org.koitharu.kotatsu.core.db.entity import androidx.room.Embedded import androidx.room.Junction import androidx.room.Relation -import org.koitharu.kotatsu.parsers.util.mapToSet class MangaWithTags( @Embedded val manga: MangaEntity, @@ -12,8 +11,5 @@ class MangaWithTags( entityColumn = "tag_id", associateBy = Junction(MangaTagsEntity::class) ) - val tags: List -) { - - fun toManga() = manga.toManga(tags.mapToSet { it.toMangaTag() }) -} \ No newline at end of file + val tags: List, +) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TagEntity.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TagEntity.kt index 150715f4a..fe813a02b 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TagEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TagEntity.kt @@ -3,10 +3,6 @@ package org.koitharu.kotatsu.core.db.entity import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.koitharu.kotatsu.parsers.model.MangaSource -import org.koitharu.kotatsu.parsers.model.MangaTag -import org.koitharu.kotatsu.parsers.util.longHashCode -import org.koitharu.kotatsu.parsers.util.toTitleCase @Entity(tableName = "tags") class TagEntity( @@ -15,21 +11,4 @@ class TagEntity( @ColumnInfo(name = "title") val title: String, @ColumnInfo(name = "key") val key: String, @ColumnInfo(name = "source") val source: String -) { - - fun toMangaTag() = MangaTag( - key = this.key, - title = this.title.toTitleCase(), - source = MangaSource.valueOf(this.source) - ) - - companion object { - - fun fromMangaTag(tag: MangaTag) = TagEntity( - title = tag.title, - key = tag.key, - source = tag.source.name, - id = "${tag.key}_${tag.source.name}".longHashCode() - ) - } -} \ No newline at end of file +) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackEntity.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackEntity.kt index 1b5c41492..91d65d82b 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackEntity.kt @@ -6,7 +6,8 @@ import androidx.room.ForeignKey import androidx.room.PrimaryKey @Entity( - tableName = "tracks", foreignKeys = [ + tableName = "tracks", + foreignKeys = [ ForeignKey( entity = MangaEntity::class, parentColumns = ["manga_id"], diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackLogEntity.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackLogEntity.kt index a4f608c4d..8bb8e61b4 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackLogEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackLogEntity.kt @@ -6,7 +6,8 @@ import androidx.room.ForeignKey import androidx.room.PrimaryKey @Entity( - tableName = "track_logs", foreignKeys = [ + tableName = "track_logs", + foreignKeys = [ ForeignKey( entity = MangaEntity::class, parentColumns = ["manga_id"], @@ -20,5 +21,5 @@ class TrackLogEntity( @ColumnInfo(name = "id") val id: Long = 0L, @ColumnInfo(name = "manga_id", index = true) val mangaId: Long, @ColumnInfo(name = "chapters") val chapters: String, - @ColumnInfo(name = "created_at") val createdAt: Long = System.currentTimeMillis() + @ColumnInfo(name = "created_at") val createdAt: Long = System.currentTimeMillis(), ) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackLogWithManga.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackLogWithManga.kt index 037b22ad8..7a6e145a4 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackLogWithManga.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackLogWithManga.kt @@ -3,10 +3,6 @@ package org.koitharu.kotatsu.core.db.entity import androidx.room.Embedded import androidx.room.Junction import androidx.room.Relation -import java.util.* -import org.koitharu.kotatsu.core.model.TrackingLogItem -import org.koitharu.kotatsu.parsers.util.mapToSet -import org.koitharu.kotatsu.utils.ext.mapToSet class TrackLogWithManga( @Embedded val trackLog: TrackLogEntity, @@ -20,13 +16,5 @@ class TrackLogWithManga( entityColumn = "tag_id", associateBy = Junction(MangaTagsEntity::class) ) - val tags: List -) { - - fun toTrackingLogItem() = TrackingLogItem( - id = trackLog.id, - chapters = trackLog.chapters.split('\n').filterNot { x -> x.isEmpty() }, - manga = manga.toManga(tags.mapToSet { x -> x.toMangaTag() }), - createdAt = Date(trackLog.createdAt) - ) -} \ No newline at end of file + val tags: List, +) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/favourites/data/EntityMapping.kt b/app/src/main/java/org/koitharu/kotatsu/favourites/data/EntityMapping.kt new file mode 100644 index 000000000..801f2566a --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/favourites/data/EntityMapping.kt @@ -0,0 +1,14 @@ +package org.koitharu.kotatsu.favourites.data + +import java.util.* +import org.koitharu.kotatsu.core.db.entity.SortOrder +import org.koitharu.kotatsu.core.model.FavouriteCategory +import org.koitharu.kotatsu.parsers.model.SortOrder + +fun FavouriteCategoryEntity.toFavouriteCategory(id: Long = categoryId.toLong()) = FavouriteCategory( + id = id, + title = title, + sortKey = sortKey, + order = SortOrder(order, SortOrder.NEWEST), + createdAt = Date(createdAt), +) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoryEntity.kt b/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoryEntity.kt index b7a81e840..d2b0bc7ed 100644 --- a/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoryEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/favourites/data/FavouriteCategoryEntity.kt @@ -3,9 +3,6 @@ package org.koitharu.kotatsu.favourites.data import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.koitharu.kotatsu.core.model.FavouriteCategory -import org.koitharu.kotatsu.parsers.model.SortOrder -import java.util.* @Entity(tableName = "favourite_categories") class FavouriteCategoryEntity( @@ -15,13 +12,4 @@ class FavouriteCategoryEntity( @ColumnInfo(name = "sort_key") val sortKey: Int, @ColumnInfo(name = "title") val title: String, @ColumnInfo(name = "order") val order: String, -) { - - fun toFavouriteCategory(id: Long? = null) = FavouriteCategory( - id = id ?: categoryId.toLong(), - title = title, - sortKey = sortKey, - order = SortOrder.values().find { x -> x.name == order } ?: SortOrder.NEWEST, - createdAt = Date(createdAt), - ) -} \ No newline at end of file +) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/favourites/domain/FavouritesRepository.kt b/app/src/main/java/org/koitharu/kotatsu/favourites/domain/FavouritesRepository.kt index 49156d9ce..5cb19fda5 100644 --- a/app/src/main/java/org/koitharu/kotatsu/favourites/domain/FavouritesRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/favourites/domain/FavouritesRepository.kt @@ -6,36 +6,35 @@ import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.map import org.koitharu.kotatsu.core.db.MangaDatabase -import org.koitharu.kotatsu.core.db.entity.MangaEntity -import org.koitharu.kotatsu.core.db.entity.TagEntity +import org.koitharu.kotatsu.core.db.entity.* import org.koitharu.kotatsu.core.model.FavouriteCategory import org.koitharu.kotatsu.favourites.data.FavouriteCategoryEntity import org.koitharu.kotatsu.favourites.data.FavouriteEntity +import org.koitharu.kotatsu.favourites.data.toFavouriteCategory import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.SortOrder -import org.koitharu.kotatsu.parsers.util.mapToSet import org.koitharu.kotatsu.utils.ext.mapItems class FavouritesRepository(private val db: MangaDatabase) { suspend fun getAllManga(): List { val entities = db.favouritesDao.findAll() - return entities.map { it.manga.toManga(it.tags.mapToSet(TagEntity::toMangaTag)) } + return entities.map { it.manga.toManga(it.tags.toMangaTags()) } } fun observeAll(order: SortOrder): Flow> { return db.favouritesDao.observeAll(order) - .mapItems { it.manga.toManga(it.tags.mapToSet(TagEntity::toMangaTag)) } + .mapItems { it.manga.toManga(it.tags.toMangaTags()) } } suspend fun getManga(categoryId: Long): List { val entities = db.favouritesDao.findAll(categoryId) - return entities.map { it.manga.toManga(it.tags.mapToSet(TagEntity::toMangaTag)) } + return entities.map { it.manga.toManga(it.tags.toMangaTags()) } } fun observeAll(categoryId: Long, order: SortOrder): Flow> { return db.favouritesDao.observeAll(categoryId, order) - .mapItems { it.manga.toManga(it.tags.mapToSet(TagEntity::toMangaTag)) } + .mapItems { it.manga.toManga(it.tags.toMangaTags()) } } fun observeAll(categoryId: Long): Flow> { @@ -95,9 +94,9 @@ class FavouritesRepository(private val db: MangaDatabase) { suspend fun addToCategory(categoryId: Long, mangas: Collection) { db.withTransaction { for (manga in mangas) { - val tags = manga.tags.map(TagEntity.Companion::fromMangaTag) + val tags = manga.tags.toEntities() db.tagsDao.upsert(tags) - db.mangaDao.upsert(MangaEntity.from(manga), tags) + db.mangaDao.upsert(manga.toEntity(), tags) val entity = FavouriteEntity(manga.id, categoryId, System.currentTimeMillis()) db.favouritesDao.insert(entity) } @@ -122,7 +121,7 @@ class FavouritesRepository(private val db: MangaDatabase) { private fun observeOrder(categoryId: Long): Flow { return db.favouriteCategoriesDao.observe(categoryId) - .map { x -> SortOrder.values().find { it.name == x.order } ?: SortOrder.NEWEST } + .map { x -> SortOrder(x.order, SortOrder.NEWEST) } .distinctUntilChanged() } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/history/data/EntityMapping.kt b/app/src/main/java/org/koitharu/kotatsu/history/data/EntityMapping.kt new file mode 100644 index 000000000..c03300b99 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/history/data/EntityMapping.kt @@ -0,0 +1,12 @@ +package org.koitharu.kotatsu.history.data + +import java.util.* +import org.koitharu.kotatsu.core.model.MangaHistory + +fun HistoryEntity.toMangaHistory() = MangaHistory( + createdAt = Date(createdAt), + updatedAt = Date(updatedAt), + chapterId = chapterId, + page = page, + scroll = scroll.toInt() +) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryEntity.kt b/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryEntity.kt index 770181595..24a487c60 100644 --- a/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryEntity.kt @@ -5,11 +5,10 @@ import androidx.room.Entity import androidx.room.ForeignKey import androidx.room.PrimaryKey import org.koitharu.kotatsu.core.db.entity.MangaEntity -import org.koitharu.kotatsu.core.model.MangaHistory -import java.util.* @Entity( - tableName = "history", foreignKeys = [ + tableName = "history", + foreignKeys = [ ForeignKey( entity = MangaEntity::class, parentColumns = ["manga_id"], @@ -26,13 +25,4 @@ class HistoryEntity( @ColumnInfo(name = "chapter_id") val chapterId: Long, @ColumnInfo(name = "page") val page: Int, @ColumnInfo(name = "scroll") val scroll: Float, -) { - - fun toMangaHistory() = MangaHistory( - createdAt = Date(createdAt), - updatedAt = Date(updatedAt), - chapterId = chapterId, - page = page, - scroll = scroll.toInt() - ) -} \ No newline at end of file +) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryWithManga.kt b/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryWithManga.kt index 55f41adc6..eda8c0010 100644 --- a/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryWithManga.kt +++ b/app/src/main/java/org/koitharu/kotatsu/history/data/HistoryWithManga.kt @@ -19,5 +19,5 @@ class HistoryWithManga( entityColumn = "tag_id", associateBy = Junction(MangaTagsEntity::class) ) - val tags: List + val tags: List, ) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/history/domain/HistoryRepository.kt b/app/src/main/java/org/koitharu/kotatsu/history/domain/HistoryRepository.kt index 0701dafd0..a88c8a82d 100644 --- a/app/src/main/java/org/koitharu/kotatsu/history/domain/HistoryRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/history/domain/HistoryRepository.kt @@ -5,14 +5,13 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.map import org.koitharu.kotatsu.core.db.MangaDatabase -import org.koitharu.kotatsu.core.db.entity.MangaEntity -import org.koitharu.kotatsu.core.db.entity.TagEntity +import org.koitharu.kotatsu.core.db.entity.* import org.koitharu.kotatsu.core.model.MangaHistory import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.history.data.HistoryEntity +import org.koitharu.kotatsu.history.data.toMangaHistory import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaTag -import org.koitharu.kotatsu.parsers.util.mapToSet import org.koitharu.kotatsu.tracker.domain.TrackingRepository import org.koitharu.kotatsu.utils.ext.mapItems @@ -24,25 +23,25 @@ class HistoryRepository( suspend fun getList(offset: Int, limit: Int = 20): List { val entities = db.historyDao.findAll(offset, limit) - return entities.map { it.manga.toManga(it.tags.mapToSet(TagEntity::toMangaTag)) } + return entities.map { it.manga.toManga(it.tags.toMangaTags()) } } suspend fun getLastOrNull(): Manga? { val entity = db.historyDao.findAll(0, 1).firstOrNull() ?: return null - return entity.manga.toManga(entity.tags.mapToSet { it.toMangaTag() }) + return entity.manga.toManga(entity.tags.toMangaTags()) } fun observeAll(): Flow> { return db.historyDao.observeAll().mapItems { - it.manga.toManga(it.tags.mapToSet(TagEntity::toMangaTag)) + it.manga.toManga(it.tags.toMangaTags()) } } fun observeAllWithHistory(): Flow> { return db.historyDao.observeAll().mapItems { MangaWithHistory( - it.manga.toManga(it.tags.mapToSet(TagEntity::toMangaTag)), - it.history.toMangaHistory() + it.manga.toManga(it.tags.toMangaTags()), + it.history.toMangaHistory(), ) } } @@ -63,10 +62,10 @@ class HistoryRepository( if (manga.isNsfw && settings.isHistoryExcludeNsfw) { return } - val tags = manga.tags.map(TagEntity.Companion::fromMangaTag) + val tags = manga.tags.toEntities() db.withTransaction { db.tagsDao.upsert(tags) - db.mangaDao.upsert(MangaEntity.from(manga), tags) + db.mangaDao.upsert(manga.toEntity(), tags) db.historyDao.upsert( HistoryEntity( mangaId = manga.id, @@ -74,7 +73,7 @@ class HistoryRepository( updatedAt = System.currentTimeMillis(), chapterId = chapterId, page = page, - scroll = scroll.toFloat() // we migrate to int, but decide to not update database + scroll = scroll.toFloat(), // we migrate to int, but decide to not update database ) ) trackingRepository.upsert(manga) @@ -106,7 +105,7 @@ class HistoryRepository( * Useful for replacing saved manga on deleting it with remove source */ suspend fun deleteOrSwap(manga: Manga, alternative: Manga?) { - if (alternative == null || db.mangaDao.update(MangaEntity.from(alternative)) <= 0) { + if (alternative == null || db.mangaDao.update(alternative.toEntity()) <= 0) { db.historyDao.delete(manga.id) } } diff --git a/app/src/main/java/org/koitharu/kotatsu/search/domain/MangaSearchRepository.kt b/app/src/main/java/org/koitharu/kotatsu/search/domain/MangaSearchRepository.kt index c8ed656e7..270238553 100644 --- a/app/src/main/java/org/koitharu/kotatsu/search/domain/MangaSearchRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/search/domain/MangaSearchRepository.kt @@ -10,6 +10,8 @@ import kotlinx.coroutines.flow.* import kotlinx.coroutines.isActive import kotlinx.coroutines.withContext import org.koitharu.kotatsu.core.db.MangaDatabase +import org.koitharu.kotatsu.core.db.entity.toManga +import org.koitharu.kotatsu.core.db.entity.toMangaTag import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.parsers.model.Manga diff --git a/app/src/main/java/org/koitharu/kotatsu/suggestions/data/SuggestionEntity.kt b/app/src/main/java/org/koitharu/kotatsu/suggestions/data/SuggestionEntity.kt index 97212bf2e..c49daad43 100644 --- a/app/src/main/java/org/koitharu/kotatsu/suggestions/data/SuggestionEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/suggestions/data/SuggestionEntity.kt @@ -24,4 +24,4 @@ class SuggestionEntity( @FloatRange(from = 0.0, to = 1.0) @ColumnInfo(name = "relevance") val relevance: Float, @ColumnInfo(name = "created_at") val createdAt: Long = System.currentTimeMillis(), -) +) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/suggestions/data/SuggestionWithManga.kt b/app/src/main/java/org/koitharu/kotatsu/suggestions/data/SuggestionWithManga.kt index 13aa11bec..c7fc3c15f 100644 --- a/app/src/main/java/org/koitharu/kotatsu/suggestions/data/SuggestionWithManga.kt +++ b/app/src/main/java/org/koitharu/kotatsu/suggestions/data/SuggestionWithManga.kt @@ -19,5 +19,5 @@ data class SuggestionWithManga( entityColumn = "tag_id", associateBy = Junction(MangaTagsEntity::class) ) - val tags: List + val tags: List, ) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/suggestions/domain/SuggestionRepository.kt b/app/src/main/java/org/koitharu/kotatsu/suggestions/domain/SuggestionRepository.kt index 17dd71840..d334f31ef 100644 --- a/app/src/main/java/org/koitharu/kotatsu/suggestions/domain/SuggestionRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/suggestions/domain/SuggestionRepository.kt @@ -3,10 +3,11 @@ package org.koitharu.kotatsu.suggestions.domain import androidx.room.withTransaction import kotlinx.coroutines.flow.Flow import org.koitharu.kotatsu.core.db.MangaDatabase -import org.koitharu.kotatsu.core.db.entity.MangaEntity -import org.koitharu.kotatsu.core.db.entity.TagEntity +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.parsers.model.Manga -import org.koitharu.kotatsu.parsers.util.mapToSet import org.koitharu.kotatsu.suggestions.data.SuggestionEntity import org.koitharu.kotatsu.utils.ext.mapItems @@ -16,7 +17,7 @@ class SuggestionRepository( fun observeAll(): Flow> { return db.suggestionDao.observeAll().mapItems { - it.manga.toManga(it.tags.mapToSet(TagEntity::toMangaTag)) + it.manga.toManga(it.tags.toMangaTags()) } } @@ -32,9 +33,9 @@ class SuggestionRepository( db.withTransaction { db.suggestionDao.deleteAll() suggestions.forEach { x -> - val tags = x.manga.tags.map(TagEntity.Companion::fromMangaTag) + val tags = x.manga.tags.toEntities() db.tagsDao.upsert(tags) - db.mangaDao.upsert(MangaEntity.from(x.manga), tags) + db.mangaDao.upsert(x.manga.toEntity(), tags) db.suggestionDao.upsert( SuggestionEntity( mangaId = x.manga.id, diff --git a/app/src/main/java/org/koitharu/kotatsu/tracker/domain/TrackingRepository.kt b/app/src/main/java/org/koitharu/kotatsu/tracker/domain/TrackingRepository.kt index dd0c4e9ec..661f68bba 100644 --- a/app/src/main/java/org/koitharu/kotatsu/tracker/domain/TrackingRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/tracker/domain/TrackingRepository.kt @@ -4,6 +4,8 @@ import androidx.room.withTransaction import org.koitharu.kotatsu.core.db.MangaDatabase import org.koitharu.kotatsu.core.db.entity.TrackEntity import org.koitharu.kotatsu.core.db.entity.TrackLogEntity +import org.koitharu.kotatsu.core.db.entity.toManga +import org.koitharu.kotatsu.core.db.entity.toTrackingLogItem import org.koitharu.kotatsu.core.model.MangaTracking import org.koitharu.kotatsu.core.model.TrackingLogItem import org.koitharu.kotatsu.parsers.model.Manga @@ -22,10 +24,10 @@ class TrackingRepository( suspend fun getAllTracks(useFavourites: Boolean, useHistory: Boolean): List { val mangaList = ArrayList() if (useFavourites) { - db.favouritesDao.findAllManga().mapTo(mangaList) { it.toManga() } + db.favouritesDao.findAllManga().mapTo(mangaList) { it.toManga(emptySet()) } } if (useHistory) { - db.historyDao.findAllManga().mapTo(mangaList) { it.toManga() } + db.historyDao.findAllManga().mapTo(mangaList) { it.toManga(emptySet()) } } val tracks = db.tracksDao.findAll().groupBy { it.mangaId } return mangaList