Changed data-mappers to use function format (#10045)

The lambda-format was really confusing to read and keep which anonymous data
 item was corresponding to which field. Now it's directly inspectable in the IDE
This commit is contained in:
Caleb Morris 2023-10-21 18:42:09 -07:00 committed by GitHub
parent 8e4cedf173
commit 15423bfc84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 300 additions and 319 deletions

View File

@ -1,12 +0,0 @@
package tachiyomi.data.category
import tachiyomi.domain.category.model.Category
val categoryMapper: (Long, String, Long, Long) -> Category = { id, name, order, flags ->
Category(
id = id,
name = name,
order = order,
flags = flags,
)
}

View File

@ -12,26 +12,26 @@ class CategoryRepositoryImpl(
) : CategoryRepository {
override suspend fun get(id: Long): Category? {
return handler.awaitOneOrNull { categoriesQueries.getCategory(id, categoryMapper) }
return handler.awaitOneOrNull { categoriesQueries.getCategory(id, ::mapCategory) }
}
override suspend fun getAll(): List<Category> {
return handler.awaitList { categoriesQueries.getCategories(categoryMapper) }
return handler.awaitList { categoriesQueries.getCategories(::mapCategory) }
}
override fun getAllAsFlow(): Flow<List<Category>> {
return handler.subscribeToList { categoriesQueries.getCategories(categoryMapper) }
return handler.subscribeToList { categoriesQueries.getCategories(::mapCategory) }
}
override suspend fun getCategoriesByMangaId(mangaId: Long): List<Category> {
return handler.awaitList {
categoriesQueries.getCategoriesByMangaId(mangaId, categoryMapper)
categoriesQueries.getCategoriesByMangaId(mangaId, ::mapCategory)
}
}
override fun getCategoriesByMangaIdAsFlow(mangaId: Long): Flow<List<Category>> {
return handler.subscribeToList {
categoriesQueries.getCategoriesByMangaId(mangaId, categoryMapper)
categoriesQueries.getCategoriesByMangaId(mangaId, ::mapCategory)
}
}
@ -81,4 +81,18 @@ class CategoryRepositoryImpl(
)
}
}
private fun mapCategory(
id: Long,
name: String,
order: Long,
flags: Long,
): Category {
return Category(
id = id,
name = name,
order = order,
flags = flags,
)
}
}

View File

@ -1,36 +0,0 @@
package tachiyomi.data.chapter
import tachiyomi.domain.chapter.model.Chapter
val chapterMapper: (
Long,
Long,
String,
String,
String?,
Boolean,
Boolean,
Long,
Double,
Long,
Long,
Long,
Long,
) -> Chapter =
{ id, mangaId, url, name, scanlator, read, bookmark, lastPageRead, chapterNumber, sourceOrder, dateFetch, dateUpload, lastModifiedAt ->
Chapter(
id = id,
mangaId = mangaId,
read = read,
bookmark = bookmark,
lastPageRead = lastPageRead,
dateFetch = dateFetch,
sourceOrder = sourceOrder,
url = url,
name = name,
dateUpload = dateUpload,
chapterNumber = chapterNumber,
scanlator = scanlator,
lastModifiedAt = lastModifiedAt,
)
}

View File

@ -77,27 +77,27 @@ class ChapterRepositoryImpl(
}
override suspend fun getChapterByMangaId(mangaId: Long): List<Chapter> {
return handler.awaitList { chaptersQueries.getChaptersByMangaId(mangaId, chapterMapper) }
return handler.awaitList { chaptersQueries.getChaptersByMangaId(mangaId, ::mapChapter) }
}
override suspend fun getBookmarkedChaptersByMangaId(mangaId: Long): List<Chapter> {
return handler.awaitList {
chaptersQueries.getBookmarkedChaptersByMangaId(
mangaId,
chapterMapper,
::mapChapter,
)
}
}
override suspend fun getChapterById(id: Long): Chapter? {
return handler.awaitOneOrNull { chaptersQueries.getChapterById(id, chapterMapper) }
return handler.awaitOneOrNull { chaptersQueries.getChapterById(id, ::mapChapter) }
}
override suspend fun getChapterByMangaIdAsFlow(mangaId: Long): Flow<List<Chapter>> {
return handler.subscribeToList {
chaptersQueries.getChaptersByMangaId(
mangaId,
chapterMapper,
::mapChapter,
)
}
}
@ -107,8 +107,38 @@ class ChapterRepositoryImpl(
chaptersQueries.getChapterByUrlAndMangaId(
url,
mangaId,
chapterMapper,
::mapChapter,
)
}
}
private fun mapChapter(
id: Long,
mangaId: Long,
url: String,
name: String,
scanlator: String?,
read: Boolean,
bookmark: Boolean,
lastPageRead: Long,
chapterNumber: Double,
sourceOrder: Long,
dateFetch: Long,
dateUpload: Long,
lastModifiedAt: Long,
): Chapter = Chapter(
id = id,
mangaId = mangaId,
read = read,
bookmark = bookmark,
lastPageRead = lastPageRead,
dateFetch = dateFetch,
sourceOrder = sourceOrder,
url = url,
name = name,
dateUpload = dateUpload,
chapterNumber = chapterNumber,
scanlator = scanlator,
lastModifiedAt = lastModifiedAt,
)
}

View File

@ -5,30 +5,32 @@ import tachiyomi.domain.history.model.HistoryWithRelations
import tachiyomi.domain.manga.model.MangaCover
import java.util.Date
val historyMapper: (Long, Long, Date?, Long) -> History = { id, chapterId, readAt, readDuration ->
History(
object HistoryMapper {
fun mapHistory(
id: Long,
chapterId: Long,
readAt: Date?,
readDuration: Long,
): History = History(
id = id,
chapterId = chapterId,
readAt = readAt,
readDuration = readDuration,
)
}
val historyWithRelationsMapper: (
Long,
Long,
Long,
String,
String?,
Long,
Boolean,
Long,
Double,
Date?,
Long,
) -> HistoryWithRelations = {
historyId, mangaId, chapterId, title, thumbnailUrl, sourceId, isFavorite, coverLastModified, chapterNumber, readAt, readDuration ->
HistoryWithRelations(
fun mapHistoryWithRelations(
historyId: Long,
mangaId: Long,
chapterId: Long,
title: String,
thumbnailUrl: String?,
sourceId: Long,
isFavorite: Boolean,
coverLastModified: Long,
chapterNumber: Double,
readAt: Date?,
readDuration: Long,
): HistoryWithRelations = HistoryWithRelations(
id = historyId,
chapterId = chapterId,
mangaId = mangaId,

View File

@ -15,13 +15,13 @@ class HistoryRepositoryImpl(
override fun getHistory(query: String): Flow<List<HistoryWithRelations>> {
return handler.subscribeToList {
historyViewQueries.history(query, historyWithRelationsMapper)
historyViewQueries.history(query, HistoryMapper::mapHistoryWithRelations)
}
}
override suspend fun getLastHistory(): HistoryWithRelations? {
return handler.awaitOneOrNull {
historyViewQueries.getLatestHistory(historyWithRelationsMapper)
historyViewQueries.getLatestHistory(HistoryMapper::mapHistoryWithRelations)
}
}
@ -30,7 +30,7 @@ class HistoryRepositoryImpl(
}
override suspend fun getHistoryByMangaId(mangaId: Long): List<History> {
return handler.awaitList { historyQueries.getHistoryByMangaId(mangaId, historyMapper) }
return handler.awaitList { historyQueries.getHistoryByMangaId(mangaId, HistoryMapper::mapHistory) }
}
override suspend fun resetHistory(historyId: Long) {

View File

@ -4,32 +4,31 @@ import eu.kanade.tachiyomi.source.model.UpdateStrategy
import tachiyomi.domain.library.model.LibraryManga
import tachiyomi.domain.manga.model.Manga
val mangaMapper: (
Long,
Long,
String,
String?,
String?,
String?,
List<String>?,
String,
Long,
String?,
Boolean,
Long?,
Long?,
Boolean,
Long,
Long,
Long,
Long,
UpdateStrategy,
Long,
Long,
Long?,
) -> Manga =
{ id, source, url, artist, author, description, genre, title, status, thumbnailUrl, favorite, lastUpdate, nextUpdate, initialized, viewerFlags, chapterFlags, coverLastModified, dateAdded, updateStrategy, calculateInterval, lastModifiedAt, favoriteModifiedAt ->
Manga(
object MangaMapper {
fun mapManga(
id: Long,
source: Long,
url: String,
artist: String?,
author: String?,
description: String?,
genre: List<String>?,
title: String,
status: Long,
thumbnailUrl: String?,
favorite: Boolean,
lastUpdate: Long?,
nextUpdate: Long?,
initialized: Boolean,
viewerFlags: Long,
chapterFlags: Long,
coverLastModified: Long,
dateAdded: Long,
updateStrategy: UpdateStrategy,
calculateInterval: Long,
lastModifiedAt: Long,
favoriteModifiedAt: Long?,
): Manga = Manga(
id = id,
source = source,
favorite = favorite,
@ -53,42 +52,39 @@ val mangaMapper: (
lastModifiedAt = lastModifiedAt,
favoriteModifiedAt = favoriteModifiedAt,
)
}
val libraryManga: (
Long,
Long,
String,
String?,
String?,
String?,
List<String>?,
String,
Long,
String?,
Boolean,
Long?,
Long?,
Boolean,
Long,
Long,
Long,
Long,
UpdateStrategy,
Long,
Long,
Long?,
Long,
Double,
Long,
Long,
Long,
Double,
Long,
) -> LibraryManga =
{ id, source, url, artist, author, description, genre, title, status, thumbnailUrl, favorite, lastUpdate, nextUpdate, initialized, viewerFlags, chapterFlags, coverLastModified, dateAdded, updateStrategy, calculateInterval, lastModifiedAt, favoriteModifiedAt, totalCount, readCount, latestUpload, chapterFetchedAt, lastRead, bookmarkCount, category ->
LibraryManga(
manga = mangaMapper(
fun mapLibraryManga(
id: Long,
source: Long,
url: String,
artist: String?,
author: String?,
description: String?,
genre: List<String>?,
title: String,
status: Long,
thumbnailUrl: String?,
favorite: Boolean,
lastUpdate: Long?,
nextUpdate: Long?,
initialized: Boolean,
viewerFlags: Long,
chapterFlags: Long,
coverLastModified: Long,
dateAdded: Long,
updateStrategy: UpdateStrategy,
calculateInterval: Long,
lastModifiedAt: Long,
favoriteModifiedAt: Long?,
totalCount: Long,
readCount: Double,
latestUpload: Long,
chapterFetchedAt: Long,
lastRead: Long,
bookmarkCount: Double,
category: Long,
): LibraryManga = LibraryManga(
manga = mapManga(
id,
source,
url,
@ -120,4 +116,4 @@ val libraryManga: (
chapterFetchedAt = chapterFetchedAt,
lastRead = lastRead,
)
}
}

View File

@ -16,11 +16,11 @@ class MangaRepositoryImpl(
) : MangaRepository {
override suspend fun getMangaById(id: Long): Manga {
return handler.awaitOne { mangasQueries.getMangaById(id, mangaMapper) }
return handler.awaitOne { mangasQueries.getMangaById(id, MangaMapper::mapManga) }
}
override suspend fun getMangaByIdAsFlow(id: Long): Flow<Manga> {
return handler.subscribeToOne { mangasQueries.getMangaById(id, mangaMapper) }
return handler.subscribeToOne { mangasQueries.getMangaById(id, MangaMapper::mapManga) }
}
override suspend fun getMangaByUrlAndSourceId(url: String, sourceId: Long): Manga? {
@ -28,7 +28,7 @@ class MangaRepositoryImpl(
mangasQueries.getMangaByUrlAndSource(
url,
sourceId,
mangaMapper,
MangaMapper::mapManga,
)
}
}
@ -38,30 +38,30 @@ class MangaRepositoryImpl(
mangasQueries.getMangaByUrlAndSource(
url,
sourceId,
mangaMapper,
MangaMapper::mapManga,
)
}
}
override suspend fun getFavorites(): List<Manga> {
return handler.awaitList { mangasQueries.getFavorites(mangaMapper) }
return handler.awaitList { mangasQueries.getFavorites(MangaMapper::mapManga) }
}
override suspend fun getLibraryManga(): List<LibraryManga> {
return handler.awaitList { libraryViewQueries.library(libraryManga) }
return handler.awaitList { libraryViewQueries.library(MangaMapper::mapLibraryManga) }
}
override fun getLibraryMangaAsFlow(): Flow<List<LibraryManga>> {
return handler.subscribeToList { libraryViewQueries.library(libraryManga) }
return handler.subscribeToList { libraryViewQueries.library(MangaMapper::mapLibraryManga) }
}
override fun getFavoritesBySourceId(sourceId: Long): Flow<List<Manga>> {
return handler.subscribeToList { mangasQueries.getFavoriteBySourceId(sourceId, mangaMapper) }
return handler.subscribeToList { mangasQueries.getFavoriteBySourceId(sourceId, MangaMapper::mapManga) }
}
override suspend fun getDuplicateLibraryManga(id: Long, title: String): List<Manga> {
return handler.awaitList {
mangasQueries.getDuplicateLibraryManga(title, id, mangaMapper)
mangasQueries.getDuplicateLibraryManga(title, id, MangaMapper::mapManga)
}
}

View File

@ -1,18 +0,0 @@
package tachiyomi.data.source
import tachiyomi.domain.source.model.Source
import tachiyomi.domain.source.model.StubSource
val sourceMapper: (eu.kanade.tachiyomi.source.Source) -> Source = { source ->
Source(
id = source.id,
lang = source.lang,
name = source.name,
supportsLatest = false,
isStub = false,
)
}
val sourceDataMapper: (Long, String, String) -> StubSource = { id, lang, name ->
StubSource(id = id, lang = lang, name = name)
}

View File

@ -1,48 +1,50 @@
package tachiyomi.data.source
import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.model.FilterList
import eu.kanade.tachiyomi.source.online.HttpSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import tachiyomi.data.DatabaseHandler
import tachiyomi.domain.source.model.Source
import tachiyomi.domain.source.model.SourceWithCount
import tachiyomi.domain.source.model.StubSource
import tachiyomi.domain.source.repository.SourcePagingSourceType
import tachiyomi.domain.source.repository.SourceRepository
import tachiyomi.domain.source.service.SourceManager
import tachiyomi.domain.source.model.Source as DomainSource
class SourceRepositoryImpl(
private val sourceManager: SourceManager,
private val handler: DatabaseHandler,
) : SourceRepository {
override fun getSources(): Flow<List<Source>> {
override fun getSources(): Flow<List<DomainSource>> {
return sourceManager.catalogueSources.map { sources ->
sources.map {
sourceMapper(it).copy(
mapSourceToDomainSource(it).copy(
supportsLatest = it.supportsLatest,
)
}
}
}
override fun getOnlineSources(): Flow<List<Source>> {
override fun getOnlineSources(): Flow<List<DomainSource>> {
return sourceManager.catalogueSources.map { sources ->
sources
.filterIsInstance<HttpSource>()
.map(sourceMapper)
.map(::mapSourceToDomainSource)
}
}
override fun getSourcesWithFavoriteCount(): Flow<List<Pair<Source, Long>>> {
val sourceIdWithFavoriteCount = handler.subscribeToList { mangasQueries.getSourceIdWithFavoriteCount() }
override fun getSourcesWithFavoriteCount(): Flow<List<Pair<DomainSource, Long>>> {
val sourceIdWithFavoriteCount =
handler.subscribeToList { mangasQueries.getSourceIdWithFavoriteCount() }
return sourceIdWithFavoriteCount.map { sourceIdsWithCount ->
sourceIdsWithCount
.map { (sourceId, count) ->
val source = sourceManager.getOrStub(sourceId)
val domainSource = sourceMapper(source).copy(
val domainSource = mapSourceToDomainSource(source).copy(
isStub = source is StubSource,
)
domainSource to count
@ -51,11 +53,12 @@ class SourceRepositoryImpl(
}
override fun getSourcesWithNonLibraryManga(): Flow<List<SourceWithCount>> {
val sourceIdWithNonLibraryManga = handler.subscribeToList { mangasQueries.getSourceIdsWithNonLibraryManga() }
val sourceIdWithNonLibraryManga =
handler.subscribeToList { mangasQueries.getSourceIdsWithNonLibraryManga() }
return sourceIdWithNonLibraryManga.map { sourceId ->
sourceId.map { (sourceId, count) ->
val source = sourceManager.getOrStub(sourceId)
val domainSource = sourceMapper(source).copy(
val domainSource = mapSourceToDomainSource(source).copy(
isStub = source is StubSource,
)
SourceWithCount(domainSource, count)
@ -81,4 +84,12 @@ class SourceRepositoryImpl(
val source = sourceManager.get(sourceId) as CatalogueSource
return SourceLatestPagingSource(source)
}
private fun mapSourceToDomainSource(source: Source): DomainSource = DomainSource(
id = source.id,
lang = source.lang,
name = source.name,
supportsLatest = false,
isStub = false,
)
}

View File

@ -10,14 +10,20 @@ class StubSourceRepositoryImpl(
) : StubSourceRepository {
override fun subscribeAll(): Flow<List<StubSource>> {
return handler.subscribeToList { sourcesQueries.findAll(sourceDataMapper) }
return handler.subscribeToList { sourcesQueries.findAll(::mapStubSource) }
}
override suspend fun getStubSource(id: Long): StubSource? {
return handler.awaitOneOrNull { sourcesQueries.findOne(id, sourceDataMapper) }
return handler.awaitOneOrNull { sourcesQueries.findOne(id, ::mapStubSource) }
}
override suspend fun upsertStubSource(id: Long, lang: String, name: String) {
handler.await { sourcesQueries.upsert(id, lang, name) }
}
private fun mapStubSource(
id: Long,
lang: String,
name: String,
): StubSource = StubSource(id = id, lang = lang, name = name)
}

View File

@ -1,36 +0,0 @@
package tachiyomi.data.track
import tachiyomi.domain.track.model.Track
val trackMapper: (
Long,
Long,
Long,
Long,
Long?,
String,
Double,
Long,
Long,
Double,
String,
Long,
Long,
) -> Track =
{ id, mangaId, syncId, remoteId, libraryId, title, lastChapterRead, totalChapters, status, score, remoteUrl, startDate, finishDate ->
Track(
id = id,
mangaId = mangaId,
syncId = syncId,
remoteId = remoteId,
libraryId = libraryId,
title = title,
lastChapterRead = lastChapterRead,
totalChapters = totalChapters,
status = status,
score = score,
remoteUrl = remoteUrl,
startDate = startDate,
finishDate = finishDate,
)
}

View File

@ -10,24 +10,24 @@ class TrackRepositoryImpl(
) : TrackRepository {
override suspend fun getTrackById(id: Long): Track? {
return handler.awaitOneOrNull { manga_syncQueries.getTrackById(id, trackMapper) }
return handler.awaitOneOrNull { manga_syncQueries.getTrackById(id, ::mapTrack) }
}
override suspend fun getTracksByMangaId(mangaId: Long): List<Track> {
return handler.awaitList {
manga_syncQueries.getTracksByMangaId(mangaId, trackMapper)
manga_syncQueries.getTracksByMangaId(mangaId, ::mapTrack)
}
}
override fun getTracksAsFlow(): Flow<List<Track>> {
return handler.subscribeToList {
manga_syncQueries.getTracks(trackMapper)
manga_syncQueries.getTracks(::mapTrack)
}
}
override fun getTracksByMangaIdAsFlow(mangaId: Long): Flow<List<Track>> {
return handler.subscribeToList {
manga_syncQueries.getTracksByMangaId(mangaId, trackMapper)
manga_syncQueries.getTracksByMangaId(mangaId, ::mapTrack)
}
}
@ -68,4 +68,34 @@ class TrackRepositoryImpl(
}
}
}
private fun mapTrack(
id: Long,
mangaId: Long,
syncId: Long,
remoteId: Long,
libraryId: Long?,
title: String,
lastChapterRead: Double,
totalChapters: Long,
status: Long,
score: Double,
remoteUrl: String,
startDate: Long,
finishDate: Long,
): Track = Track(
id = id,
mangaId = mangaId,
syncId = syncId,
remoteId = remoteId,
libraryId = libraryId,
title = title,
lastChapterRead = lastChapterRead,
totalChapters = totalChapters,
status = status,
score = score,
remoteUrl = remoteUrl,
startDate = startDate,
finishDate = finishDate,
)
}

View File

@ -1,42 +0,0 @@
package tachiyomi.data.updates
import tachiyomi.domain.manga.model.MangaCover
import tachiyomi.domain.updates.model.UpdatesWithRelations
val updateWithRelationMapper: (
Long,
String,
Long,
String,
String?,
Boolean,
Boolean,
Long,
Long,
Boolean,
String?,
Long,
Long,
Long,
) -> UpdatesWithRelations = {
mangaId, mangaTitle, chapterId, chapterName, scanlator, read, bookmark, lastPageRead, sourceId, favorite, thumbnailUrl, coverLastModified, _, dateFetch ->
UpdatesWithRelations(
mangaId = mangaId,
mangaTitle = mangaTitle,
chapterId = chapterId,
chapterName = chapterName,
scanlator = scanlator,
read = read,
bookmark = bookmark,
lastPageRead = lastPageRead,
sourceId = sourceId,
dateFetch = dateFetch,
coverData = MangaCover(
mangaId = mangaId,
sourceId = sourceId,
isMangaFavorite = favorite,
url = thumbnailUrl,
lastModified = coverLastModified,
),
)
}

View File

@ -2,6 +2,7 @@ package tachiyomi.data.updates
import kotlinx.coroutines.flow.Flow
import tachiyomi.data.DatabaseHandler
import tachiyomi.domain.manga.model.MangaCover
import tachiyomi.domain.updates.model.UpdatesWithRelations
import tachiyomi.domain.updates.repository.UpdatesRepository
@ -19,14 +20,14 @@ class UpdatesRepositoryImpl(
read = read,
after = after,
limit = limit,
mapper = updateWithRelationMapper,
mapper = ::mapUpdatesWithRelations,
)
}
}
override fun subscribeAll(after: Long, limit: Long): Flow<List<UpdatesWithRelations>> {
return databaseHandler.subscribeToList {
updatesViewQueries.getRecentUpdates(after, limit, updateWithRelationMapper)
updatesViewQueries.getRecentUpdates(after, limit, ::mapUpdatesWithRelations)
}
}
@ -40,8 +41,43 @@ class UpdatesRepositoryImpl(
read = read,
after = after,
limit = limit,
mapper = updateWithRelationMapper,
mapper = ::mapUpdatesWithRelations,
)
}
}
private fun mapUpdatesWithRelations(
mangaId: Long,
mangaTitle: String,
chapterId: Long,
chapterName: String,
scanlator: String?,
read: Boolean,
bookmark: Boolean,
lastPageRead: Long,
sourceId: Long,
favorite: Boolean,
thumbnailUrl: String?,
coverLastModified: Long,
dateUpload: Long,
dateFetch: Long,
): UpdatesWithRelations = UpdatesWithRelations(
mangaId = mangaId,
mangaTitle = mangaTitle,
chapterId = chapterId,
chapterName = chapterName,
scanlator = scanlator,
read = read,
bookmark = bookmark,
lastPageRead = lastPageRead,
sourceId = sourceId,
dateFetch = dateFetch,
coverData = MangaCover(
mangaId = mangaId,
sourceId = sourceId,
isMangaFavorite = favorite,
url = thumbnailUrl,
lastModified = coverLastModified,
),
)
}