починила пагинацию

This commit is contained in:
Полина Чубыкина 2024-11-27 22:35:25 +04:00
parent a631adb572
commit 5fd2a61912
4 changed files with 41 additions and 56 deletions

View File

@ -5,11 +5,17 @@ part 'pokemon_dto.g.dart';
@JsonSerializable(createToJson: false)
class PokemonDto {
final List<PokemonDataDto>? data;
final MetaDto? meta;
final int? page;
final int? pageSize;
final int? count;
final int? totalCount;
const PokemonDto({
this.data,
this.meta,
this.page,
this.pageSize,
this.count,
this.totalCount,
});
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
@ -56,24 +62,4 @@ class PokemonImagesDto {
const PokemonImagesDto({this.large});
factory PokemonImagesDto.fromJson(Map<String, dynamic> json) => _$PokemonImagesDtoFromJson(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);
}

View File

@ -10,9 +10,10 @@ PokemonDto _$PokemonDtoFromJson(Map<String, dynamic> json) => PokemonDto(
data: (json['data'] as List<dynamic>?)
?.map((e) => PokemonDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
meta: json['meta'] == null
? null
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
page: (json['page'] as num?)?.toInt(),
pageSize: (json['pageSize'] as num?)?.toInt(),
count: (json['count'] as num?)?.toInt(),
totalCount: (json['totalCount'] as num?)?.toInt(),
);
PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
@ -44,16 +45,3 @@ PokemonImagesDto _$PokemonImagesDtoFromJson(Map<String, dynamic> json) =>
PokemonImagesDto(
large: json['large'] 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(),
);

View File

@ -7,10 +7,16 @@ const _imagePlaceholder =
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
extension PokemonDtoToModel on PokemonDto {
HomeData toDomain() => HomeData(
data: data?.map((e) => e.toDomain()).toList(),
nextPage: meta?.pagination?.next,
);
HomeData toDomain() {
final nextPage = page != null && pageSize != null && totalCount != null
? ((page! + 1) * pageSize! < totalCount!) ? page! + 1 : null
: null;
return HomeData(
data: data?.map((e) => e.toDomain()).toList(),
nextPage: nextPage,
);
}
}
extension PokemonDataDtoToModel on PokemonDataDto {

View File

@ -1,12 +1,9 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mobilki_lab1/data/repositories/potter_repository.dart';
import 'package:mobilki_lab1/data/repositories/pokemon_repository.dart';
import 'package:mobilki_lab1/presentation/home_page/bloc/events.dart';
import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart';
import '../../../data/repositories/pokemon_repository.dart';
class HomeBloc extends Bloc<HomeEvent, HomeState> {
// final PotterRepository repo;
final PokemonRepository repo;
HomeBloc(this.repo) : super(const HomeState()) {
@ -28,15 +25,23 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
onError: (e) => error = e,
);
if (event.nextPage != null) {
data?.data?.insertAll(0, state.data?.data ?? []);
}
if (data != null) {
if (event.nextPage != null) {
data.data?.insertAll(0, state.data?.data ?? []);
}
emit(state.copyWith(
isLoading: false,
isPaginationLoading: false,
data: data,
error: error,
));
emit(state.copyWith(
isLoading: false,
isPaginationLoading: false,
data: data,
error: error,
));
} else {
emit(state.copyWith(
isLoading: false,
isPaginationLoading: false,
error: error ?? 'Failed to load data',
));
}
}
}
}