Skip to content

Commit

Permalink
mangadex: add option to use source language for title (#6471)
Browse files Browse the repository at this point in the history
* mangadex: add option to use source language for title

closes #5747

* use 'extension' instead of 'source' for variable names & translations

* simplify title selection
  • Loading branch information
Kladki authored Dec 9, 2024
1 parent f665140 commit 8a51465
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/all/mangadex/assets/i18n/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ original_language=Original language
original_language_filter_chinese=%s (Manhua)
original_language_filter_japanese=%s (Manga)
original_language_filter_korean=%s (Manhwa)
prefer_title_in_extension_language=Use Alternate Titles
prefer_title_in_extension_language_summary=If there is an alternate title available which matches the extension language, it will be used
publication_demographic=Publication demographic
publication_demographic_josei=Josei
publication_demographic_none=None
Expand Down
2 changes: 1 addition & 1 deletion src/all/mangadex/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
extName = 'MangaDex'
extClass = '.MangaDexFactory'
extVersionCode = 195
extVersionCode = 196
isNsfw = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ object MDConstants {
return "${altTitlesInDescPref}_$dexLang"
}

private const val preferExtensionLangTitlePref = "preferExtensionLangTitle"
fun getPreferExtensionLangTitlePrefKey(dexLang: String): String {
return "${preferExtensionLangTitlePref}_$dexLang"
}

private const val tagGroupContent = "content"
private const val tagGroupFormat = "format"
private const val tagGroupGenre = "genre"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
.firstInstanceOrNull<CoverArtDto>()
?.attributes?.fileName
}
helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang)
helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang, preferences.preferExtensionLangTitle)
}

return MangasPage(mangaList, mangaListDto.hasNextPage)
Expand Down Expand Up @@ -177,7 +177,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
.firstInstanceOrNull<CoverArtDto>()
?.attributes?.fileName
}
helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang)
helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang, preferences.preferExtensionLangTitle)
}

return MangasPage(mangaList, chapterListDto.hasNextPage)
Expand Down Expand Up @@ -360,7 +360,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
.firstInstanceOrNull<CoverArtDto>()
?.attributes?.fileName
}
helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang)
helper.createBasicManga(mangaDataDto, fileName, coverSuffix, dexLang, preferences.preferExtensionLangTitle)
}

return mangaList
Expand Down Expand Up @@ -423,6 +423,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
dexLang,
preferences.coverQuality,
preferences.altTitlesInDesc,
preferences.preferExtensionLangTitle,
)
}

Expand Down Expand Up @@ -757,11 +758,27 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
}
}

val preferExtensionLangTitlePref = SwitchPreferenceCompat(screen.context).apply {
key = MDConstants.getPreferExtensionLangTitlePrefKey(dexLang)
title = helper.intl["prefer_title_in_extension_language"]
summary = helper.intl["prefer_title_in_extension_language_summary"]
setDefaultValue(true)

setOnPreferenceChangeListener { _, newValue ->
val checkValue = newValue as Boolean

preferences.edit()
.putBoolean(MDConstants.getPreferExtensionLangTitlePrefKey(dexLang), checkValue)
.commit()
}
}

screen.addPreference(coverQualityPref)
screen.addPreference(tryUsingFirstVolumeCoverPref)
screen.addPreference(dataSaverPref)
screen.addPreference(standardHttpsPortPref)
screen.addPreference(altTitlesInDescPref)
screen.addPreference(preferExtensionLangTitlePref)
screen.addPreference(contentRatingPref)
screen.addPreference(originalLanguagePref)
screen.addPreference(blockedGroupsPref)
Expand Down Expand Up @@ -840,6 +857,9 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
private val SharedPreferences.altTitlesInDesc
get() = getBoolean(MDConstants.getAltTitlesInDescPrefKey(dexLang), false)

private val SharedPreferences.preferExtensionLangTitle
get() = getBoolean(MDConstants.getPreferExtensionLangTitlePrefKey(dexLang), true)

/**
* Previous versions of the extension allowed invalid UUID values to be stored in the
* preferences. This method clear invalid UUIDs in case the user have updated from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ class MangaDexHelper(lang: String) {
return GET(tokenRequestUrl, headers, cacheControl)
}

private fun List<Map<String, String>>.findTitleByLang(lang: String): String? =
firstOrNull { it[lang] != null }?.values?.singleOrNull()

/**
* Create a [SManga] from the JSON element with only basic attributes filled.
*/
Expand All @@ -283,15 +286,24 @@ class MangaDexHelper(lang: String) {
coverFileName: String?,
coverSuffix: String?,
lang: String,
preferExtensionLangTitle: Boolean,
): SManga = SManga.create().apply {
url = "/manga/${mangaDataDto.id}"

val titleMap = mangaDataDto.attributes!!.title
val dirtyTitle =
titleMap.values.firstOrNull() // use literally anything from title as first resort
?: mangaDataDto.attributes.altTitles
.find { (it[lang] ?: it["en"]) !== null }
?.values?.singleOrNull() // find something else from alt titles
title = dirtyTitle?.removeEntities().orEmpty()
title = with(mangaDataDto.attributes) {
titleMap[lang] ?: altTitles.run {
val mainTitle = titleMap.values.firstOrNull()
val langTitle = findTitleByLang(lang)
val enTitle = findTitleByLang("en")

if (preferExtensionLangTitle) {
listOf(langTitle, mainTitle, enTitle)
} else {
listOf(mainTitle, langTitle, enTitle)
}.firstNotNullOfOrNull { it }
}
}?.removeEntities().orEmpty()

coverFileName?.let {
thumbnail_url = when (!coverSuffix.isNullOrEmpty()) {
Expand All @@ -311,6 +323,7 @@ class MangaDexHelper(lang: String) {
lang: String,
coverSuffix: String?,
altTitlesInDesc: Boolean,
preferExtensionLangTitle: Boolean,
): SManga {
val attr = mangaDataDto.attributes!!

Expand Down Expand Up @@ -370,7 +383,7 @@ class MangaDexHelper(lang: String) {
}
}

return createBasicManga(mangaDataDto, coverFileName, coverSuffix, lang).apply {
return createBasicManga(mangaDataDto, coverFileName, coverSuffix, lang, preferExtensionLangTitle).apply {
description = desc
author = authors.joinToString()
artist = artists.joinToString()
Expand Down

0 comments on commit 8a51465

Please sign in to comment.