2024-10-16 14:37:36 +04:00
|
|
|
import 'package:flutter_app/components/utils/error_callback.dart';
|
2024-10-16 12:08:15 +04:00
|
|
|
import 'package:flutter_app/data/dtos/mangas_dto.dart';
|
|
|
|
import 'package:flutter_app/data/mappers/mangas_mapper.dart';
|
|
|
|
import 'package:flutter_app/data/repositories/api_interface.dart';
|
|
|
|
import 'package:dio/dio.dart';
|
2024-10-16 14:37:36 +04:00
|
|
|
import 'package:flutter_app/presentation/home_page/home_page.dart';
|
2024-10-16 12:08:15 +04:00
|
|
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
|
|
|
|
|
|
|
class MangaRepository extends ApiInterface {
|
|
|
|
static final Dio _dio = Dio()
|
|
|
|
..interceptors.add(PrettyDioLogger(
|
|
|
|
requestHeader: true,
|
|
|
|
requestBody: true,
|
|
|
|
));
|
|
|
|
static const String _baseUrl = 'https://api.jikan.moe';
|
|
|
|
|
|
|
|
@override
|
2024-10-16 14:37:36 +04:00
|
|
|
Future<HomeData?> loadData({
|
|
|
|
OnErrorCallback? onError,
|
|
|
|
String? q,
|
|
|
|
int page = 1,
|
|
|
|
int pageSize = 25,
|
|
|
|
}) async {
|
2024-10-16 12:08:15 +04:00
|
|
|
try {
|
|
|
|
const String url = '$_baseUrl/v4/manga';
|
|
|
|
|
2024-10-16 14:37:36 +04:00
|
|
|
final Response<dynamic> response = await _dio
|
|
|
|
.get<Map<dynamic, dynamic>>(url, queryParameters: {
|
|
|
|
'q': q,
|
|
|
|
'page': page,
|
|
|
|
'limit': !(pageSize > 25) ? pageSize : 25
|
|
|
|
});
|
|
|
|
|
2024-10-16 12:08:15 +04:00
|
|
|
final MangasDto dto =
|
|
|
|
MangasDto.fromJson(response.data as Map<String, dynamic>);
|
2024-10-16 14:37:36 +04:00
|
|
|
final HomeData? data = dto.toDomain();
|
2024-10-16 12:08:15 +04:00
|
|
|
return data;
|
|
|
|
} on DioException catch (e) {
|
2024-10-16 14:37:36 +04:00
|
|
|
onError?.call(e.response?.statusMessage);
|
2024-10-16 12:08:15 +04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|