added pagination
This commit is contained in:
parent
e24dbfe4be
commit
b16bb21a20
20
lib/components/utils/debounce.dart
Normal file
20
lib/components/utils/debounce.dart
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
class Debounce {
|
||||||
|
factory Debounce() => _instance;
|
||||||
|
|
||||||
|
Debounce._();
|
||||||
|
|
||||||
|
static final Debounce _instance = Debounce._();
|
||||||
|
|
||||||
|
static Timer? _timer;
|
||||||
|
|
||||||
|
static void run(
|
||||||
|
VoidCallback action,
|
||||||
|
{Duration delay = const Duration(milliseconds: 500)}
|
||||||
|
) {
|
||||||
|
_timer?.cancel();
|
||||||
|
_timer = Timer(delay, action);
|
||||||
|
}
|
||||||
|
}
|
1
lib/components/utils/error_callback.dart
Normal file
1
lib/components/utils/error_callback.dart
Normal file
@ -0,0 +1 @@
|
|||||||
|
typedef OnErrorCallback = void Function(String? error);
|
@ -5,8 +5,9 @@ part 'characters_dto.g.dart';
|
|||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class CharactersDto {
|
class CharactersDto {
|
||||||
final List<CharacterDataDto>? data;
|
final List<CharacterDataDto>? data;
|
||||||
|
final MetaDto? meta;
|
||||||
|
|
||||||
const CharactersDto({this.data});
|
const CharactersDto({this.data, this.meta});
|
||||||
|
|
||||||
factory CharactersDto.fromJson(Map<String, dynamic> json) => _$CharactersDtoFromJson(json);
|
factory CharactersDto.fromJson(Map<String, dynamic> json) => _$CharactersDtoFromJson(json);
|
||||||
}
|
}
|
||||||
@ -33,3 +34,23 @@ class CharacterAttributesDataDto {
|
|||||||
|
|
||||||
factory CharacterAttributesDataDto.fromJson(Map<String, dynamic> json) => _$CharacterAttributesDataDtoFromJson(json);
|
factory CharacterAttributesDataDto.fromJson(Map<String, dynamic> json) => _$CharacterAttributesDataDtoFromJson(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class MetaDto {
|
||||||
|
final PaginationDto? pagination;
|
||||||
|
|
||||||
|
const MetaDto({this.pagination});
|
||||||
|
|
||||||
|
factory MetaDto.fromJson(Map<String, dynamic> json) => _$MetaDtoFromJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class PaginationDto {
|
||||||
|
final int? current;
|
||||||
|
final int? next;
|
||||||
|
final int? last;
|
||||||
|
|
||||||
|
const PaginationDto({this.current, this.next, this.last});
|
||||||
|
|
||||||
|
factory PaginationDto.fromJson(Map<String, dynamic> json) => _$PaginationDtoFromJson(json);
|
||||||
|
}
|
||||||
|
@ -11,6 +11,9 @@ CharactersDto _$CharactersDtoFromJson(Map<String, dynamic> json) =>
|
|||||||
data: (json['data'] as List<dynamic>?)
|
data: (json['data'] as List<dynamic>?)
|
||||||
?.map((e) => CharacterDataDto.fromJson(e as Map<String, dynamic>))
|
?.map((e) => CharacterDataDto.fromJson(e as Map<String, dynamic>))
|
||||||
.toList(),
|
.toList(),
|
||||||
|
meta: json['meta'] == null
|
||||||
|
? null
|
||||||
|
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
|
||||||
);
|
);
|
||||||
|
|
||||||
CharacterDataDto _$CharacterDataDtoFromJson(Map<String, dynamic> json) =>
|
CharacterDataDto _$CharacterDataDtoFromJson(Map<String, dynamic> json) =>
|
||||||
@ -31,3 +34,16 @@ CharacterAttributesDataDto _$CharacterAttributesDataDtoFromJson(
|
|||||||
died: json['died'] as String?,
|
died: json['died'] as String?,
|
||||||
image: json['image'] as String?,
|
image: json['image'] as String?,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
MetaDto _$MetaDtoFromJson(Map<String, dynamic> json) => MetaDto(
|
||||||
|
pagination: json['pagination'] == null
|
||||||
|
? null
|
||||||
|
: PaginationDto.fromJson(json['pagination'] as Map<String, dynamic>),
|
||||||
|
);
|
||||||
|
|
||||||
|
PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) =>
|
||||||
|
PaginationDto(
|
||||||
|
current: (json['current'] as num?)?.toInt(),
|
||||||
|
next: (json['next'] as num?)?.toInt(),
|
||||||
|
last: (json['last'] as num?)?.toInt(),
|
||||||
|
);
|
||||||
|
@ -1,11 +1,20 @@
|
|||||||
import 'package:flutter_android_app/data/dtos/characters_dto.dart';
|
import 'package:flutter_android_app/data/dtos/characters_dto.dart';
|
||||||
import 'package:flutter_android_app/domain/models/card.dart';
|
import 'package:flutter_android_app/domain/models/card.dart';
|
||||||
|
|
||||||
|
import '../../domain/models/home.dart';
|
||||||
|
|
||||||
const _imagePlaceholder = 'https://gryazoveckij-r19.gosweb.gosuslugi.ru/netcat_files/460/2008/net_foto_muzh.jpg';
|
const _imagePlaceholder = 'https://gryazoveckij-r19.gosweb.gosuslugi.ru/netcat_files/460/2008/net_foto_muzh.jpg';
|
||||||
|
|
||||||
|
extension CharactersDataDto on CharactersDto {
|
||||||
|
HomeData toDomain() => HomeData(
|
||||||
|
data: data?.map((e) => e.toDomain()).toList(),
|
||||||
|
nextPage: meta?.pagination?.next,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
extension CharacterDataDtoToModel on CharacterDataDto {
|
extension CharacterDataDtoToModel on CharacterDataDto {
|
||||||
CardData toDomain() => CardData(
|
CardData toDomain() => CardData(
|
||||||
title: attributes?.name ?? 'UKNOWNN',
|
title: attributes?.name ?? 'UNKNOWN',
|
||||||
imageUrl: attributes?.image ?? _imagePlaceholder,
|
imageUrl: attributes?.image ?? _imagePlaceholder,
|
||||||
description: _makeDescriptionText(attributes?.born, attributes?.died),
|
description: _makeDescriptionText(attributes?.born, attributes?.died),
|
||||||
);
|
);
|
||||||
|
8
lib/domain/models/home.dart
Normal file
8
lib/domain/models/home.dart
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import 'card.dart';
|
||||||
|
|
||||||
|
class HomeData {
|
||||||
|
final List<CardData>? data;
|
||||||
|
final int? nextPage;
|
||||||
|
|
||||||
|
HomeData({this.data, this.nextPage});
|
||||||
|
}
|
@ -1,5 +1,8 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_android_app/presentation/home_page/bloc/bloc.dart';
|
||||||
import 'package:flutter_android_app/presentation/home_page/home_page.dart';
|
import 'package:flutter_android_app/presentation/home_page/home_page.dart';
|
||||||
|
import 'package:flutter_android_app/repositories/potter_repository.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
@ -16,7 +19,15 @@ class MyApp extends StatelessWidget {
|
|||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
home: const MyHomePage(title: 'Harry Potter characters'),
|
home: RepositoryProvider<PotterRepository>(
|
||||||
|
lazy: true,
|
||||||
|
create: (_) => PotterRepository(),
|
||||||
|
child: BlocProvider<HomeBloc>(
|
||||||
|
lazy: false,
|
||||||
|
create: (context) => HomeBloc(context.read<PotterRepository>()),
|
||||||
|
child: const MyHomePage(title: 'Harry Potter characters'),
|
||||||
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
39
lib/presentation/home_page/bloc/bloc.dart
Normal file
39
lib/presentation/home_page/bloc/bloc.dart
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import 'package:flutter_android_app/presentation/home_page/bloc/events.dart';
|
||||||
|
import 'package:flutter_android_app/presentation/home_page/bloc/state.dart';
|
||||||
|
import 'package:flutter_android_app/repositories/potter_repository.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
|
||||||
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||||
|
final PotterRepository repo;
|
||||||
|
|
||||||
|
HomeBloc(this.repo) : super(const HomeState()) {
|
||||||
|
on<HomeLoadDataEvent>(_onLoadData);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||||||
|
if (event.nextPage == null) {
|
||||||
|
emit(state.copyWith(isLoading: true));
|
||||||
|
} else {
|
||||||
|
emit(state.copyWith(isPaginationLoading: true));
|
||||||
|
}
|
||||||
|
|
||||||
|
String? error;
|
||||||
|
|
||||||
|
final data = await repo.loadData(
|
||||||
|
q: event.search,
|
||||||
|
page: event.nextPage ?? 1,
|
||||||
|
onError: (e) => error = e,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (event.nextPage != null) {
|
||||||
|
data?.data?.insertAll(0, state.data?.data ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
emit(state.copyWith(
|
||||||
|
isLoading: false,
|
||||||
|
isPaginationLoading: false,
|
||||||
|
data: data,
|
||||||
|
error: error,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
10
lib/presentation/home_page/bloc/events.dart
Normal file
10
lib/presentation/home_page/bloc/events.dart
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
abstract class HomeEvent {
|
||||||
|
const HomeEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
class HomeLoadDataEvent extends HomeEvent {
|
||||||
|
final String? search;
|
||||||
|
final int? nextPage;
|
||||||
|
|
||||||
|
const HomeLoadDataEvent({this.search, this.nextPage});
|
||||||
|
}
|
36
lib/presentation/home_page/bloc/state.dart
Normal file
36
lib/presentation/home_page/bloc/state.dart
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import '../../../domain/models/home.dart';
|
||||||
|
|
||||||
|
class HomeState extends Equatable {
|
||||||
|
final HomeData? data;
|
||||||
|
final bool isLoading;
|
||||||
|
final bool isPaginationLoading;
|
||||||
|
final String? error;
|
||||||
|
|
||||||
|
const HomeState({
|
||||||
|
this.data,
|
||||||
|
this.isLoading = false,
|
||||||
|
this.isPaginationLoading = false,
|
||||||
|
this.error,
|
||||||
|
});
|
||||||
|
|
||||||
|
HomeState copyWith({
|
||||||
|
HomeData? data,
|
||||||
|
bool? isLoading,
|
||||||
|
bool? isPaginationLoading,
|
||||||
|
String? error,
|
||||||
|
}) => HomeState(
|
||||||
|
data: data ?? this.data,
|
||||||
|
isLoading: isLoading ?? this.isLoading,
|
||||||
|
isPaginationLoading: isPaginationLoading ?? this.isPaginationLoading,
|
||||||
|
error: error ?? this.error,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
data,
|
||||||
|
isLoading,
|
||||||
|
isPaginationLoading,
|
||||||
|
error,
|
||||||
|
];
|
||||||
|
}
|
@ -1,8 +1,12 @@
|
|||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_android_app/components/utils/debounce.dart';
|
||||||
import 'package:flutter_android_app/domain/models/card.dart';
|
import 'package:flutter_android_app/domain/models/card.dart';
|
||||||
import 'package:flutter_android_app/presentation/details_page/details_page.dart';
|
import 'package:flutter_android_app/presentation/details_page/details_page.dart';
|
||||||
import 'package:flutter_android_app/repositories/potter_repository.dart';
|
import 'package:flutter_android_app/presentation/home_page/bloc/bloc.dart';
|
||||||
|
import 'package:flutter_android_app/presentation/home_page/bloc/events.dart';
|
||||||
|
import 'package:flutter_android_app/presentation/home_page/bloc/state.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
|
||||||
part 'card.dart';
|
part 'card.dart';
|
||||||
|
|
||||||
@ -16,18 +20,6 @@ class MyHomePage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
@override
|
|
||||||
void initState() {
|
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
||||||
content: Text('Hello', style: Theme.of(context).textTheme.bodyLarge),
|
|
||||||
backgroundColor: Colors.deepPurple.shade200,
|
|
||||||
duration: const Duration(seconds: 4),
|
|
||||||
));
|
|
||||||
});
|
|
||||||
super.initState();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@ -49,8 +41,18 @@ class Body extends StatefulWidget {
|
|||||||
|
|
||||||
class _BodyState extends State<Body> {
|
class _BodyState extends State<Body> {
|
||||||
final TextEditingController searchController = TextEditingController();
|
final TextEditingController searchController = TextEditingController();
|
||||||
final repository = PotterRepository();
|
final ScrollController scrollController = ScrollController();
|
||||||
Future<List<CardData>?> data = PotterRepository().loadData();
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||||
|
});
|
||||||
|
|
||||||
|
scrollController.addListener(_onNextPageListener);
|
||||||
|
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -63,38 +65,57 @@ class _BodyState extends State<Body> {
|
|||||||
child: CupertinoSearchTextField(
|
child: CupertinoSearchTextField(
|
||||||
controller: searchController,
|
controller: searchController,
|
||||||
onChanged: (search) {
|
onChanged: (search) {
|
||||||
setState(() {
|
Debounce.run(() => context.read<HomeBloc>().add(HomeLoadDataEvent(search: search)));
|
||||||
data = repository.loadData(q: search);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Expanded(
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
child: Center(
|
builder: (context, state) => state.error != null
|
||||||
child: FutureBuilder<List<CardData>?>(
|
? Text(
|
||||||
future: data,
|
state.error ?? '',
|
||||||
builder: (context, snapshot) => SingleChildScrollView(
|
style: Theme.of(context).textTheme.headlineSmall?.copyWith(color: Colors.red),
|
||||||
child: snapshot.hasData
|
|
||||||
? Column(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
children: snapshot.data?.map((data) {
|
|
||||||
return _Card.fromData(
|
|
||||||
data,
|
|
||||||
onLike: (String title, bool isLiked) => _showSnackBar(context, title, isLiked),
|
|
||||||
onTap: () => _navToDetails(context, data),
|
|
||||||
);
|
|
||||||
}).toList() ?? [],
|
|
||||||
)
|
|
||||||
: const CircularProgressIndicator(),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
)
|
: state.isLoading
|
||||||
|
? const CircularProgressIndicator()
|
||||||
|
: Expanded(
|
||||||
|
child: RefreshIndicator(
|
||||||
|
onRefresh: _onRefresh,
|
||||||
|
child: ListView.builder(
|
||||||
|
controller: scrollController,
|
||||||
|
padding: EdgeInsets.zero,
|
||||||
|
itemCount: state.data?.data?.length ?? 0,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final data = state.data?.data?[index];
|
||||||
|
return data != null
|
||||||
|
? _Card.fromData(
|
||||||
|
data,
|
||||||
|
onLike: (title, isLiked) => _showSnackBar(context, title, isLiked),
|
||||||
|
onTap: () => _navToDetails(context, data),
|
||||||
|
)
|
||||||
|
: const SizedBox.shrink();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
),
|
||||||
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
|
builder: (context, state) => state.isPaginationLoading
|
||||||
|
? const CircularProgressIndicator()
|
||||||
|
: const SizedBox.shrink(),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
searchController.dispose();
|
||||||
|
scrollController.dispose();
|
||||||
|
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
void _showSnackBar(BuildContext context, String title, bool isLiked) {
|
void _showSnackBar(BuildContext context, String title, bool isLiked) {
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||||
@ -111,4 +132,21 @@ class _BodyState extends State<Body> {
|
|||||||
void _navToDetails(BuildContext context, CardData data) {
|
void _navToDetails(BuildContext context, CardData data) {
|
||||||
Navigator.push(context, CupertinoPageRoute(builder: (context) => DetailsPage(data)));
|
Navigator.push(context, CupertinoPageRoute(builder: (context) => DetailsPage(data)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _onRefresh() {
|
||||||
|
context.read<HomeBloc>().add(HomeLoadDataEvent(search: searchController.text));
|
||||||
|
return Future.value(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onNextPageListener() {
|
||||||
|
if (scrollController.offset >= scrollController.position.maxScrollExtent) {
|
||||||
|
final bloc = context.read<HomeBloc>();
|
||||||
|
if (!bloc.state.isPaginationLoading) {
|
||||||
|
bloc.add(HomeLoadDataEvent(
|
||||||
|
search: searchController.text,
|
||||||
|
nextPage: bloc.state.data?.nextPage,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import 'package:flutter_android_app/domain/models/card.dart';
|
import 'package:flutter_android_app/domain/models/home.dart';
|
||||||
|
|
||||||
|
import '../components/utils/error_callback.dart';
|
||||||
|
|
||||||
abstract class ApiInterface {
|
abstract class ApiInterface {
|
||||||
Future<List<CardData>?> loadData({String? q});
|
Future<HomeData?> loadData({OnErrorCallback? onError});
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,13 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter_android_app/domain/models/card.dart';
|
import 'package:flutter_android_app/domain/models/card.dart';
|
||||||
import 'package:flutter_android_app/repositories/api_interface.dart';
|
import 'package:flutter_android_app/repositories/api_interface.dart';
|
||||||
|
|
||||||
|
import '../components/utils/error_callback.dart';
|
||||||
|
import '../domain/models/home.dart';
|
||||||
|
|
||||||
class MockRepository extends ApiInterface {
|
class MockRepository extends ApiInterface {
|
||||||
@override
|
@override
|
||||||
Future<List<CardData>?> loadData({String? q}) async {
|
Future<HomeData?> loadData({OnErrorCallback? onError}) async {
|
||||||
return [
|
return HomeData(data: [
|
||||||
CardData(
|
CardData(
|
||||||
title: 'Title 1',
|
title: 'Title 1',
|
||||||
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
|
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
|
||||||
@ -22,6 +25,6 @@ class MockRepository extends ApiInterface {
|
|||||||
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor',
|
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor',
|
||||||
imageUrl: 'https://i.imgur.com/m2FhVAK.png',
|
imageUrl: 'https://i.imgur.com/m2FhVAK.png',
|
||||||
),
|
),
|
||||||
];
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:flutter_android_app/data/dtos/characters_dto.dart';
|
import 'package:flutter_android_app/data/dtos/characters_dto.dart';
|
||||||
import 'package:flutter_android_app/data/mappers/characters_mapper.dart';
|
import 'package:flutter_android_app/data/mappers/characters_mapper.dart';
|
||||||
import 'package:flutter_android_app/domain/models/card.dart';
|
import 'package:flutter_android_app/domain/models/home.dart';
|
||||||
import 'package:flutter_android_app/repositories/api_interface.dart';
|
import 'package:flutter_android_app/repositories/api_interface.dart';
|
||||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||||
|
|
||||||
|
import '../components/utils/error_callback.dart';
|
||||||
|
|
||||||
class PotterRepository extends ApiInterface {
|
class PotterRepository extends ApiInterface {
|
||||||
static final Dio _dio = Dio()
|
static final Dio _dio = Dio()
|
||||||
..interceptors.add(PrettyDioLogger(
|
..interceptors.add(PrettyDioLogger(
|
||||||
@ -15,19 +17,28 @@ class PotterRepository extends ApiInterface {
|
|||||||
static const String _baseUrl = 'https://api.potterdb.com';
|
static const String _baseUrl = 'https://api.potterdb.com';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<CardData>?> loadData({String? q}) async {
|
Future<HomeData?> loadData({
|
||||||
|
OnErrorCallback? onError,
|
||||||
|
String? q, int page = 1,
|
||||||
|
int pageSize = 20,
|
||||||
|
}) async {
|
||||||
try {
|
try {
|
||||||
const String url = '$_baseUrl/v1/characters';
|
const String url = '$_baseUrl/v1/characters';
|
||||||
|
|
||||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||||||
url,
|
url,
|
||||||
queryParameters: q != null ? {'filter[name_cont]': q} : null,
|
queryParameters: {
|
||||||
|
'filter[name_cont]': q,
|
||||||
|
'page[number]': page,
|
||||||
|
'page[size]': pageSize,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
final CharactersDto dto = CharactersDto.fromJson(response.data as Map<String, dynamic>);
|
final CharactersDto dto = CharactersDto.fromJson(response.data as Map<String, dynamic>);
|
||||||
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
|
final HomeData data = dto.toDomain();
|
||||||
return data;
|
return data;
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
|
onError?.call(e.error?.toString());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
40
pubspec.lock
40
pubspec.lock
@ -38,6 +38,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.11.0"
|
version: "2.11.0"
|
||||||
|
bloc:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: bloc
|
||||||
|
sha256: "106842ad6569f0b60297619e9e0b1885c2fb9bf84812935490e6c5275777804e"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "8.1.4"
|
||||||
boolean_selector:
|
boolean_selector:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -198,6 +206,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.0"
|
||||||
|
equatable:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: equatable
|
||||||
|
sha256: "567c64b3cb4cf82397aac55f4f0cbd3ca20d77c6c03bedbc4ceaddc08904aef7"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.7"
|
||||||
fake_async:
|
fake_async:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -227,6 +243,14 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
flutter_bloc:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: flutter_bloc
|
||||||
|
sha256: b594505eac31a0518bdcb4b5b79573b8d9117b193cc80cc12e17d639b10aa27a
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "8.1.6"
|
||||||
flutter_lints:
|
flutter_lints:
|
||||||
dependency: "direct dev"
|
dependency: "direct dev"
|
||||||
description:
|
description:
|
||||||
@ -392,6 +416,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.0"
|
||||||
|
nested:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: nested
|
||||||
|
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.0"
|
||||||
package_config:
|
package_config:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -424,6 +456,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.4.0"
|
version: "1.4.0"
|
||||||
|
provider:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: provider
|
||||||
|
sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "6.1.2"
|
||||||
pub_semver:
|
pub_semver:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -39,6 +39,8 @@ dependencies:
|
|||||||
json_annotation: ^4.9.0
|
json_annotation: ^4.9.0
|
||||||
dio: ^5.7.0
|
dio: ^5.7.0
|
||||||
pretty_dio_logger: ^1.4.0
|
pretty_dio_logger: ^1.4.0
|
||||||
|
flutter_bloc: ^8.1.6
|
||||||
|
equatable: ^2.0.7
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
Loading…
Reference in New Issue
Block a user