From ba69a8aa27200be2aa96c97a368e90ddc021f126 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Fri, 18 Oct 2024 19:20:31 +0400 Subject: [PATCH] 5 --- lib/Presentation/home_page/home_page.dart | 68 ++++++++++++++----- lib/data/dtos/bread_dto.dart | 33 --------- lib/data/dtos/breads_dto.dart | 28 ++++++++ .../{bread_dto.g.dart => breads_dto.g.dart} | 20 ++---- lib/data/mappers/bread_mapper.dart | 8 +-- lib/repositories/bread_repository.dart | 36 ++++++++++ 6 files changed, 125 insertions(+), 68 deletions(-) delete mode 100644 lib/data/dtos/bread_dto.dart create mode 100644 lib/data/dtos/breads_dto.dart rename lib/data/dtos/{bread_dto.g.dart => breads_dto.g.dart} (50%) create mode 100644 lib/repositories/bread_repository.dart diff --git a/lib/Presentation/home_page/home_page.dart b/lib/Presentation/home_page/home_page.dart index 4daa024..0d093f4 100644 --- a/lib/Presentation/home_page/home_page.dart +++ b/lib/Presentation/home_page/home_page.dart @@ -1,6 +1,8 @@ +import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:labs_petrushin/repositories/mock_repository.dart'; import '../../Presentation/detailPage.dart'; +import '../../repositories/bread_repository.dart'; part '../../domain/models/card.dart'; class MyHomePage extends StatefulWidget { @@ -31,27 +33,61 @@ class _MyHomePageState extends State { } } -class Body extends StatelessWidget { +class Body extends StatefulWidget { const Body({super.key}); + @override + State createState() => _BodyState(); +} + +class _BodyState extends State { + final TextEditingController searchController = TextEditingController(); + late Future?> data; + + final repo = BreadRepository(); + + @override + void initState() { + data = repo.loadData(); + super.initState(); + } + @override Widget build(BuildContext context) { - final data = MockRepository().loadData(); - return Center( - child: FutureBuilder?>( - future: data, - builder: (context, snapshot) => - SingleChildScrollView( - child: snapshot.hasData ? Column( - mainAxisAlignment: MainAxisAlignment.center, - children: snapshot.data?.map((e) => MyCard.fromData( - e, - onLike: () => _showSnackBar(context), - )).toList() ?? [], - ) - : const CircularProgressIndicator(), + return Column( + children: [ + Padding( + padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top), + child: Padding(padding: const EdgeInsets.all(12), + child: CupertinoSearchTextField( + controller: searchController, + onChanged: (search) { + setState(() { + data = repo.loadData(q: search); + }); + }, + ) + ), + ), + Expanded( + child: Center( + child: FutureBuilder?>( + future: data, + builder: (context, snapshot) => + SingleChildScrollView( + child: snapshot.hasData ? Column( + mainAxisAlignment: MainAxisAlignment.center, + children: snapshot.data?.map((e) => MyCard.fromData( + e, + onLike: () => _showSnackBar(context), + )).toList() ?? [], + ) + : const CircularProgressIndicator(), + ), ), - ), + ), + ), + ], ); } diff --git a/lib/data/dtos/bread_dto.dart b/lib/data/dtos/bread_dto.dart deleted file mode 100644 index fa0fa1f..0000000 --- a/lib/data/dtos/bread_dto.dart +++ /dev/null @@ -1,33 +0,0 @@ -import 'package:json_annotation/json_annotation.dart'; - -part 'bread_dto.g.dart'; - -@JsonSerializable(createToJson: false) -class BreadsDto { - final List? data; - - const BreadsDto({this.data}); - - factory BreadsDto.fromJson(Map json) => _$BreadsDtoFromJson(json); -} - -@JsonSerializable(createToJson: false) -class BreadDataDto { - final String? id; - final String? type; - final BreadAttributesDataDto? attributes; - - const BreadDataDto({this.id, this.type, this.attributes}); - - factory BreadDataDto.fromJson(Map json) => _$BreadDataDtoFromJson(json); -} - -@JsonSerializable(createToJson: false) -class BreadAttributesDataDto { - final String? name; - final String? info; - final String? image; - - const BreadAttributesDataDto({this.name, this.info, this.image}); - factory BreadAttributesDataDto.fromJson(Map json) => _$BreadAttributesDataDtoFromJson(json); -} \ No newline at end of file diff --git a/lib/data/dtos/breads_dto.dart b/lib/data/dtos/breads_dto.dart new file mode 100644 index 0000000..34965af --- /dev/null +++ b/lib/data/dtos/breads_dto.dart @@ -0,0 +1,28 @@ +import 'dart:convert'; +import 'dart:ffi'; + +import 'package:json_annotation/json_annotation.dart'; + +part 'breads_dto.g.dart'; + +@JsonSerializable(createToJson: false) +class BreadsDto { + final List? foods; + + const BreadsDto({this.foods}); + + factory BreadsDto.fromJson(Map json) => _$BreadsDtoFromJson(json); + +} + +@JsonSerializable(createToJson: false) +class BreadDataDto { + final int? fdcId; + final String? brandName; + final String? description; + final String? image; + + const BreadDataDto({this.fdcId, this.brandName, this.description, this.image}); + + factory BreadDataDto.fromJson(Map json) => _$BreadDataDtoFromJson(json); +} \ No newline at end of file diff --git a/lib/data/dtos/bread_dto.g.dart b/lib/data/dtos/breads_dto.g.dart similarity index 50% rename from lib/data/dtos/bread_dto.g.dart rename to lib/data/dtos/breads_dto.g.dart index 8d78cdc..6d01075 100644 --- a/lib/data/dtos/bread_dto.g.dart +++ b/lib/data/dtos/breads_dto.g.dart @@ -1,30 +1,20 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -part of 'bread_dto.dart'; +part of 'breads_dto.dart'; // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** BreadsDto _$BreadsDtoFromJson(Map json) => BreadsDto( - data: (json['data'] as List?) + foods: (json['foods'] as List?) ?.map((e) => BreadDataDto.fromJson(e as Map)) .toList(), ); BreadDataDto _$BreadDataDtoFromJson(Map json) => BreadDataDto( - id: json['id'] as String?, - type: json['type'] as String?, - attributes: json['attributes'] == null - ? null - : BreadAttributesDataDto.fromJson( - json['attributes'] as Map), - ); - -BreadAttributesDataDto _$BreadAttributesDataDtoFromJson( - Map json) => - BreadAttributesDataDto( - name: json['name'] as String?, - info: json['info'] as String?, + fdcId: (json['fdcId'] as num?)?.toInt(), + brandName: json['brandName'] as String?, + description: json['description'] as String?, image: json['image'] as String?, ); diff --git a/lib/data/mappers/bread_mapper.dart b/lib/data/mappers/bread_mapper.dart index a0d74d8..e02521b 100644 --- a/lib/data/mappers/bread_mapper.dart +++ b/lib/data/mappers/bread_mapper.dart @@ -1,10 +1,10 @@ import 'package:labs_petrushin/Presentation/home_page/home_page.dart'; -import 'package:labs_petrushin/data/dtos/bread_dto.dart'; +import 'package:labs_petrushin/data/dtos/breads_dto.dart'; extension CharacterDataDtoToModel on BreadDataDto { CardData toDomain() => CardData( - text: attributes?.name ?? "Просто хлэп", - info: attributes?.info ?? "Очень кусьна", - urlImage: attributes?.image); + text: brandName ?? "Просто хлэп", + info: description ?? "Очень кусьна", + urlImage: image); } \ No newline at end of file diff --git a/lib/repositories/bread_repository.dart b/lib/repositories/bread_repository.dart new file mode 100644 index 0000000..b44913e --- /dev/null +++ b/lib/repositories/bread_repository.dart @@ -0,0 +1,36 @@ + +import 'package:dio/dio.dart'; +import 'package:labs_petrushin/Presentation/home_page/home_page.dart'; +import 'package:labs_petrushin/data/dtos/breads_dto.dart'; +import 'package:labs_petrushin/data/mappers/bread_mapper.dart'; +import 'package:labs_petrushin/repositories/api_interface.dart'; +import 'package:pretty_dio_logger/pretty_dio_logger.dart'; + +class BreadRepository extends ApiInterface { + static final Dio _dio = Dio() + ..interceptors.add( + PrettyDioLogger( + requestHeader: true, + requestBody: true, + )); + + // 91xPcWwfSGljSRMuoS8IH0GP4hM9QqwwtgSzqJMw + static const String _baseUrl = 'https://api.nal.usda.gov'; + + @override + Future?> loadData({String? q}) async { + try { + final String searchQuery = q ?? 'Bread'; // Используем 'Bread' как значение по умолчанию + final String url = '$_baseUrl/fdc/v1/foods/search?api_key=91xPcWwfSGljSRMuoS8IH0GP4hM9QqwwtgSzqJMw&query=$searchQuery&pageSize=10'; + + final Response response = await _dio.get>(url); + + final BreadsDto dto = BreadsDto.fromJson(response.data as Map); + final List? data = dto.foods?.map((e) => e.toDomain()).toList(); + return data; + } on DioException catch (e) { + // err + return null; + } + } +} \ No newline at end of file