починила пагинацию
This commit is contained in:
parent
a631adb572
commit
5fd2a61912
@ -5,11 +5,17 @@ part 'pokemon_dto.g.dart';
|
|||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class PokemonDto {
|
class PokemonDto {
|
||||||
final List<PokemonDataDto>? data;
|
final List<PokemonDataDto>? data;
|
||||||
final MetaDto? meta;
|
final int? page;
|
||||||
|
final int? pageSize;
|
||||||
|
final int? count;
|
||||||
|
final int? totalCount;
|
||||||
|
|
||||||
const PokemonDto({
|
const PokemonDto({
|
||||||
this.data,
|
this.data,
|
||||||
this.meta,
|
this.page,
|
||||||
|
this.pageSize,
|
||||||
|
this.count,
|
||||||
|
this.totalCount,
|
||||||
});
|
});
|
||||||
|
|
||||||
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
|
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
|
||||||
@ -57,23 +63,3 @@ class PokemonImagesDto {
|
|||||||
|
|
||||||
factory PokemonImagesDto.fromJson(Map<String, dynamic> json) => _$PokemonImagesDtoFromJson(json);
|
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);
|
|
||||||
}
|
|
@ -10,9 +10,10 @@ PokemonDto _$PokemonDtoFromJson(Map<String, dynamic> json) => PokemonDto(
|
|||||||
data: (json['data'] as List<dynamic>?)
|
data: (json['data'] as List<dynamic>?)
|
||||||
?.map((e) => PokemonDataDto.fromJson(e as Map<String, dynamic>))
|
?.map((e) => PokemonDataDto.fromJson(e as Map<String, dynamic>))
|
||||||
.toList(),
|
.toList(),
|
||||||
meta: json['meta'] == null
|
page: (json['page'] as num?)?.toInt(),
|
||||||
? null
|
pageSize: (json['pageSize'] as num?)?.toInt(),
|
||||||
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
|
count: (json['count'] as num?)?.toInt(),
|
||||||
|
totalCount: (json['totalCount'] as num?)?.toInt(),
|
||||||
);
|
);
|
||||||
|
|
||||||
PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
|
PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
|
||||||
@ -44,16 +45,3 @@ PokemonImagesDto _$PokemonImagesDtoFromJson(Map<String, dynamic> json) =>
|
|||||||
PokemonImagesDto(
|
PokemonImagesDto(
|
||||||
large: json['large'] as String?,
|
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(),
|
|
||||||
);
|
|
||||||
|
@ -7,11 +7,17 @@ const _imagePlaceholder =
|
|||||||
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
||||||
|
|
||||||
extension PokemonDtoToModel on PokemonDto {
|
extension PokemonDtoToModel on PokemonDto {
|
||||||
HomeData toDomain() => HomeData(
|
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(),
|
data: data?.map((e) => e.toDomain()).toList(),
|
||||||
nextPage: meta?.pagination?.next,
|
nextPage: nextPage,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extension PokemonDataDtoToModel on PokemonDataDto {
|
extension PokemonDataDtoToModel on PokemonDataDto {
|
||||||
CardData toDomain() => CardData(
|
CardData toDomain() => CardData(
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
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/events.dart';
|
||||||
import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart';
|
import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart';
|
||||||
|
|
||||||
import '../../../data/repositories/pokemon_repository.dart';
|
|
||||||
|
|
||||||
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||||
// final PotterRepository repo;
|
|
||||||
final PokemonRepository repo;
|
final PokemonRepository repo;
|
||||||
|
|
||||||
HomeBloc(this.repo) : super(const HomeState()) {
|
HomeBloc(this.repo) : super(const HomeState()) {
|
||||||
@ -28,8 +25,9 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
|||||||
onError: (e) => error = e,
|
onError: (e) => error = e,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (data != null) {
|
||||||
if (event.nextPage != null) {
|
if (event.nextPage != null) {
|
||||||
data?.data?.insertAll(0, state.data?.data ?? []);
|
data.data?.insertAll(0, state.data?.data ?? []);
|
||||||
}
|
}
|
||||||
|
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
@ -38,5 +36,12 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
|||||||
data: data,
|
data: data,
|
||||||
error: error,
|
error: error,
|
||||||
));
|
));
|
||||||
|
} else {
|
||||||
|
emit(state.copyWith(
|
||||||
|
isLoading: false,
|
||||||
|
isPaginationLoading: false,
|
||||||
|
error: error ?? 'Failed to load data',
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user