diff --git a/lib/core/comments/comments_cubit/comments_cubit.dart b/lib/core/comments/comments_cubit/comments_cubit.dart index 2bc21d14..7e5df141 100644 --- a/lib/core/comments/comments_cubit/comments_cubit.dart +++ b/lib/core/comments/comments_cubit/comments_cubit.dart @@ -85,20 +85,30 @@ class CommentsCubit extends Cubit { } Future deleteComment({required int id}) async { + if (state is! CommentsStateLoaded) return false; + final commentModel = (state as CommentsStateLoaded).comment; + final updatedCommentModel = commentModel.copyWith(data: commentModel.data.where((comment) => comment.id != id).toList()); try { await commentsRepository.deleteComment(id: id); + emit(CommentsState.loaded(comment: updatedCommentModel)); return true; } catch (e, st) { + emit(CommentsState.loaded(comment: commentModel)); log(e.toString(), stackTrace: st); } return false; } Future updateComment({required int id, required String body}) async { + final commentModel = (state as CommentsStateLoaded).comment; + final updatedCommentModel = + commentModel.copyWith(data: commentModel.data.map((comment) => comment.id == id ? comment.copyWith(body: body) : comment).toList()); try { await commentsRepository.updateComment(id: id, body: body); + emit(CommentsState.loaded(comment: updatedCommentModel)); return true; } catch (e, st) { + emit(CommentsState.loaded(comment: commentModel)); log(e.toString(), stackTrace: st); } return false; diff --git a/lib/core/comments/comments_cubit/comments_state.dart b/lib/core/comments/comments_cubit/comments_state.dart index 0f9c49ea..79fed119 100644 --- a/lib/core/comments/comments_cubit/comments_state.dart +++ b/lib/core/comments/comments_cubit/comments_state.dart @@ -12,6 +12,7 @@ abstract class CommentsState with _$CommentsState { const factory CommentsState.loaded({ required CommentModel comment, int? replyId, + int? editedCommentId, }) = CommentsStateLoaded; const factory CommentsState.error({ diff --git a/lib/core/comments/models/comment_update_model.dart b/lib/core/comments/models/comment_update_model.dart new file mode 100644 index 00000000..dfe26dfe --- /dev/null +++ b/lib/core/comments/models/comment_update_model.dart @@ -0,0 +1,12 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; + +part 'comment_update_model.freezed.dart'; +part 'comment_update_model.g.dart'; + +@freezed +abstract class CommentUpdateModel with _$CommentUpdateModel { + + factory CommentUpdateModel({required String body}) = _CommentUpdateModel; + + factory CommentUpdateModel.fromJson(Map json) => _$CommentUpdateModelFromJson(json); +} \ No newline at end of file diff --git a/lib/core/comments/presentation/comments_actions_bottom_sheet.dart b/lib/core/comments/presentation/comments_actions_bottom_sheet.dart index 2fc9498e..deeea5ff 100644 --- a/lib/core/comments/presentation/comments_actions_bottom_sheet.dart +++ b/lib/core/comments/presentation/comments_actions_bottom_sheet.dart @@ -2,17 +2,20 @@ import 'package:eatvio_app/core/theme/custom_text_style.dart'; import 'package:eatvio_app/core/theme/theme_helper.dart'; import 'package:eatvio_app/core/widgets/bottom_sheet_handler_bar.dart'; import 'package:eatvio_app/core/widgets/custom_divider.dart'; +import 'package:eatvio_app/core/widgets/custom_text_form_field.dart'; import 'package:eatvio_app/generated/l10n.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:go_router/go_router.dart'; void showCommentsActionsBottomSheet( BuildContext context, { required void Function()? reply, required void Function() copy, - required void Function() edit, + required void Function(String text) edit, required void Function() delete, required void Function() complain, + required String commentText, required bool isMyComment, }) { showModalBottomSheet( @@ -32,11 +35,12 @@ void showCommentsActionsBottomSheet( edit: edit, delete: delete, complain: complain, + commentText: commentText, ), )); } -class CommentsActionsBottomSheet extends StatelessWidget { +class CommentsActionsBottomSheet extends StatefulWidget { const CommentsActionsBottomSheet({ super.key, required this.isMyComment, @@ -45,82 +49,140 @@ class CommentsActionsBottomSheet extends StatelessWidget { required this.edit, required this.delete, required this.complain, + required this.commentText, }); final bool isMyComment; final void Function()? reply; final void Function() copy; - final void Function() edit; + final void Function(String text) edit; final void Function() delete; final void Function() complain; + final String commentText; + + @override + State createState() => _CommentsActionsBottomSheetState(); +} + +class _CommentsActionsBottomSheetState extends State { + bool isUserEditingComment = false; + final controller = TextEditingController(); + @override + void initState() { + super.initState(); + controller.text = widget.commentText; + } + @override Widget build(BuildContext context) { final s = S.of(context); return Padding( padding: const EdgeInsets.symmetric(horizontal: 16), - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - const SizedBox( - height: 7, - ), - const BottomSheetHandlerBar(), - if (isMyComment) - _ActionWidget( - text: s.edit, - iconPath: 'assets/icons/edit_paint.svg', - onTap: edit, - ) - else if (reply != null) - _ActionWidget( - text: s.reply, - iconPath: 'assets/icons/reply.svg', - onTap: reply!, - ), - if (reply != null) const CustomDivider(), - _ActionWidget( - text: s.copy, - iconPath: 'assets/icons/copy2.svg', - onTap: copy, - ), - if (isMyComment) ...[ - if (reply != null) ...[ - const CustomDivider(), - _ActionWidget( - text: s.reply, - iconPath: 'assets/icons/reply.svg', - onTap: reply!, + child: isUserEditingComment + ? Padding( + padding: const EdgeInsets.symmetric(vertical: 16.0), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Row(children: [ + Expanded( + child: CustomTextFormField( + autofocus: true, + controller: controller, + onChanged: (value) => setState(() {}), + ), + ), + const SizedBox(width: 8), + GestureDetector( + onTap: () { + widget.edit(controller.text); + GoRouter.of(context).pop(); + }, + child: Container( + decoration: BoxDecoration( + color: controller.text.length > 2 ? appTheme.blueA400 : appTheme.gray50, + shape: BoxShape.circle, + ), + height: 32, + width: 32, + child: Icon( + Icons.send_rounded, + size: 18, + color: controller.text.length > 2 ? appTheme.whiteA700 : appTheme.black, + ), + ), + ) + ]) + ], ), - ], - const CustomDivider(), - _ActionWidget( - text: s.deleteComment, - iconPath: 'assets/icons/red_trash.svg', - onTap: delete, + ) + : Column( + mainAxisSize: MainAxisSize.min, + children: [ + const SizedBox( + height: 7, + ), + const BottomSheetHandlerBar(), + if (widget.isMyComment) + _ActionWidget( + text: s.edit, + iconPath: 'assets/icons/edit_paint.svg', + needToPop: false, + onTap: () => setState(() { + isUserEditingComment = true; + }), + ) + else if (widget.reply != null) + _ActionWidget( + text: s.reply, + iconPath: 'assets/icons/reply.svg', + onTap: widget.reply!, + ), + if (widget.reply != null) const CustomDivider(), + _ActionWidget( + text: s.copy, + iconPath: 'assets/icons/copy2.svg', + onTap: widget.copy, + ), + if (widget.isMyComment) ...[ + if (widget.reply != null) ...[ + const CustomDivider(), + _ActionWidget( + text: s.reply, + iconPath: 'assets/icons/reply.svg', + onTap: widget.reply!, + ), + ], + const CustomDivider(), + _ActionWidget( + text: s.deleteComment, + iconPath: 'assets/icons/red_trash.svg', + onTap: widget.delete, + ), + ] else ...[ + const CustomDivider(), + _ActionWidget( + text: s.complain, + iconPath: 'assets/icons/complain.svg', + onTap: widget.complain, + ), + ] + ], ), - ] else ...[ - const CustomDivider(), - _ActionWidget( - text: s.complain, - iconPath: 'assets/icons/complain.svg', - onTap: complain, - ), - ] - ], - ), ); } } class _ActionWidget extends StatelessWidget { - const _ActionWidget({required this.text, required this.iconPath, required this.onTap}); + const _ActionWidget({required this.text, required this.iconPath, required this.onTap, this.needToPop = true}); final String text; final String iconPath; final void Function() onTap; + final bool needToPop; @override Widget build(BuildContext context) { return GestureDetector( onTap: () { - Navigator.pop(context); + if (needToPop) Navigator.pop(context); onTap(); }, child: Padding( diff --git a/lib/core/comments/repositories/comments_repository.dart b/lib/core/comments/repositories/comments_repository.dart index bc6e654b..096c3690 100644 --- a/lib/core/comments/repositories/comments_repository.dart +++ b/lib/core/comments/repositories/comments_repository.dart @@ -1,3 +1,4 @@ +import 'package:eatvio_app/core/comments/models/comment_update_model.dart'; import 'package:eatvio_app/core/comments/services/comments_service.dart'; class CommentsRepository { @@ -15,7 +16,7 @@ class CommentsRepository { Future updateComment({required int id, required String body}) async { try { - return service.updateComment(id, body); + return service.updateComment(id, CommentUpdateModel(body: body)); } catch (e) { rethrow; } diff --git a/lib/core/comments/services/comments_service.dart b/lib/core/comments/services/comments_service.dart index d2db4218..606f61bd 100644 --- a/lib/core/comments/services/comments_service.dart +++ b/lib/core/comments/services/comments_service.dart @@ -1,4 +1,5 @@ import 'package:dio/dio.dart'; +import 'package:eatvio_app/core/comments/models/comment_update_model.dart'; import 'package:retrofit/retrofit.dart'; part 'comments_service.g.dart'; @@ -7,9 +8,9 @@ part 'comments_service.g.dart'; abstract class CommentsService { factory CommentsService(Dio dio, {String baseUrl}) = _CommentsService; - @DELETE('/api/comments/{comment}') + @DELETE('/api/comments/{id}') Future deleteComment(@Path('id') int id); - @PATCH('/api/comments/{comment}') - Future updateComment(@Path('id') int id, @Body() String body); + @PATCH('/api/comments/{id}') + Future updateComment(@Path('id') int id, @Body() CommentUpdateModel body); } diff --git a/lib/core/const.dart b/lib/core/const.dart index 4f38a54e..31c47e9c 100644 --- a/lib/core/const.dart +++ b/lib/core/const.dart @@ -1,5 +1,5 @@ -// String baseUrl = 'https://eatvio.ru'; -String baseUrl = 'https://dev2.eatvio.com'; +String baseUrl = 'https://eatvio.ru'; +// String baseUrl = 'https://dev2.eatvio.com'; const List durationValues = [7, 14, 21]; diff --git a/lib/features/feed/presentation/widgets/comments_widget.dart b/lib/features/feed/presentation/widgets/comments_widget.dart index d3365b15..e36bdf52 100644 --- a/lib/features/feed/presentation/widgets/comments_widget.dart +++ b/lib/features/feed/presentation/widgets/comments_widget.dart @@ -64,6 +64,7 @@ class _CommentsWidgetState extends State { onTap: () { showCommentsActionsBottomSheet( context, + commentText: comment.body, reply: !isChild ? () { context.read().setReplyId(comment.id); @@ -73,8 +74,12 @@ class _CommentsWidgetState extends State { Clipboard.setData(ClipboardData(text: comment.body)); showInfoToast(s.link_was_copied); }, - edit: () {}, - delete: () {}, + edit: (String text) { + context.read().updateComment(id: comment.id, body: text); + }, + delete: () { + context.read().deleteComment(id: comment.id); + }, complain: () { showModalBottomSheet( context: context,