diff --git a/lib/data/mappers/user_mapper.dart b/lib/data/mappers/user_mapper.dart index a534764..786cde9 100644 --- a/lib/data/mappers/user_mapper.dart +++ b/lib/data/mappers/user_mapper.dart @@ -12,9 +12,17 @@ extension UserDtoToModel on UserDto { } return CardPostData( - name ?? "", - description ?? "", - isValidImageUrl(image) ? image! : "https://placehold.co/480x640/png", - false); + name: name ?? "", + surname: surname ?? "", + description: description ?? "", + imageUrl: isValidImageUrl(image) + ? image + : (gender! == "male" + ? "https://avatar.iran.liara.run/public/boy" + : "https://avatar.iran.liara.run/public/girl") + .toString(), + isLiked: false, + age: age, + distance: distance); } } diff --git a/lib/data/repositories/api_user_repository.dart b/lib/data/repositories/api_user_repository.dart new file mode 100644 index 0000000..ef69560 --- /dev/null +++ b/lib/data/repositories/api_user_repository.dart @@ -0,0 +1,30 @@ +import 'package:pmu/data/dto/page_dto.dart'; +import 'package:pmu/data/dto/user_dto.dart'; +import 'package:pmu/data/mappers/user_mapper.dart'; +import 'package:pmu/data/repositories/api_interface.dart'; +import 'package:pmu/domain/card.dart'; +import 'package:dio/dio.dart'; +import 'package:pretty_dio_logger/pretty_dio_logger.dart'; + +class ApiUserRepository extends ApiInterface { + static final Dio _dio = Dio() + ..interceptors.add(PrettyDioLogger(requestHeader: true, requestBody: true)); + static const String _baseUrl = + 'http://lovesearch-api.nspotapov.ru/api/users/'; + + @override + Future?> loadData() async { + const String url = _baseUrl; + final List data = []; + final Response response = + await _dio.get>(url); + final PageDto pageDto = + PageDto.fromJson(response.data as Map); + int itemsCount = pageDto.itemsCount ?? 0; + for (int i = 0; i < itemsCount; i++) { + final CardPostData cardPost = pageDto.items![i].toDomain(); + data.add(cardPost); + } + return data; + } +} diff --git a/lib/data/repositories/mock_user_repository.dart b/lib/data/repositories/mock_user_repository.dart index dc924a0..76268d8 100644 --- a/lib/data/repositories/mock_user_repository.dart +++ b/lib/data/repositories/mock_user_repository.dart @@ -5,70 +5,6 @@ class MockUserRepository extends ApiInterface { @override Future?> loadData() async { return [ - const CardPostData( - name: "Марьяна", - surname: "Ро", - description: "Люблю вечерами погамать в майн", - imageUrl: "https://placehold.co/600x400/png", - age: 21, - distance: 24.5, - isLiked: false), - const CardPostData( - name: "Константин", - surname: "Злобин", - description: "Веду канал в тг про криптожизнь", - imageUrl: "https://placehold.co/600x400/png", - age: 24, - distance: 478.3, - isLiked: false), - const CardPostData( - name: "Марьяна", - surname: "Ро", - description: "Люблю вечерами погамать в майн", - imageUrl: "https://placehold.co/600x400/png", - age: 21, - distance: 24.5, - isLiked: false), - const CardPostData( - name: "Константин", - surname: "Злобин", - description: "Веду канал в тг про криптожизнь", - imageUrl: "https://placehold.co/600x400/png", - age: 24, - distance: 478.3, - isLiked: false), - const CardPostData( - name: "Марьяна", - surname: "Ро", - description: "Люблю вечерами погамать в майн", - imageUrl: "https://placehold.co/600x400/png", - age: 21, - distance: 24.5, - isLiked: false), - const CardPostData( - name: "Константин", - surname: "Злобин", - description: "Веду канал в тг про криптожизнь", - imageUrl: "https://placehold.co/600x400/png", - age: 24, - distance: 478.3, - isLiked: false), - const CardPostData( - name: "Марьяна", - surname: "Ро", - description: "Люблю вечерами погамать в майн", - imageUrl: "https://placehold.co/600x400/png", - age: 21, - distance: 24.5, - isLiked: false), - const CardPostData( - name: "Константин", - surname: "Злобин", - description: "Веду канал в тг про криптожизнь", - imageUrl: "https://placehold.co/600x400/png", - age: 24, - distance: 478.3, - isLiked: false), const CardPostData( name: "Марьяна", surname: "Ро", diff --git a/lib/domain/card.dart b/lib/domain/card.dart index 3a96a32..d188721 100644 --- a/lib/domain/card.dart +++ b/lib/domain/card.dart @@ -1,8 +1,18 @@ class CardPostData { - final String name; - final String description; + final String? name; + final String? surname; + final String? description; final String? imageUrl; - final bool isLiked; + final int? age; + final double? distance; + final bool? isLiked; - const CardPostData(this.name, this.description, this.imageUrl, this.isLiked); + const CardPostData( + {this.name, + this.surname, + this.description, + this.imageUrl, + this.isLiked, + this.age, + this.distance}); } diff --git a/lib/presentation/detail_pages/card_detail_page.dart b/lib/presentation/detail_pages/card_detail_page.dart index ebd244e..e9d724b 100644 --- a/lib/presentation/detail_pages/card_detail_page.dart +++ b/lib/presentation/detail_pages/card_detail_page.dart @@ -25,14 +25,14 @@ class DetailPage extends StatelessWidget { Padding( padding: const EdgeInsets.all(4.0), child: Text( - data.name, + data.name ?? "", style: const TextStyle(color: Colors.black, fontSize: 30), ), ), Padding( padding: const EdgeInsets.all(4.0), child: Text( - data.description, + data.description ?? "", style: const TextStyle(color: Colors.black, fontSize: 20), ), ), diff --git a/lib/presentation/home_page/card.dart b/lib/presentation/home_page/card.dart index 59aeb86..70dd95c 100644 --- a/lib/presentation/home_page/card.dart +++ b/lib/presentation/home_page/card.dart @@ -9,21 +9,34 @@ const double NORMAL_ICON_SCALE = 2.0; const double SCALED_ICON_SCALE = 2.5; class CardPost extends StatefulWidget { - final String description; + final String? description; final String? imageUrl; - final bool isLiked; - final String name; + final bool? isLiked; + final String? name; + final String? surname; final OnLikeCallback onLike; final VoidCallback? onTap; - const CardPost(this.name, this.description, this.imageUrl, this.isLiked, - this.onLike, this.onTap); + const CardPost( + {this.name, + this.surname, + this.description, + this.imageUrl, + this.isLiked, + this.onLike, + this.onTap}); factory CardPost.fromData(CardPostData data, {OnLikeCallback onLike, VoidCallback? onTap}) => - CardPost(data.name, data.description, data.imageUrl, data.isLiked, onLike, - onTap); + CardPost( + name: data.name, + surname: data.surname, + description: data.description, + imageUrl: data.imageUrl, + isLiked: data.isLiked, + onLike: onLike, + onTap: onTap); @override State createState() => _CardPostState(); @@ -67,7 +80,7 @@ class _CardPostState extends State { children: [ Flexible( child: Text( - widget.name, + (widget.name ?? "") + (" ") + (widget.surname ?? ""), style: const TextStyle( color: Colors.white, fontSize: 30, @@ -83,7 +96,7 @@ class _CardPostState extends State { children: [ Flexible( child: Text( - widget.description, + widget.description ?? "", textAlign: TextAlign.center, style: const TextStyle( color: Colors.white, @@ -118,7 +131,7 @@ class _CardPostState extends State { iconScale = NORMAL_ICON_SCALE; }); }); - widget.onLike?.call(widget.name, isLiked); + widget.onLike?.call(widget.name ?? "", isLiked); }) }, child: AnimatedScale( diff --git a/lib/presentation/home_page/home_page.dart b/lib/presentation/home_page/home_page.dart index f8d3f6f..79c6876 100644 --- a/lib/presentation/home_page/home_page.dart +++ b/lib/presentation/home_page/home_page.dart @@ -1,5 +1,6 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:pmu/data/repositories/api_user_repository.dart'; import 'package:pmu/domain/card.dart'; import 'package:pmu/presentation/detail_pages/card_detail_page.dart'; import 'package:pmu/presentation/home_page/card.dart'; @@ -42,10 +43,7 @@ class Body extends StatelessWidget { content: Text( '${isLiked ? 'Вы поставили лайк: ' : 'Вы убрали лайк: '} $title', style: const TextStyle( - color: Colors.black45, - fontSize: 20, - fontWeight: FontWeight.bold - ), + color: Colors.black45, fontSize: 20, fontWeight: FontWeight.bold), ), backgroundColor: isLiked ? Colors.green : Colors.red, duration: const Duration(seconds: 2), @@ -55,32 +53,24 @@ class Body extends StatelessWidget { @override Widget build(BuildContext context) { - final data = [ - const CardPostData( - "Евгения", - "Люблю программирование и кататься на скейте", - "https://avatar.iran.liara.run/public/girl", - false), - const CardPostData("Алекс", "Junior Flutter Engineer", - "https://avatar.iran.liara.run/public/36", false), - const CardPostData( - "Станислав", - "Нет ничего приятнее прогулки на природе!", - "https://avatar.iran.liara.run/public/boy", - false), - const CardPostData("Вероника", "В свободное время хожу на уроки вокала", - "https://avatar.iran.liara.run/public/93", false), - ]; + final data = ApiUserRepository().loadData(); return Center( - child: SingleChildScrollView( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: data.map((data) { - return CardPost.fromData(data, - onLike: (String title, bool isLiked) => - _showSnackBar(context, title, isLiked), - onTap: () => _navToDetails(context, data)); - }).toList(), - ))); + child: FutureBuilder?>( + future: data, + builder: (context, snapshot) => SingleChildScrollView( + child: snapshot.hasData + ? Column( + mainAxisAlignment: MainAxisAlignment.center, + children: snapshot.data + ?.map((data) => CardPost.fromData(data, + onLike: (String title, bool isLiked) => + _showSnackBar(context, title, isLiked), + onTap: () => _navToDetails(context, data))) + .toList() ?? + []) + : const CircularProgressIndicator(), + ), + ), + ); } }