Chapters graphql, reader is broken

This commit is contained in:
Syer10
2024-09-02 18:08:57 -04:00
parent 856aa34aad
commit 478c58a9ac
37 changed files with 791 additions and 892 deletions

View File

@@ -0,0 +1,84 @@
fragment ChapterFragment on ChapterType {
chapterNumber
fetchedAt
id
isBookmarked
isDownloaded
isRead
lastPageRead
lastReadAt
mangaId
name
pageCount
realUrl
scanlator
sourceOrder
uploadDate
url
meta {
key
value
}
}
query GetChapter($id: Int!) {
chapter(id: $id) {
...ChapterFragment
}
}
query GetMangaChapters($id: Int!) {
chapters(
condition: {mangaId: $id}
orderBy: SOURCE_ORDER,
orderByType: DESC,
) {
nodes {
...ChapterFragment
}
}
}
mutation UpdateChapter($id: Int!, $patch: UpdateChapterPatchInput!) {
updateChapter(input: {id: $id, patch: $patch}) {
clientMutationId
}
}
mutation UpdateChapters($id: [Int!]!, $patch: UpdateChapterPatchInput!) {
updateChapters(input: {ids: $id, patch: $patch}) {
clientMutationId
}
}
mutation UpdateChapterMeta($id: Int!, $key: String!, $value: String!) {
setChapterMeta(input: {meta: {chapterId: $id, key: $key, value: $value}}) {
clientMutationId
}
}
mutation DeleteDownloadedChapter($id: Int!) {
deleteDownloadedChapter(input: {id: $id}) {
clientMutationId
}
}
mutation DeleteDownloadedChapters($ids: [Int!]!) {
deleteDownloadedChapters(input: {ids: $ids}) {
clientMutationId
}
}
mutation FetchChapters($mangaId: Int!) {
fetchChapters(input: {mangaId: $mangaId}) {
chapters {
...ChapterFragment
}
}
}
mutation FetchChapterPages($id: Int!) {
fetchChapterPages(input: {chapterId: $id}) {
pages
}
}

View File

@@ -7,7 +7,9 @@
package ca.gosyer.jui.data
import ca.gosyer.jui.core.lang.addSuffix
import ca.gosyer.jui.data.chapter.ChapterRepositoryImpl
import ca.gosyer.jui.data.settings.SettingsRepositoryImpl
import ca.gosyer.jui.domain.chapter.service.ChapterRepository
import ca.gosyer.jui.domain.server.Http
import ca.gosyer.jui.domain.server.service.ServerPreferences
import ca.gosyer.jui.domain.settings.service.SettingsRepository
@@ -50,4 +52,11 @@ interface DataComponent : SharedDataComponent {
@Provides
fun settingsRepository(apolloClient: ApolloClient): SettingsRepository = SettingsRepositoryImpl(apolloClient)
@Provides
fun chapterRepository(
apolloClient: ApolloClient,
http: Http,
serverPreferences: ServerPreferences,
): ChapterRepository = ChapterRepositoryImpl(apolloClient, http, serverPreferences.serverUrl().get())
}

View File

@@ -0,0 +1,208 @@
package ca.gosyer.jui.data.chapter
import ca.gosyer.jui.data.graphql.DeleteDownloadedChapterMutation
import ca.gosyer.jui.data.graphql.DeleteDownloadedChaptersMutation
import ca.gosyer.jui.data.graphql.FetchChapterPagesMutation
import ca.gosyer.jui.data.graphql.FetchChaptersMutation
import ca.gosyer.jui.data.graphql.GetChapterQuery
import ca.gosyer.jui.data.graphql.GetMangaChaptersQuery
import ca.gosyer.jui.data.graphql.UpdateChapterMetaMutation
import ca.gosyer.jui.data.graphql.UpdateChapterMutation
import ca.gosyer.jui.data.graphql.UpdateChaptersMutation
import ca.gosyer.jui.data.graphql.fragment.ChapterFragment
import ca.gosyer.jui.data.graphql.type.UpdateChapterPatchInput
import ca.gosyer.jui.domain.chapter.model.Chapter
import ca.gosyer.jui.domain.chapter.model.ChapterMeta
import ca.gosyer.jui.domain.chapter.service.ChapterRepository
import ca.gosyer.jui.domain.server.Http
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.api.Optional
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.get
import io.ktor.client.statement.readBytes
import io.ktor.http.Url
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
class ChapterRepositoryImpl(
private val apolloClient: ApolloClient,
private val http: Http,
private val serverUrl: Url,
) : ChapterRepository {
override fun getChapter(chapterId: Long): Flow<Chapter> {
return apolloClient.query(
GetChapterQuery(chapterId.toInt())
)
.toFlow()
.map {
val data = it.dataAssertNoErrors
data.chapter.chapterFragment.toChapter()
}
}
override fun getChapters(mangaId: Long): Flow<List<Chapter>> {
return apolloClient.query(
GetMangaChaptersQuery(mangaId.toInt())
)
.toFlow()
.map {
val data = it.dataAssertNoErrors
data.chapters.nodes.map { it.chapterFragment.toChapter() }
}
}
override fun updateChapter(
chapterId: Long,
bookmarked: Boolean?,
read: Boolean?,
lastPageRead: Int?,
): Flow<Unit> {
return apolloClient.mutation(
UpdateChapterMutation(
chapterId.toInt(),
UpdateChapterPatchInput(
isBookmarked = Optional.presentIfNotNull(bookmarked),
isRead = Optional.presentIfNotNull(read),
lastPageRead = Optional.presentIfNotNull(lastPageRead)
),
)
)
.toFlow()
.map {
it.dataAssertNoErrors
}
}
override fun updateChapters(
chapterIds: List<Long>,
bookmarked: Boolean?,
read: Boolean?,
lastPageRead: Int?,
): Flow<Unit> {
return apolloClient.mutation(
UpdateChaptersMutation(
chapterIds.map { it.toInt() },
UpdateChapterPatchInput(
isBookmarked = Optional.presentIfNotNull(bookmarked),
isRead = Optional.presentIfNotNull(read),
lastPageRead = Optional.presentIfNotNull(lastPageRead)
),
)
)
.toFlow()
.map {
it.dataAssertNoErrors
}
}
override fun deleteDownloadedChapter(
chapterId: Long,
): Flow<Unit> {
return apolloClient.mutation(
DeleteDownloadedChapterMutation(
chapterId.toInt(),
)
)
.toFlow()
.map {
it.dataAssertNoErrors
}
}
override fun deleteDownloadedChapters(
chapterIds: List<Long>,
): Flow<Unit> {
return apolloClient.mutation(
DeleteDownloadedChaptersMutation(
chapterIds.map { it.toInt() },
)
)
.toFlow()
.map {
it.dataAssertNoErrors
}
}
override fun updateChapterMeta(
chapterId: Long,
key: String,
value: String,
): Flow<Unit> {
return apolloClient.mutation(
UpdateChapterMetaMutation(
chapterId.toInt(),
key,
value,
)
)
.toFlow()
.map {
it.dataAssertNoErrors
}
}
override fun fetchChapters(
mangaId: Long,
): Flow<List<Chapter>> {
return apolloClient.mutation(
FetchChaptersMutation(
mangaId.toInt(),
)
)
.toFlow()
.map {
val chapters = it.dataAssertNoErrors
chapters.fetchChapters.chapters.map { it.chapterFragment.toChapter() }
}
}
override fun getPages(chapterId: Long): Flow<List<String>> {
return apolloClient.mutation(
FetchChapterPagesMutation(
chapterId.toInt(),
)
)
.toFlow()
.map {
val chapters = it.dataAssertNoErrors
chapters.fetchChapterPages.pages
}
}
override fun getPage(
url: String,
block: HttpRequestBuilder.() -> Unit,
): Flow<ByteArray> {
val realUrl = Url("$serverUrl$url")
return flow { http.get(realUrl, block).readBytes() }
}
companion object {
internal fun ChapterFragment.toChapter(): Chapter {
return Chapter(
id = id.toLong(),
url = url,
name = name,
uploadDate = uploadDate,
chapterNumber = chapterNumber.toFloat(),
scanlator = scanlator,
mangaId = mangaId.toLong(),
read = isRead,
bookmarked = isBookmarked,
lastPageRead = lastPageRead,
index = sourceOrder,
fetchedAt = fetchedAt,
realUrl = realUrl,
pageCount = pageCount,
lastReadAt = lastPageRead,
downloaded = isDownloaded,
meta = ChapterMeta(
juiPageOffset = meta.find { it.key == "juiPageOffset" }?.value?.toIntOrNull() ?: 0
)
)
}
}
}