From 09e641ec632796e94662bd258aeb729bb22b28c2 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 2 Dec 2024 12:56:28 +0300 Subject: [PATCH] feat(feed): add subscribe button --- .../feed/models/diets_pagination_model.dart | 1 + .../diets_pagination_model.freezed.dart | 53 ++++++++++++----- .../feed/models/diets_pagination_model.g.dart | 2 + .../widgets/blog_post_widget.dart | 24 ++------ .../widgets/subscribe_button_widget.dart | 57 +++++++++++++++++++ .../presentation/widgets/sub_widget.dart | 1 + 6 files changed, 105 insertions(+), 33 deletions(-) create mode 100644 lib/features/feed/presentation/widgets/subscribe_button_widget.dart diff --git a/lib/features/feed/models/diets_pagination_model.dart b/lib/features/feed/models/diets_pagination_model.dart index 6fe4f677..426306cd 100644 --- a/lib/features/feed/models/diets_pagination_model.dart +++ b/lib/features/feed/models/diets_pagination_model.dart @@ -46,6 +46,7 @@ class Author with _$Author { @JsonKey(name: "author_name") required String authorName, @JsonKey(name: "author_profile_icon") required String authorProfileIcon, @JsonKey(name: "author_is_me") required bool authorIsMe, + @JsonKey(name: "is_entity_subscribed_by_me") required bool isEntitySubscribedByMe, }) = _Author; factory Author.fromJson(Map json) => _$AuthorFromJson(json); diff --git a/lib/features/feed/models/diets_pagination_model.freezed.dart b/lib/features/feed/models/diets_pagination_model.freezed.dart index 38789091..34cd67d0 100644 --- a/lib/features/feed/models/diets_pagination_model.freezed.dart +++ b/lib/features/feed/models/diets_pagination_model.freezed.dart @@ -812,6 +812,8 @@ mixin _$Author { String get authorProfileIcon => throw _privateConstructorUsedError; @JsonKey(name: "author_is_me") bool get authorIsMe => throw _privateConstructorUsedError; + @JsonKey(name: "is_entity_subscribed_by_me") + bool get isEntitySubscribedByMe => throw _privateConstructorUsedError; /// Serializes this Author to a JSON map. Map toJson() => throw _privateConstructorUsedError; @@ -831,7 +833,9 @@ abstract class $AuthorCopyWith<$Res> { {@JsonKey(name: "author_slug") String authorSlug, @JsonKey(name: "author_name") String authorName, @JsonKey(name: "author_profile_icon") String authorProfileIcon, - @JsonKey(name: "author_is_me") bool authorIsMe}); + @JsonKey(name: "author_is_me") bool authorIsMe, + @JsonKey(name: "is_entity_subscribed_by_me") + bool isEntitySubscribedByMe}); } /// @nodoc @@ -853,6 +857,7 @@ class _$AuthorCopyWithImpl<$Res, $Val extends Author> Object? authorName = null, Object? authorProfileIcon = null, Object? authorIsMe = null, + Object? isEntitySubscribedByMe = null, }) { return _then(_value.copyWith( authorSlug: null == authorSlug @@ -871,6 +876,10 @@ class _$AuthorCopyWithImpl<$Res, $Val extends Author> ? _value.authorIsMe : authorIsMe // ignore: cast_nullable_to_non_nullable as bool, + isEntitySubscribedByMe: null == isEntitySubscribedByMe + ? _value.isEntitySubscribedByMe + : isEntitySubscribedByMe // ignore: cast_nullable_to_non_nullable + as bool, ) as $Val); } } @@ -886,7 +895,9 @@ abstract class _$$AuthorImplCopyWith<$Res> implements $AuthorCopyWith<$Res> { {@JsonKey(name: "author_slug") String authorSlug, @JsonKey(name: "author_name") String authorName, @JsonKey(name: "author_profile_icon") String authorProfileIcon, - @JsonKey(name: "author_is_me") bool authorIsMe}); + @JsonKey(name: "author_is_me") bool authorIsMe, + @JsonKey(name: "is_entity_subscribed_by_me") + bool isEntitySubscribedByMe}); } /// @nodoc @@ -906,6 +917,7 @@ class __$$AuthorImplCopyWithImpl<$Res> Object? authorName = null, Object? authorProfileIcon = null, Object? authorIsMe = null, + Object? isEntitySubscribedByMe = null, }) { return _then(_$AuthorImpl( authorSlug: null == authorSlug @@ -924,6 +936,10 @@ class __$$AuthorImplCopyWithImpl<$Res> ? _value.authorIsMe : authorIsMe // ignore: cast_nullable_to_non_nullable as bool, + isEntitySubscribedByMe: null == isEntitySubscribedByMe + ? _value.isEntitySubscribedByMe + : isEntitySubscribedByMe // ignore: cast_nullable_to_non_nullable + as bool, )); } } @@ -935,7 +951,9 @@ class _$AuthorImpl implements _Author { {@JsonKey(name: "author_slug") required this.authorSlug, @JsonKey(name: "author_name") required this.authorName, @JsonKey(name: "author_profile_icon") required this.authorProfileIcon, - @JsonKey(name: "author_is_me") required this.authorIsMe}); + @JsonKey(name: "author_is_me") required this.authorIsMe, + @JsonKey(name: "is_entity_subscribed_by_me") + required this.isEntitySubscribedByMe}); factory _$AuthorImpl.fromJson(Map json) => _$$AuthorImplFromJson(json); @@ -952,10 +970,13 @@ class _$AuthorImpl implements _Author { @override @JsonKey(name: "author_is_me") final bool authorIsMe; + @override + @JsonKey(name: "is_entity_subscribed_by_me") + final bool isEntitySubscribedByMe; @override String toString() { - return 'Author(authorSlug: $authorSlug, authorName: $authorName, authorProfileIcon: $authorProfileIcon, authorIsMe: $authorIsMe)'; + return 'Author(authorSlug: $authorSlug, authorName: $authorName, authorProfileIcon: $authorProfileIcon, authorIsMe: $authorIsMe, isEntitySubscribedByMe: $isEntitySubscribedByMe)'; } @override @@ -970,13 +991,15 @@ class _$AuthorImpl implements _Author { (identical(other.authorProfileIcon, authorProfileIcon) || other.authorProfileIcon == authorProfileIcon) && (identical(other.authorIsMe, authorIsMe) || - other.authorIsMe == authorIsMe)); + other.authorIsMe == authorIsMe) && + (identical(other.isEntitySubscribedByMe, isEntitySubscribedByMe) || + other.isEntitySubscribedByMe == isEntitySubscribedByMe)); } @JsonKey(includeFromJson: false, includeToJson: false) @override - int get hashCode => Object.hash( - runtimeType, authorSlug, authorName, authorProfileIcon, authorIsMe); + int get hashCode => Object.hash(runtimeType, authorSlug, authorName, + authorProfileIcon, authorIsMe, isEntitySubscribedByMe); /// Create a copy of Author /// with the given fields replaced by the non-null parameter values. @@ -996,12 +1019,13 @@ class _$AuthorImpl implements _Author { abstract class _Author implements Author { const factory _Author( - {@JsonKey(name: "author_slug") required final String authorSlug, - @JsonKey(name: "author_name") required final String authorName, - @JsonKey(name: "author_profile_icon") - required final String authorProfileIcon, - @JsonKey(name: "author_is_me") required final bool authorIsMe}) = - _$AuthorImpl; + {@JsonKey(name: "author_slug") required final String authorSlug, + @JsonKey(name: "author_name") required final String authorName, + @JsonKey(name: "author_profile_icon") + required final String authorProfileIcon, + @JsonKey(name: "author_is_me") required final bool authorIsMe, + @JsonKey(name: "is_entity_subscribed_by_me") + required final bool isEntitySubscribedByMe}) = _$AuthorImpl; factory _Author.fromJson(Map json) = _$AuthorImpl.fromJson; @@ -1017,6 +1041,9 @@ abstract class _Author implements Author { @override @JsonKey(name: "author_is_me") bool get authorIsMe; + @override + @JsonKey(name: "is_entity_subscribed_by_me") + bool get isEntitySubscribedByMe; /// Create a copy of Author /// with the given fields replaced by the non-null parameter values. diff --git a/lib/features/feed/models/diets_pagination_model.g.dart b/lib/features/feed/models/diets_pagination_model.g.dart index 61060256..c7e54383 100644 --- a/lib/features/feed/models/diets_pagination_model.g.dart +++ b/lib/features/feed/models/diets_pagination_model.g.dart @@ -74,6 +74,7 @@ _$AuthorImpl _$$AuthorImplFromJson(Map json) => _$AuthorImpl( authorName: json['author_name'] as String, authorProfileIcon: json['author_profile_icon'] as String, authorIsMe: json['author_is_me'] as bool, + isEntitySubscribedByMe: json['is_entity_subscribed_by_me'] as bool, ); Map _$$AuthorImplToJson(_$AuthorImpl instance) => @@ -82,6 +83,7 @@ Map _$$AuthorImplToJson(_$AuthorImpl instance) => 'author_name': instance.authorName, 'author_profile_icon': instance.authorProfileIcon, 'author_is_me': instance.authorIsMe, + 'is_entity_subscribed_by_me': instance.isEntitySubscribedByMe, }; _$DailyScheduleImpl _$$DailyScheduleImplFromJson(Map json) => diff --git a/lib/features/feed/presentation/widgets/blog_post_widget.dart b/lib/features/feed/presentation/widgets/blog_post_widget.dart index e4ad937c..45271667 100644 --- a/lib/features/feed/presentation/widgets/blog_post_widget.dart +++ b/lib/features/feed/presentation/widgets/blog_post_widget.dart @@ -7,11 +7,11 @@ import 'package:eatvio_app/features/chat/presentation/pages/photo_view_screen.da import 'package:eatvio_app/features/feed/models/feed_blog_post_model.dart'; import 'package:eatvio_app/features/feed/presentation/bloc/feed_cubit/feed_cubit.dart'; import 'package:eatvio_app/core/theme/theme_helper.dart'; +import 'package:eatvio_app/features/feed/presentation/widgets/subscribe_button_widget.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_html/flutter_html.dart'; import 'package:flutter_svg/flutter_svg.dart'; -import 'package:fluttertoast/fluttertoast.dart'; import 'package:go_router/go_router.dart'; import 'package:share_plus/share_plus.dart'; @@ -89,26 +89,10 @@ class _BlogPostWidgetState extends State { ], ), const Spacer(), - InkWell( - onTap: () { - Fluttertoast.showToast(msg: 'ща доделаем'); - }, - borderRadius: BorderRadius.circular(8), - child: Ink( - padding: EdgeInsets.symmetric(horizontal: 12, vertical: 4), - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(8), - color: appTheme.gray200, - ), - child: Text( - 'Подписаться', - style: TextStyle( - fontSize: 13, - fontWeight: FontWeight.w500, - ), - ), + if (!widget.feedBlogPostModel.author.isEntitySubscribedByMe && !widget.feedBlogPostModel.author.authorIsMe) + SubscribeButtonWidget( + slug: widget.feedBlogPostModel.author.authorSlug, ), - ), ], ), ), diff --git a/lib/features/feed/presentation/widgets/subscribe_button_widget.dart b/lib/features/feed/presentation/widgets/subscribe_button_widget.dart new file mode 100644 index 00000000..90074bef --- /dev/null +++ b/lib/features/feed/presentation/widgets/subscribe_button_widget.dart @@ -0,0 +1,57 @@ +import 'package:eatvio_app/core/locator_service.dart'; +import 'package:eatvio_app/core/theme/theme_helper.dart'; +import 'package:eatvio_app/features/profile/presentation/bloc/sub_cubit/sub_cubit.dart'; +import 'package:flutter/material.dart'; + +class SubscribeButtonWidget extends StatefulWidget { + final String slug; + + const SubscribeButtonWidget({ + super.key, + required this.slug, + }); + + @override + State createState() => _SubscribeButtonWidgetState(); +} + +class _SubscribeButtonWidgetState extends State { + bool isSubscribe = false; + + @override + Widget build(BuildContext context) { + return AnimatedOpacity( + opacity: isSubscribe ? 0 : 1, + duration: Duration(milliseconds: 250), + child: InkWell( + onTap: () async { + try { + setState(() { + isSubscribe = true; + }); + await sl().subscribe(widget.slug); + } catch (e) { + setState(() { + isSubscribe = false; + }); + } + }, + borderRadius: BorderRadius.circular(8), + child: Ink( + padding: EdgeInsets.symmetric(horizontal: 12, vertical: 4), + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(8), + color: appTheme.gray200, + ), + child: Text( + 'Подписаться', + style: TextStyle( + fontSize: 13, + fontWeight: FontWeight.w500, + ), + ), + ), + ), + ); + } +} diff --git a/lib/features/profile/presentation/widgets/sub_widget.dart b/lib/features/profile/presentation/widgets/sub_widget.dart index 444c48f5..0a4e2d40 100644 --- a/lib/features/profile/presentation/widgets/sub_widget.dart +++ b/lib/features/profile/presentation/widgets/sub_widget.dart @@ -27,6 +27,7 @@ class _SubWidgetState extends State { authorName: widget.subModel.name, authorProfileIcon: widget.subModel.profilePhotoUrl, authorIsMe: false, + isEntitySubscribedByMe: false, ), ), child: Ink(