Skip to content

Commit

Permalink
Respect thumbnailQuality and tryUsingFirstVolumeCover preferences…
Browse files Browse the repository at this point in the history
…. (MD) (#1260)
  • Loading branch information
NGB-Was-Taken authored Sep 1, 2024
1 parent fbac29e commit bdb8553
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class MangaDex(delegate: HttpSource, val context: Context) :
private fun usePort443Only() = sourcePreferences.getBoolean(getStandardHttpsPreferenceKey(mdLang.lang), false)
private fun blockedGroups() = sourcePreferences.getString(getBlockedGroupsPrefKey(mdLang.lang), "").orEmpty()
private fun blockedUploaders() = sourcePreferences.getString(getBlockedUploaderPrefKey(mdLang.lang), "").orEmpty()
private fun coverQuality() = sourcePreferences.getString(getCoverQualityPrefKey(mdLang.lang), "").orEmpty()
private fun tryUsingFirstVolumeCover() = sourcePreferences.getBoolean(getTryUsingFirstVolumeCoverKey(mdLang.lang), false)

private val mangadexService by lazy {
MangaDexService(client)
Expand Down Expand Up @@ -189,11 +191,11 @@ class MangaDex(delegate: HttpSource, val context: Context) :

@Deprecated("Use the 1.x API instead", replaceWith = ReplaceWith("getMangaDetails"))
override fun fetchMangaDetails(manga: SManga): Observable<SManga> {
return mangaHandler.fetchMangaDetailsObservable(manga, id)
return mangaHandler.fetchMangaDetailsObservable(manga, id, coverQuality(), tryUsingFirstVolumeCover())
}

override suspend fun getMangaDetails(manga: SManga): SManga {
return mangaHandler.getMangaDetails(manga, id)
return mangaHandler.getMangaDetails(manga, id, coverQuality(), tryUsingFirstVolumeCover())
}

@Deprecated("Use the 1.x API instead", replaceWith = ReplaceWith("getChapterList"))
Expand Down Expand Up @@ -239,7 +241,7 @@ class MangaDex(delegate: HttpSource, val context: Context) :
override fun newMetaInstance() = MangaDexSearchMetadata()

override suspend fun parseIntoMetadata(metadata: MangaDexSearchMetadata, input: Triple<MangaDto, List<String>, StatisticsMangaDto>) {
apiMangaParser.parseIntoMetadata(metadata, input.first, input.second, input.third)
apiMangaParser.parseIntoMetadata(metadata, input.first, input.second, input.third, null, coverQuality())
}

// LoginSource methods
Expand Down Expand Up @@ -334,5 +336,17 @@ class MangaDex(delegate: HttpSource, val context: Context) :
fun getBlockedUploaderPrefKey(dexLang: String): String {
return "${blockedUploaderPref}_$dexLang"
}

private const val coverQualityPref = "thumbnailQuality"

fun getCoverQualityPrefKey(dexLang: String): String {
return "${coverQualityPref}_$dexLang"
}

private const val tryUsingFirstVolumeCover = "tryUsingFirstVolumeCover"

fun getTryUsingFirstVolumeCoverKey(dexLang: String): String {
return "${tryUsingFirstVolumeCover}_$dexLang"
}
}
}
8 changes: 2 additions & 6 deletions app/src/main/java/exh/md/dto/MangaDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,12 @@ data class ReadChapterDto(

@Serializable
data class CoverListDto(
val results: List<CoverDto>,
val data: List<CoverDto>,
)

@Serializable
data class CoverDto(
val data: CoverDataDto,
)

@Serializable
data class CoverDataDto(
val id: String,
val attributes: CoverAttributesDto,
val relationships: List<RelationshipDto>,
)
Expand Down
24 changes: 16 additions & 8 deletions app/src/main/java/exh/md/handlers/ApiMangaParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class ApiMangaParser(
input: MangaDto,
simpleChapters: List<String>,
statistics: StatisticsMangaDto?,
coverFileName: String?,
coverQuality: String,
): SManga {
val mangaId = getManga.await(manga.url, sourceId)?.id
val metadata = if (mangaId != null) {
Expand All @@ -50,7 +52,7 @@ class ApiMangaParser(
newMetaInstance()
}

parseIntoMetadata(metadata, input, simpleChapters, statistics)
parseIntoMetadata(metadata, input, simpleChapters, statistics, coverFileName, coverQuality)
if (mangaId != null) {
metadata.mangaId = mangaId
insertFlatMetadata.await(metadata.flatten())
Expand All @@ -64,6 +66,8 @@ class ApiMangaParser(
mangaDto: MangaDto,
simpleChapters: List<String>,
statistics: StatisticsMangaDto?,
coverFileName: String?,
coverQuality: String,
) {
with(metadata) {
try {
Expand All @@ -73,13 +77,17 @@ class ApiMangaParser(
altTitles = mangaAttributesDto.altTitles.mapNotNull { it[lang] }.nullIfEmpty()

val mangaRelationshipsDto = mangaDto.data.relationships
mangaRelationshipsDto
.firstOrNull { relationshipDto -> relationshipDto.type == MdConstants.Types.coverArt }
?.attributes
?.fileName
?.let { coverFileName ->
cover = MdUtil.cdnCoverUrl(mangaDto.data.id, coverFileName)
}
cover = if (!coverFileName.isNullOrEmpty()) {
MdUtil.cdnCoverUrl(mangaDto.data.id, "$coverFileName$coverQuality")
} else {
mangaRelationshipsDto
.firstOrNull { relationshipDto -> relationshipDto.type == MdConstants.Types.coverArt }
?.attributes
?.fileName
?.let { coverFileName ->
MdUtil.cdnCoverUrl(mangaDto.data.id, "$coverFileName$coverQuality")
}
}

description = MdUtil.cleanDescription(
MdUtil.getFromLangMap(
Expand Down
23 changes: 19 additions & 4 deletions app/src/main/java/exh/md/handlers/MangaHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ class MangaHandler(
private val apiMangaParser: ApiMangaParser,
private val followsHandler: FollowsHandler,
) {
suspend fun getMangaDetails(manga: SManga, sourceId: Long): SManga {
suspend fun getMangaDetails(
manga: SManga,
sourceId: Long,
coverQuality: String,
tryUsingFirstVolumeCover: Boolean,
): SManga {
return coroutineScope {
val mangaId = MdUtil.getMangaId(manga.url)
val response = async(Dispatchers.IO) { service.viewManga(mangaId) }
Expand All @@ -32,19 +37,29 @@ class MangaHandler(
async(Dispatchers.IO) {
kotlin.runCatching { service.mangasRating(mangaId) }.getOrNull()?.statistics?.get(mangaId)
}
val responseData = response.await()
val coverFileName = if (tryUsingFirstVolumeCover) {
async(Dispatchers.IO) {
service.fetchFirstVolumeCover(responseData)
}
} else {
null
}
apiMangaParser.parseToManga(
manga,
sourceId,
response.await(),
responseData,
simpleChapters.await(),
statistics.await(),
coverFileName?.await(),
coverQuality,
)
}
}

fun fetchMangaDetailsObservable(manga: SManga, sourceId: Long): Observable<SManga> {
fun fetchMangaDetailsObservable(manga: SManga, sourceId: Long, coverQuality: String, tryUsingFirstVolumeCover: Boolean): Observable<SManga> {
return runAsObservable {
getMangaDetails(manga, sourceId)
getMangaDetails(manga, sourceId, coverQuality, tryUsingFirstVolumeCover)
}
}

Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/exh/md/service/MangaDexService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import exh.md.dto.AtHomeDto
import exh.md.dto.AtHomeImageReportDto
import exh.md.dto.ChapterDto
import exh.md.dto.ChapterListDto
import exh.md.dto.CoverListDto
import exh.md.dto.MangaDto
import exh.md.dto.MangaListDto
import exh.md.dto.RelationListDto
Expand Down Expand Up @@ -209,4 +210,23 @@ class MangaDexService(
).awaitSuccess().parseAs()
}
}

suspend fun fetchFirstVolumeCover(mangaDto: MangaDto): String? {
val mangaData = mangaDto.data
val result: CoverListDto = with(MdUtil.jsonParser) {
client.newCall(
GET(
MdApi.cover.toHttpUrl().newBuilder()
.apply {
addQueryParameter("order[volume]", "asc")
addQueryParameter("manga[]", mangaData.id)
addQueryParameter("locales[]", mangaData.attributes.originalLanguage)
addQueryParameter("limit", "1")
}
.build(),
),
).awaitSuccess().parseAs()
}
return result.data.firstOrNull()?.attributes?.fileName
}
}
1 change: 1 addition & 0 deletions app/src/main/java/exh/md/utils/MdApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exh.md.utils
object MdApi {
const val baseUrl = "https://api.mangadex.org"
const val manga = "$baseUrl/manga"
const val cover = "$baseUrl/cover"
const val chapter = "$baseUrl/chapter"
const val group = "$baseUrl/group"
const val author = "$baseUrl/author"
Expand Down

0 comments on commit bdb8553

Please sign in to comment.