From 90346127473733c75093568e52cdd9ea5b463410 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Sun, 20 Oct 2024 12:36:08 +0400 Subject: [PATCH] =?UTF-8?q?6=20=D0=BF=D0=BE=D1=87=D0=B8=D0=BD=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=B0=D0=B3=D0=B8=D0=BD=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/data/dtos/foods_dto.dart | 25 +++---------------------- lib/data/dtos/foods_dto.g.dart | 18 ++---------------- lib/data/mappers/food_mapper.dart | 16 ++++++++++++---- 3 files changed, 17 insertions(+), 42 deletions(-) diff --git a/lib/data/dtos/foods_dto.dart b/lib/data/dtos/foods_dto.dart index 6740bea..9c63744 100644 --- a/lib/data/dtos/foods_dto.dart +++ b/lib/data/dtos/foods_dto.dart @@ -8,9 +8,10 @@ part 'foods_dto.g.dart'; @JsonSerializable(createToJson: false) class FoodsDto { final List? foods; - final MetaDto? meta; + final int? currentPage; + final int? totalPages; - const FoodsDto({this.foods, this.meta}); + const FoodsDto({this.foods, this.currentPage, this.totalPages}); factory FoodsDto.fromJson(Map json) => _$FoodsDtoFromJson(json); @@ -26,24 +27,4 @@ class FoodDataDto { const FoodDataDto({this.fdcId, this.brandName, this.description, this.image}); factory FoodDataDto.fromJson(Map json) => _$FoodDataDtoFromJson(json); -} - -@JsonSerializable(createToJson: false) -class MetaDto { - final PaginationDto? pagination; - - const MetaDto({this.pagination}); - - factory MetaDto.fromJson(Map 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 json) => _$PaginationDtoFromJson(json); } \ No newline at end of file diff --git a/lib/data/dtos/foods_dto.g.dart b/lib/data/dtos/foods_dto.g.dart index d571098..0102141 100644 --- a/lib/data/dtos/foods_dto.g.dart +++ b/lib/data/dtos/foods_dto.g.dart @@ -10,9 +10,8 @@ FoodsDto _$FoodsDtoFromJson(Map json) => FoodsDto( foods: (json['foods'] as List?) ?.map((e) => FoodDataDto.fromJson(e as Map)) .toList(), - meta: json['meta'] == null - ? null - : MetaDto.fromJson(json['meta'] as Map), + currentPage: (json['currentPage'] as num?)?.toInt(), + totalPages: (json['totalPages'] as num?)?.toInt(), ); FoodDataDto _$FoodDataDtoFromJson(Map json) => FoodDataDto( @@ -21,16 +20,3 @@ FoodDataDto _$FoodDataDtoFromJson(Map json) => FoodDataDto( description: json['description'] as String?, image: json['image'] as String?, ); - -MetaDto _$MetaDtoFromJson(Map json) => MetaDto( - pagination: json['pagination'] == null - ? null - : PaginationDto.fromJson(json['pagination'] as Map), - ); - -PaginationDto _$PaginationDtoFromJson(Map json) => - PaginationDto( - current: (json['current'] as num?)?.toInt(), - next: (json['next'] as num?)?.toInt(), - last: (json['last'] as num?)?.toInt(), - ); diff --git a/lib/data/mappers/food_mapper.dart b/lib/data/mappers/food_mapper.dart index 08ea21b..86d049e 100644 --- a/lib/data/mappers/food_mapper.dart +++ b/lib/data/mappers/food_mapper.dart @@ -12,8 +12,16 @@ extension CharacterDataDtoToModel on FoodDataDto { } extension CharactersDtoToModel on FoodsDto { - HomeData toDomain() => HomeData( - data: foods?.map((e) => e.toDomain()).toList(), - nextPage: meta?.pagination?.next, - ); + HomeData toDomain() { + int? nextPage = currentPage; + + if (currentPage != null && totalPages != null && totalPages! > currentPage!) { + nextPage = currentPage! + 1; + } + + return HomeData( + data: foods?.map((e) => e.toDomain()).toList(), + nextPage: nextPage, + ); + } } \ No newline at end of file