-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
685 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
query SearchQuery( | ||
$query: String, | ||
$adultContent: Boolean, | ||
$preventGenres: [String], | ||
$type: MediaType | ||
) { | ||
Page { | ||
media( | ||
search: $query, | ||
isAdult: $adultContent, | ||
genre_not_in: $preventGenres, | ||
sort: [POPULARITY_DESC], | ||
type: $type | ||
) { | ||
id, | ||
idMal, | ||
type, | ||
status(version: 2), | ||
description(asHtml: true), | ||
episodes, | ||
duration, | ||
chapters, | ||
countryOfOrigin, | ||
popularity, | ||
isFavourite, | ||
isFavouriteBlocked, | ||
isAdult, | ||
format, | ||
bannerImage, | ||
coverImage { | ||
extraLarge, | ||
large, | ||
medium, | ||
color | ||
}, | ||
averageScore, | ||
title { | ||
english, | ||
native, | ||
romaji, | ||
userPreferred | ||
}, | ||
nextAiringEpisode { | ||
episode, | ||
airingAt | ||
}, | ||
rankings { | ||
rank, | ||
allTime, | ||
year, | ||
season, | ||
type | ||
}, | ||
genres, | ||
characters(sort: [FAVOURITES_DESC,RELEVANCE]) { | ||
nodes { | ||
id, | ||
name { | ||
first | ||
middle | ||
last | ||
full | ||
native | ||
userPreferred | ||
}, | ||
image { | ||
large | ||
medium | ||
}, | ||
description(asHtml: true) | ||
gender, | ||
dateOfBirth { | ||
year | ||
month | ||
day | ||
}, | ||
bloodType, | ||
isFavourite, | ||
isFavouriteBlocked, | ||
} | ||
}, | ||
mediaListEntry { | ||
score(format: POINT_5), | ||
status, | ||
progress, | ||
repeat, | ||
startedAt { | ||
year, | ||
month, | ||
day | ||
} | ||
}, | ||
trailer { | ||
id, | ||
site, | ||
thumbnail | ||
}, | ||
siteUrl, | ||
chapters, | ||
volumes, | ||
startDate { | ||
year, | ||
month, | ||
day | ||
} | ||
} | ||
} | ||
} |
169 changes: 169 additions & 0 deletions
169
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/SearchRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package dev.datlag.aniflow.anilist | ||
|
||
import com.apollographql.apollo3.ApolloClient | ||
import com.apollographql.apollo3.api.Optional | ||
import dev.datlag.aniflow.anilist.state.CollectionState | ||
import dev.datlag.aniflow.anilist.type.MediaType | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.FlowPreview | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.flow.debounce | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.emitAll | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.mapLatest | ||
import kotlinx.coroutines.flow.transformLatest | ||
import kotlinx.coroutines.flow.update | ||
|
||
class SearchRepository( | ||
private val client: ApolloClient, | ||
private val fallbackClient: ApolloClient, | ||
private val nsfw: Flow<Boolean> = flowOf(false), | ||
private val viewManga: Flow<Boolean> = flowOf(false), | ||
) { | ||
|
||
private val search = MutableStateFlow<String?>(null) | ||
private val _type = MutableStateFlow(MediaType.UNKNOWN__) | ||
|
||
val searchQuery: String? | ||
get() = search.value?.ifBlank { null } | ||
|
||
@OptIn(FlowPreview::class) | ||
private val searchDebounced = search.debounce { | ||
if (it.isNullOrBlank()) { | ||
0 | ||
} else { | ||
100 | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
val type = _type.transformLatest { | ||
return@transformLatest if (it == MediaType.UNKNOWN__) { | ||
emitAll(viewManga.map { m -> | ||
if (m) { | ||
MediaType.MANGA | ||
} else { | ||
MediaType.ANIME | ||
} | ||
}) | ||
} else { | ||
emit(it) | ||
} | ||
}.distinctUntilChanged() | ||
|
||
private val query = combine(searchDebounced, type, nsfw.distinctUntilChanged()) { s, t, n -> | ||
if (s.isNullOrBlank()) { | ||
null | ||
} else { | ||
Query( | ||
search = s, | ||
nsfw = n, | ||
type = t | ||
) | ||
} | ||
}.distinctUntilChanged() | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
private val fallbackQuery = query.transformLatest { | ||
return@transformLatest if (it == null) { | ||
emit(it) | ||
} else { | ||
emitAll(fallbackClient.query(it.toGraphQL()).toFlow()) | ||
} | ||
}.mapLatest { | ||
if (it == null) { | ||
return@mapLatest CollectionState.None | ||
} | ||
|
||
val data = it.data | ||
if (data == null) { | ||
if (it.hasErrors()) { | ||
CollectionState.fromSearchGraphQL(data) | ||
} else { | ||
CollectionState.None | ||
} | ||
} else { | ||
CollectionState.fromSearchGraphQL(data) | ||
} | ||
}.distinctUntilChanged() | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
val result = query.transformLatest { | ||
return@transformLatest if (it == null) { | ||
emit(it) | ||
} else { | ||
emitAll(client.query(it.toGraphQL()).toFlow()) | ||
} | ||
}.mapLatest { | ||
if (it == null) { | ||
return@mapLatest CollectionState.None | ||
} | ||
|
||
val data = it.data | ||
if (data == null) { | ||
if (it.hasErrors()) { | ||
CollectionState.fromSearchGraphQL(data) | ||
} else { | ||
CollectionState.None | ||
} | ||
} else { | ||
CollectionState.fromSearchGraphQL(data) | ||
} | ||
}.transformLatest { | ||
return@transformLatest if (it.isError) { | ||
emitAll(fallbackQuery) | ||
} else { | ||
emit(it) | ||
} | ||
} | ||
|
||
fun query(query: String) { | ||
search.update { query } | ||
} | ||
|
||
fun setType(type: MediaType) { | ||
_type.update { type } | ||
} | ||
|
||
fun viewAnime() = setType(MediaType.ANIME) | ||
fun viewManga() = setType(MediaType.MANGA) | ||
|
||
fun toggleType() { | ||
_type.update { | ||
if (it == MediaType.MANGA) { | ||
MediaType.ANIME | ||
} else { | ||
MediaType.MANGA | ||
} | ||
} | ||
} | ||
|
||
private data class Query( | ||
val search: String, | ||
val nsfw: Boolean, | ||
val type: MediaType | ||
) { | ||
fun toGraphQL() = SearchQuery( | ||
query = Optional.present(search), | ||
adultContent = if (nsfw) { | ||
Optional.absent() | ||
} else { | ||
Optional.present(nsfw) | ||
}, | ||
preventGenres = if (nsfw) { | ||
Optional.absent() | ||
} else { | ||
Optional.present(AdultContent.Genre.allTags) | ||
}, | ||
type = if (type == MediaType.UNKNOWN__) { | ||
Optional.absent() | ||
} else { | ||
Optional.present(type) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.