Skip to content

Commit

Permalink
feat: added advanced search beta
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanYuuki committed Nov 25, 2024
1 parent be2692d commit 768a3aa
Showing 1 changed file with 68 additions and 10 deletions.
78 changes: 68 additions & 10 deletions lib/utils/apiHooks/anilist/anime/search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,41 @@ import 'dart:convert';
import 'dart:developer';
import 'package:http/http.dart' as http;

Future<List<Map<String, dynamic>>> fetchAnimeBySearch(String query) async {
Future<List<Map<String, dynamic>>> fetchAnimeBySearch({
String? query, // Made optional
String? sort, // e.g., 'SCORE_DESC'
String? season, // e.g., 'WINTER'
String? status, // e.g., 'FINISHED'
String? format, // e.g., 'TV'
List<String>? genres, // e.g., ['Action', 'Adventure']
}) async {
const url = 'https://graphql.anilist.co/';
final headers = {'Content-Type': 'application/json'};

final body = jsonEncode({
'query': '''
query (\$search: String) {
final Map<String, dynamic> variables = {
if (query != null && query.isNotEmpty)
'search': query, // Include only if query is not empty
if (sort != null) 'sort': [sort],
if (season != null) 'season': season.toUpperCase(),
if (status != null) 'status': status.toUpperCase(),
if (format != null) 'format': format.replaceAll(' ', '_').toUpperCase(),
if (genres != null && genres.isNotEmpty) 'genre_in': genres,
};
dynamic body;
if (query != null && query.isNotEmpty) {
body = jsonEncode({
'query': '''
query (\$search: String, \$sort: [MediaSort], \$season: MediaSeason, \$status: MediaStatus, \$format: MediaFormat, \$genre_in: [String]) {
Page (page: 1) {
media (search: \$search, type: ANIME) {
media (
${query != null && query.isNotEmpty ? 'search: \$search,' : ''}
type: ANIME,
sort: \$sort,
season: \$season,
status: \$status,
format: \$format,
genre_in: \$genre_in
) {
id
title {
english
Expand All @@ -27,8 +53,40 @@ Future<List<Map<String, dynamic>>> fetchAnimeBySearch(String query) async {
}
}
''',
'variables': {'search': query}
});
'variables': variables,
});
} else {
body = jsonEncode({
'query': '''
query (\$sort: [MediaSort], \$season: MediaSeason, \$status: MediaStatus, \$format: MediaFormat, \$genre_in: [String]) {
Page (page: 1) {
media (
type: ANIME,
sort: \$sort,
season: \$season,
status: \$status,
format: \$format,
genre_in: \$genre_in
) {
id
title {
english
romaji
native
}
episodes
coverImage {
large
}
type
averageScore
}
}
}
''',
'variables': variables,
});
}

try {
final response =
Expand All @@ -46,13 +104,13 @@ Future<List<Map<String, dynamic>>> fetchAnimeBySearch(String query) async {
'poster': anime['coverImage']['large'] ?? '',
'episodes': anime['episodes'] ?? 0,
'type': anime['type'] ?? '',
'rating': ((anime['averageScore'] ?? 0) / 10)?.toString() ?? '0.0',
'rating':
((anime['averageScore'] ?? 0) / 10).toStringAsFixed(1) ?? '0.0',
};
}).toList();

return mappedData;
} else {
log('Failed to fetch anime data. Status code: ${response.statusCode}');
log('Failed to fetch anime data. Status code: ${response.statusCode} \n response body : ${response.body}');
return [];
}
} catch (e) {
Expand Down

0 comments on commit 768a3aa

Please sign in to comment.