From b8afeb53b4df99270f259016fcc6763dbfd7c729 Mon Sep 17 00:00:00 2001 From: mso7nueh Date: Fri, 22 Nov 2024 16:44:52 +0300 Subject: [PATCH] feat(profile): send message and subscribe button --- .../bloc/sub_cubit/sub_cubit.dart | 8 +++ .../widgets/sub_action_button_widget.dart | 31 ++++++++++-- .../presentation/widgets/sub_widget.dart | 13 ++++- .../repositories/profile_repository.dart | 16 ++++++ .../profile/services/profile_service.dart | 10 ++++ .../profile/services/profile_service.g.dart | 50 +++++++++++++++++++ 6 files changed, 124 insertions(+), 4 deletions(-) diff --git a/lib/features/profile/presentation/bloc/sub_cubit/sub_cubit.dart b/lib/features/profile/presentation/bloc/sub_cubit/sub_cubit.dart index 98e93bcb..ea5e2c15 100644 --- a/lib/features/profile/presentation/bloc/sub_cubit/sub_cubit.dart +++ b/lib/features/profile/presentation/bloc/sub_cubit/sub_cubit.dart @@ -26,4 +26,12 @@ class SubCubit extends Cubit { emit(SubState.error(errorMessage: e.toString())); } } + + subscribe(String slug) async { + return await profileRepository.subscribe(slug); + } + + unsubscribe(String slug) async { + return await profileRepository.unsubscribe(slug); + } } diff --git a/lib/features/profile/presentation/widgets/sub_action_button_widget.dart b/lib/features/profile/presentation/widgets/sub_action_button_widget.dart index 4337775f..0db41687 100644 --- a/lib/features/profile/presentation/widgets/sub_action_button_widget.dart +++ b/lib/features/profile/presentation/widgets/sub_action_button_widget.dart @@ -1,14 +1,21 @@ +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/profile_cubit/profile_cubit.dart'; +import 'package:eatvio_app/features/profile/presentation/bloc/sub_cubit/sub_cubit.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:go_router/go_router.dart'; enum SubActionType { SendMessage, Subscribe } class SubActionButtonWidget extends StatefulWidget { final SubActionType subActionType; + final String slug; const SubActionButtonWidget({ super.key, required this.subActionType, + required this.slug, }); @override @@ -16,11 +23,24 @@ class SubActionButtonWidget extends StatefulWidget { } class _SubActionButtonWidgetState extends State { + late SubActionType subActionType; + + @override + void initState() { + super.initState(); + subActionType = widget.subActionType; + } + @override Widget build(BuildContext context) { - if (widget.subActionType == SubActionType.Subscribe) { + if (subActionType == SubActionType.Subscribe) { return InkWell( - onTap: () {}, + onTap: () { + context.read().subscribe(widget.slug); + setState(() { + subActionType = SubActionType.SendMessage; + }); + }, borderRadius: BorderRadius.circular(8), child: Ink( padding: EdgeInsets.symmetric(horizontal: 12, vertical: 4), @@ -40,7 +60,12 @@ class _SubActionButtonWidgetState extends State { ); } else { return InkWell( - onTap: () {}, + onTap: () async { + GoRouter.of(context).pushNamed( + 'chatScreen', + extra: await sl().getChatRoomIdBySlug(slug: widget.slug), + ); + }, borderRadius: BorderRadius.circular(8), child: Ink( padding: EdgeInsets.symmetric(horizontal: 12, vertical: 4), diff --git a/lib/features/profile/presentation/widgets/sub_widget.dart b/lib/features/profile/presentation/widgets/sub_widget.dart index fc4e9f0d..444c48f5 100644 --- a/lib/features/profile/presentation/widgets/sub_widget.dart +++ b/lib/features/profile/presentation/widgets/sub_widget.dart @@ -1,6 +1,8 @@ +import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart'; import 'package:eatvio_app/features/profile/models/sub_model.dart'; import 'package:eatvio_app/features/profile/presentation/widgets/sub_action_button_widget.dart'; import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; class SubWidget extends StatefulWidget { final SubModel subModel; @@ -18,7 +20,15 @@ class _SubWidgetState extends State { @override Widget build(BuildContext context) { return InkWell( - onTap: () {}, + onTap: () => GoRouter.of(context).pushNamed( + 'profileScreen', + extra: Author( + authorSlug: widget.subModel.slug, + authorName: widget.subModel.name, + authorProfileIcon: widget.subModel.profilePhotoUrl, + authorIsMe: false, + ), + ), child: Ink( padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16), child: Row( @@ -56,6 +66,7 @@ class _SubWidgetState extends State { const Spacer(), SubActionButtonWidget( subActionType: widget.subModel.isSubscribe ? SubActionType.SendMessage : SubActionType.Subscribe, + slug: widget.subModel.slug, ), ], ), diff --git a/lib/features/profile/repositories/profile_repository.dart b/lib/features/profile/repositories/profile_repository.dart index 911fc981..5432c3b8 100644 --- a/lib/features/profile/repositories/profile_repository.dart +++ b/lib/features/profile/repositories/profile_repository.dart @@ -129,4 +129,20 @@ class ProfileRepository { rethrow; } } + + Future subscribe(String slug) async { + try { + return await profileService.subscribe(slug: slug); + } catch (e) { + rethrow; + } + } + + Future unsubscribe(String slug) async { + try { + return await profileService.unsubscribe(slug: slug); + } catch (e) { + rethrow; + } + } } diff --git a/lib/features/profile/services/profile_service.dart b/lib/features/profile/services/profile_service.dart index 11b7abaf..477a1aa0 100644 --- a/lib/features/profile/services/profile_service.dart +++ b/lib/features/profile/services/profile_service.dart @@ -70,4 +70,14 @@ abstract class ProfileService { Future getSubscriptions({ @Path() required String slug, }); + + @POST('/api/profile/subscribe/{slug}') + Future subscribe({ + @Path() required String slug, + }); + + @POST('/api/profile/unsubscribe/{slug}') + Future unsubscribe({ + @Path() required String slug, + }); } diff --git a/lib/features/profile/services/profile_service.g.dart b/lib/features/profile/services/profile_service.g.dart index 5121bf4c..f43fbfe2 100644 --- a/lib/features/profile/services/profile_service.g.dart +++ b/lib/features/profile/services/profile_service.g.dart @@ -442,6 +442,56 @@ class _ProfileService implements ProfileService { return _value; } + @override + Future subscribe({required String slug}) async { + final _extra = {}; + final queryParameters = {}; + final _headers = {}; + const Map? _data = null; + final _options = _setStreamType(Options( + method: 'POST', + headers: _headers, + extra: _extra, + ) + .compose( + _dio.options, + '/api/profile/subscribe/${slug}', + queryParameters: queryParameters, + data: _data, + ) + .copyWith( + baseUrl: _combineBaseUrls( + _dio.options.baseUrl, + baseUrl, + ))); + await _dio.fetch(_options); + } + + @override + Future unsubscribe({required String slug}) async { + final _extra = {}; + final queryParameters = {}; + final _headers = {}; + const Map? _data = null; + final _options = _setStreamType(Options( + method: 'POST', + headers: _headers, + extra: _extra, + ) + .compose( + _dio.options, + '/api/profile/unsubscribe/${slug}', + queryParameters: queryParameters, + data: _data, + ) + .copyWith( + baseUrl: _combineBaseUrls( + _dio.options.baseUrl, + baseUrl, + ))); + await _dio.fetch(_options); + } + RequestOptions _setStreamType(RequestOptions requestOptions) { if (T != dynamic && !(requestOptions.responseType == ResponseType.bytes ||