Skip to content

Commit

Permalink
Add year, localeMangas filter on MangaListFilter.Advanced
Browse files Browse the repository at this point in the history
Add year, localeMangas and localeMangas on MangaDexParser
  • Loading branch information
davvarrr committed Sep 10, 2024
1 parent 3df31ce commit 0ae9d88
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ abstract class MangaParser @InternalParsersApi constructor(
*/
open val searchSupportedWithMultipleFilters: Boolean = false

/**
* Whether parser supports searching by year
*/

open val isSearchYearSupported: Boolean = false

/**
* Whether parser supports searching Original Languages
*/
open val isSearchOriginalLanguages: Boolean = false


@Deprecated(
message = "Use availableSortOrders instead",
replaceWith = ReplaceWith("availableSortOrders"),
Expand Down Expand Up @@ -166,9 +178,11 @@ abstract class MangaParser @InternalParsersApi constructor(
tags = tags.orEmpty(),
tagsExclude = tagsExclude.orEmpty(),
locale = null,
localeMangas = null,
states = emptySet(),
contentRating = emptySet(),
query = null,
year = null,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ sealed interface MangaListFilter {
(tagsExclude.isEmpty() || parser.isTagsExclusionSupported) &&
(contentRating.isEmpty() || parser.availableContentRating.containsAll(contentRating)) &&
(states.isEmpty() || parser.availableStates.containsAll(states) &&
(query.isNullOrEmpty() || parser.searchSupportedWithMultipleFilters))
(parser.searchSupportedWithMultipleFilters) &&
(parser.isSearchYearSupported) && (parser.isSearchOriginalLanguages))

is Search -> parser.isSearchSupported
}
Expand All @@ -34,31 +35,37 @@ sealed interface MangaListFilter {
@JvmField val tags: Set<MangaTag>,
@JvmField val tagsExclude: Set<MangaTag>,
@JvmField val locale: Locale?,
@JvmField val localeMangas: Locale?,
@JvmField val states: Set<MangaState>,
@JvmField val contentRating: Set<ContentRating>,
@JvmField val query: String?,
@JvmField val year: Int?,
) : MangaListFilter {

override fun isEmpty(): Boolean =
tags.isEmpty() && tagsExclude.isEmpty() && locale == null && states.isEmpty() && contentRating.isEmpty() && query == null
tags.isEmpty() && tagsExclude.isEmpty() && locale == null && localeMangas == null && states.isEmpty() && contentRating.isEmpty() && query == null && year == null

fun newBuilder() = Builder(sortOrder)
.tags(tags)
.tagsExclude(tagsExclude)
.locale(locale)
.localeMangas(localeMangas)
.states(states)
.contentRatings(contentRating)
.query(query)
.year(year)

class Builder(sortOrder: SortOrder) {

private var _sortOrder: SortOrder = sortOrder
private var _tags: Set<MangaTag>? = null
private var _tagsExclude: Set<MangaTag>? = null
private var _locale: Locale? = null
private var _localeMangas: Locale? = null
private var _states: Set<MangaState>? = null
private var _contentRating: Set<ContentRating>? = null
private var _query: String? = null
private var _year: Int? = null

fun sortOrder(order: SortOrder) = apply {
_sortOrder = order
Expand All @@ -76,6 +83,10 @@ sealed interface MangaListFilter {
_locale = locale
}

fun localeMangas(localeMangas: Locale?) = apply {
_localeMangas = localeMangas
}

fun states(states: Set<MangaState>?) = apply {
_states = states
}
Expand All @@ -84,18 +95,26 @@ sealed interface MangaListFilter {
_contentRating = rating
}

fun query(query: String?) {
fun query(query: String?) = apply {
_query = query
}

fun year(year: Int?) = apply {
_year = year
}



fun build() = Advanced(
sortOrder = _sortOrder,
tags = _tags.orEmpty(),
tagsExclude = _tagsExclude.orEmpty(),
locale = _locale,
localeMangas = _localeMangas,
states = _states.orEmpty(),
contentRating = _contentRating.orEmpty(),
query = _query,
year = _year,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ internal class MangaDexParser(context: MangaLoaderContext) : MangaParser(context
EnumSet.of(MangaState.ONGOING, MangaState.FINISHED, MangaState.PAUSED, MangaState.ABANDONED)

override val isTagsExclusionSupported: Boolean = true

override val searchSupportedWithMultipleFilters: Boolean = true
override val isSearchYearSupported: Boolean = true
override val isSearchOriginalLanguages: Boolean = true

override suspend fun getList(offset: Int, filter: MangaListFilter?): List<Manga> {
val domain = domain
Expand Down Expand Up @@ -83,9 +85,10 @@ internal class MangaDexParser(context: MangaLoaderContext) : MangaParser(context
ContentRating.SAFE -> append("&contentRating[]=safe")
ContentRating.SUGGESTIVE -> append("&contentRating[]=suggestive&contentRating[]=erotica")
ContentRating.ADULT -> append("&contentRating[]=pornographic")

}
}
}
} else append("&contentRating[]=safe&contentRating[]=suggestive&contentRating[]=erotica&contentRating[]=pornographic")

append("&order")
append(
Expand Down Expand Up @@ -116,6 +119,16 @@ internal class MangaDexParser(context: MangaLoaderContext) : MangaParser(context
append("&availableTranslatedLanguage[]=")
append(it.language)
}

filter.localeMangas?.let {
append("&originalLanguage[]=")
append(it.language)
}

filter.year?.let {
append("&year=")
append(filter.year)
}
}

null -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ internal class MangaParserTest {
sortOrder = SortOrder.POPULARITY,
tags = emptySet(),
locale = null,
localeMangas = null,
states = emptySet(),
tagsExclude = emptySet(),
contentRating = emptySet(),
query = null,
year = null,
),
).minByOrNull {
it.title.length
Expand Down Expand Up @@ -132,9 +134,11 @@ internal class MangaParserTest {
tags = setOf(),
tagsExclude = setOf(),
locale = locales.random(),
localeMangas = locales.random(),
states = setOf(),
contentRating = setOf(),
query = null,
year = null,
)
val list = parser.getList(offset = 0, filter)
checkMangaList(list, filter.locale.toString())
Expand Down

0 comments on commit 0ae9d88

Please sign in to comment.