add likes

This commit is contained in:
mso7nueh
2024-08-19 17:41:09 +03:00
parent 026a11a6ca
commit 3761f25df4
12 changed files with 202 additions and 34 deletions

View File

@@ -53,6 +53,9 @@ class _BlogPostScreenState extends State<BlogPostScreen> {
SizedBox(
width: mediaQueryData.size.width - 64,
child: TextFormField(
onChanged: (_) {
setState(() {});
},
controller: textEditingController,
maxLines: 5,
minLines: 1,
@@ -109,11 +112,12 @@ class _BlogPostScreenState extends State<BlogPostScreen> {
setState(() {
textEditingController.clear();
});
FocusManager.instance.primaryFocus?.unfocus();
}
},
child: Container(
decoration: BoxDecoration(
color: appTheme.blueA400,
color: textEditingController.text.length > 2 ? appTheme.blueA400 : Colors.grey,
shape: BoxShape.circle,
),
height: 32,
@@ -177,7 +181,7 @@ class _BlogPostScreenState extends State<BlogPostScreen> {
),
),
),
SizedBox(height: 12),
if (widget.blogPost.description.isNotEmpty) SizedBox(height: 12),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Align(
@@ -319,6 +323,33 @@ class _BlogPostScreenState extends State<BlogPostScreen> {
color: Colors.black,
),
),
SizedBox(height: 4),
Row(
children: [
Text(
widget.blogPost.createdAt.format('dd MMMM', 'ru'),
style: TextStyle(
color: Colors.grey.shade600,
fontSize: 12,
fontWeight: FontWeight.w400,
height: 1,
),
),
SizedBox(width: 16),
GestureDetector(
onTap: () {},
child: Text(
'Ответить',
style: TextStyle(
height: 1,
fontSize: 12,
color: Colors.grey.shade800,
fontWeight: FontWeight.w500,
),
),
),
],
),
],
),
],

View File

@@ -47,6 +47,9 @@ class ChatSelectScreen extends StatelessWidget {
title: Padding(
padding: const EdgeInsets.only(top: 8),
child: CustomSearchView(
onChanged: (query) {
},
controller: state.searchController,
fillColor: Color(0xFFF0F2F5),
hintText: "Поиск",

View File

@@ -81,6 +81,8 @@ class ChatEntryWidget extends StatelessWidget {
SizedBox(height: 6.v),
Text(
chatEntryObj.greetingText!.trim(),
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: CustomTextStyles.bodyMediumBluegray800,
),
],

View File

@@ -16,12 +16,10 @@ class FeedBlogPostModel with _$FeedBlogPostModel {
@JsonKey(name: "created_at") required DateTime createdAt,
@JsonKey(name: "updated_at") required DateTime updatedAt,
required String title,
@JsonKey(name: "total_likes")
required int totalLikes,
@JsonKey(name: "total_views")
required int totalViews,
@JsonKey(name: "total_comments")
required int totalComments,
@JsonKey(name: "total_likes") required int totalLikes,
@JsonKey(name: "total_views") required int totalViews,
@JsonKey(name: "total_comments") required int totalComments,
@JsonKey(name: "was_liked_by_me") required bool wasLikedByMe,
}) = _FeedBlogPostModel;
factory FeedBlogPostModel.fromJson(Map<String, Object?> json) => _$FeedBlogPostModelFromJson(json);

View File

@@ -38,6 +38,8 @@ mixin _$FeedBlogPostModel {
int get totalViews => throw _privateConstructorUsedError;
@JsonKey(name: "total_comments")
int get totalComments => throw _privateConstructorUsedError;
@JsonKey(name: "was_liked_by_me")
bool get wasLikedByMe => throw _privateConstructorUsedError;
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
@@ -63,7 +65,8 @@ abstract class $FeedBlogPostModelCopyWith<$Res> {
String title,
@JsonKey(name: "total_likes") int totalLikes,
@JsonKey(name: "total_views") int totalViews,
@JsonKey(name: "total_comments") int totalComments});
@JsonKey(name: "total_comments") int totalComments,
@JsonKey(name: "was_liked_by_me") bool wasLikedByMe});
$AuthorCopyWith<$Res> get author;
}
@@ -93,6 +96,7 @@ class _$FeedBlogPostModelCopyWithImpl<$Res, $Val extends FeedBlogPostModel>
Object? totalLikes = null,
Object? totalViews = null,
Object? totalComments = null,
Object? wasLikedByMe = null,
}) {
return _then(_value.copyWith(
id: null == id
@@ -143,6 +147,10 @@ class _$FeedBlogPostModelCopyWithImpl<$Res, $Val extends FeedBlogPostModel>
? _value.totalComments
: totalComments // ignore: cast_nullable_to_non_nullable
as int,
wasLikedByMe: null == wasLikedByMe
? _value.wasLikedByMe
: wasLikedByMe // ignore: cast_nullable_to_non_nullable
as bool,
) as $Val);
}
@@ -175,7 +183,8 @@ abstract class _$$FeedBlogPostModelImplCopyWith<$Res>
String title,
@JsonKey(name: "total_likes") int totalLikes,
@JsonKey(name: "total_views") int totalViews,
@JsonKey(name: "total_comments") int totalComments});
@JsonKey(name: "total_comments") int totalComments,
@JsonKey(name: "was_liked_by_me") bool wasLikedByMe});
@override
$AuthorCopyWith<$Res> get author;
@@ -204,6 +213,7 @@ class __$$FeedBlogPostModelImplCopyWithImpl<$Res>
Object? totalLikes = null,
Object? totalViews = null,
Object? totalComments = null,
Object? wasLikedByMe = null,
}) {
return _then(_$FeedBlogPostModelImpl(
id: null == id
@@ -254,6 +264,10 @@ class __$$FeedBlogPostModelImplCopyWithImpl<$Res>
? _value.totalComments
: totalComments // ignore: cast_nullable_to_non_nullable
as int,
wasLikedByMe: null == wasLikedByMe
? _value.wasLikedByMe
: wasLikedByMe // ignore: cast_nullable_to_non_nullable
as bool,
));
}
}
@@ -273,7 +287,8 @@ class _$FeedBlogPostModelImpl implements _FeedBlogPostModel {
required this.title,
@JsonKey(name: "total_likes") required this.totalLikes,
@JsonKey(name: "total_views") required this.totalViews,
@JsonKey(name: "total_comments") required this.totalComments});
@JsonKey(name: "total_comments") required this.totalComments,
@JsonKey(name: "was_liked_by_me") required this.wasLikedByMe});
factory _$FeedBlogPostModelImpl.fromJson(Map<String, dynamic> json) =>
_$$FeedBlogPostModelImplFromJson(json);
@@ -308,10 +323,13 @@ class _$FeedBlogPostModelImpl implements _FeedBlogPostModel {
@override
@JsonKey(name: "total_comments")
final int totalComments;
@override
@JsonKey(name: "was_liked_by_me")
final bool wasLikedByMe;
@override
String toString() {
return 'FeedBlogPostModel(id: $id, state: $state, image: $image, description: $description, author: $author, youtubeUrl: $youtubeUrl, createdAt: $createdAt, updatedAt: $updatedAt, title: $title, totalLikes: $totalLikes, totalViews: $totalViews, totalComments: $totalComments)';
return 'FeedBlogPostModel(id: $id, state: $state, image: $image, description: $description, author: $author, youtubeUrl: $youtubeUrl, createdAt: $createdAt, updatedAt: $updatedAt, title: $title, totalLikes: $totalLikes, totalViews: $totalViews, totalComments: $totalComments, wasLikedByMe: $wasLikedByMe)';
}
@override
@@ -337,7 +355,9 @@ class _$FeedBlogPostModelImpl implements _FeedBlogPostModel {
(identical(other.totalViews, totalViews) ||
other.totalViews == totalViews) &&
(identical(other.totalComments, totalComments) ||
other.totalComments == totalComments));
other.totalComments == totalComments) &&
(identical(other.wasLikedByMe, wasLikedByMe) ||
other.wasLikedByMe == wasLikedByMe));
}
@JsonKey(ignore: true)
@@ -355,7 +375,8 @@ class _$FeedBlogPostModelImpl implements _FeedBlogPostModel {
title,
totalLikes,
totalViews,
totalComments);
totalComments,
wasLikedByMe);
@JsonKey(ignore: true)
@override
@@ -385,7 +406,8 @@ abstract class _FeedBlogPostModel implements FeedBlogPostModel {
required final String title,
@JsonKey(name: "total_likes") required final int totalLikes,
@JsonKey(name: "total_views") required final int totalViews,
@JsonKey(name: "total_comments") required final int totalComments}) =
@JsonKey(name: "total_comments") required final int totalComments,
@JsonKey(name: "was_liked_by_me") required final bool wasLikedByMe}) =
_$FeedBlogPostModelImpl;
factory _FeedBlogPostModel.fromJson(Map<String, dynamic> json) =
@@ -422,6 +444,9 @@ abstract class _FeedBlogPostModel implements FeedBlogPostModel {
@JsonKey(name: "total_comments")
int get totalComments;
@override
@JsonKey(name: "was_liked_by_me")
bool get wasLikedByMe;
@override
@JsonKey(ignore: true)
_$$FeedBlogPostModelImplCopyWith<_$FeedBlogPostModelImpl> get copyWith =>
throw _privateConstructorUsedError;

View File

@@ -21,6 +21,7 @@ _$FeedBlogPostModelImpl _$$FeedBlogPostModelImplFromJson(
totalLikes: (json['total_likes'] as num).toInt(),
totalViews: (json['total_views'] as num).toInt(),
totalComments: (json['total_comments'] as num).toInt(),
wasLikedByMe: json['was_liked_by_me'] as bool,
);
Map<String, dynamic> _$$FeedBlogPostModelImplToJson(
@@ -38,4 +39,5 @@ Map<String, dynamic> _$$FeedBlogPostModelImplToJson(
'total_likes': instance.totalLikes,
'total_views': instance.totalViews,
'total_comments': instance.totalComments,
'was_liked_by_me': instance.wasLikedByMe,
};

View File

@@ -4,8 +4,8 @@ import 'package:eatvio_app/features/feed/repositories/feed_repository.dart';
class FeedCubit extends Cubit<FeedState> {
final FeedRepository feedRepository;
int currentPage = 1;
final int lastPage = 7;
int _currentPage = 1;
final int _lastPage = 7;
bool isLoading = false;
FeedCubit({required this.feedRepository}) : super(const FeedState.loading());
@@ -27,9 +27,9 @@ class FeedCubit extends Cubit<FeedState> {
loadNextPage() async {
print('start');
try {
if (lastPage < currentPage) {
currentPage++;
final feedBlogPosts = await feedRepository.getFeedBlogPosts(page: currentPage);
if (_lastPage < _currentPage) {
_currentPage++;
final feedBlogPosts = await feedRepository.getFeedBlogPosts(page: _currentPage);
emit(
FeedState.loaded(
feedBlogPostList: [
@@ -47,4 +47,8 @@ class FeedCubit extends Cubit<FeedState> {
print('end');
}
}
likePost(int id) async => await feedRepository.likeBlogPost(id);
unlikedPost(int id) async => await feedRepository.unlikedBlogPost(id);
}

View File

@@ -3,6 +3,7 @@ import 'package:dismissible_page/dismissible_page.dart';
import 'package:eatvio_app/core/app_export.dart';
import 'package:eatvio_app/features/chat/presentation/pages/photo_view_screen.dart';
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:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:go_router/go_router.dart';
@@ -18,6 +19,16 @@ class BlogPostWidget extends StatefulWidget {
}
class _BlogPostWidgetState extends State<BlogPostWidget> {
late bool isLiked;
late int totalLikes;
@override
void initState() {
isLiked = widget.feedBlogPostModel.wasLikedByMe;
totalLikes = widget.feedBlogPostModel.totalLikes;
super.initState();
}
@override
Widget build(BuildContext context) {
return InkWell(
@@ -106,7 +117,23 @@ class _BlogPostWidgetState extends State<BlogPostWidget> {
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Row(
children: [
_iconButton(icon: Icons.favorite_border_rounded, text: widget.feedBlogPostModel.totalLikes.toString()),
_iconButton(
icon: Icons.favorite_border_rounded,
text: totalLikes.toString(),
iconColor: isLiked ? Colors.red.withOpacity(0.8) : null,
backgroundColor: isLiked ? Colors.red.withOpacity(0.1) : null,
textColor: isLiked ? Colors.red.withOpacity(0.8) : null,
onTap: () async {
setState(() {
isLiked = !isLiked;
isLiked ? totalLikes++ : totalLikes--;
});
isLiked
? await context.read<FeedCubit>().likePost(widget.feedBlogPostModel.id)
: await context.read<FeedCubit>().unlikedPost(widget.feedBlogPostModel.id);
await context.read<FeedCubit>().loadFeedBlogPostList();
},
),
SizedBox(width: 8),
_iconButton(
icon: Icons.mode_comment_outlined,
@@ -150,14 +177,14 @@ class _BlogPostWidgetState extends State<BlogPostWidget> {
);
}
_iconButton({required IconData icon, String? text, Function()? onTap}) {
_iconButton({required IconData icon, String? text, Function()? onTap, Color? iconColor, Color? backgroundColor, Color? textColor}) {
return InkWell(
onTap: onTap ?? () {},
borderRadius: BorderRadius.circular(16),
child: Ink(
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 4),
decoration: BoxDecoration(
color: appTheme.gray100,
color: backgroundColor ?? appTheme.gray100,
borderRadius: BorderRadius.circular(16),
),
height: 32,
@@ -167,14 +194,14 @@ class _BlogPostWidgetState extends State<BlogPostWidget> {
Icon(
icon,
size: 20,
color: appTheme.gray600,
color: iconColor ?? appTheme.gray600,
),
if (text != null) SizedBox(width: 8),
if (text != null)
Text(
text,
style: TextStyle(
color: appTheme.gray600,
color: textColor ?? appTheme.gray600,
fontWeight: FontWeight.w500,
),
),

View File

@@ -56,4 +56,20 @@ class FeedRepository {
rethrow;
}
}
Future<void> likeBlogPost(int id) async {
try {
await feedService.likeBlogPost(id: id);
} catch (e) {
rethrow;
}
}
Future<void> unlikedBlogPost(int id) async {
try {
await feedService.unlikedBlogPost(id: id);
} catch (e) {
rethrow;
}
}
}

View File

@@ -26,11 +26,21 @@ abstract class FeedService {
Future<Comment> getBlogPostComments({
@Path() required int id,
});
@POST('/api/blogPosts/{id}/comment/create')
Future<void> sendBlogPostComment({
@Path() required int id,
@Part() required String body,
@Part() int? parentCommentId,
});
@POST('/api/blogPosts/{id}/like')
Future<void> likeBlogPost({
@Path() required int id,
});
@POST('/api/blogPosts/{id}/unliked')
Future<void> unlikedBlogPost({
@Path() required int id,
});
}

View File

@@ -193,6 +193,56 @@ class _FeedService implements FeedService {
await _dio.fetch<void>(_options);
}
@override
Future<void> likeBlogPost({required int id}) 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/blogPosts/${id}/like',
queryParameters: queryParameters,
data: _data,
)
.copyWith(
baseUrl: _combineBaseUrls(
_dio.options.baseUrl,
baseUrl,
)));
await _dio.fetch<void>(_options);
}
@override
Future<void> unlikedBlogPost({required int id}) 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/blogPosts/${id}/unliked',
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 ||