feat(profile): send message and subscribe button

This commit is contained in:
mso7nueh
2024-11-22 16:44:52 +03:00
parent c909c75d47
commit b8afeb53b4
6 changed files with 124 additions and 4 deletions

View File

@@ -26,4 +26,12 @@ class SubCubit extends Cubit<SubState> {
emit(SubState.error(errorMessage: e.toString()));
}
}
subscribe(String slug) async {
return await profileRepository.subscribe(slug);
}
unsubscribe(String slug) async {
return await profileRepository.unsubscribe(slug);
}
}

View File

@@ -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<SubActionButtonWidget> {
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<SubCubit>().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<SubActionButtonWidget> {
);
} else {
return InkWell(
onTap: () {},
onTap: () async {
GoRouter.of(context).pushNamed(
'chatScreen',
extra: await sl<ProfileCubit>().getChatRoomIdBySlug(slug: widget.slug),
);
},
borderRadius: BorderRadius.circular(8),
child: Ink(
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 4),

View File

@@ -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<SubWidget> {
@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<SubWidget> {
const Spacer(),
SubActionButtonWidget(
subActionType: widget.subModel.isSubscribe ? SubActionType.SendMessage : SubActionType.Subscribe,
slug: widget.subModel.slug,
),
],
),

View File

@@ -129,4 +129,20 @@ class ProfileRepository {
rethrow;
}
}
Future<void> subscribe(String slug) async {
try {
return await profileService.subscribe(slug: slug);
} catch (e) {
rethrow;
}
}
Future<void> unsubscribe(String slug) async {
try {
return await profileService.unsubscribe(slug: slug);
} catch (e) {
rethrow;
}
}
}

View File

@@ -70,4 +70,14 @@ abstract class ProfileService {
Future<SubPaginationModel> getSubscriptions({
@Path() required String slug,
});
@POST('/api/profile/subscribe/{slug}')
Future<void> subscribe({
@Path() required String slug,
});
@POST('/api/profile/unsubscribe/{slug}')
Future<void> unsubscribe({
@Path() required String slug,
});
}

View File

@@ -442,6 +442,56 @@ class _ProfileService implements ProfileService {
return _value;
}
@override
Future<void> subscribe({required String slug}) async {
final _extra = <String, dynamic>{};
final queryParameters = <String, dynamic>{};
final _headers = <String, dynamic>{};
const Map<String, dynamic>? _data = null;
final _options = _setStreamType<void>(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<void>(_options);
}
@override
Future<void> unsubscribe({required String slug}) async {
final _extra = <String, dynamic>{};
final queryParameters = <String, dynamic>{};
final _headers = <String, dynamic>{};
const Map<String, dynamic>? _data = null;
final _options = _setStreamType<void>(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<void>(_options);
}
RequestOptions _setStreamType<T>(RequestOptions requestOptions) {
if (T != dynamic &&
!(requestOptions.responseType == ResponseType.bytes ||