refactor: scroll

This commit is contained in:
mso7nueh
2024-09-09 12:58:25 +03:00
parent f9c10047b2
commit 3ccf5b6d92
31 changed files with 610 additions and 608 deletions

View File

@@ -1,5 +1,6 @@
import 'package:eatvio_app/core/app_export.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/comments_cubit/comments_state.dart';
import 'package:eatvio_app/features/feed/presentation/widgets/comments_widget.dart';
import 'package:eatvio_app/features/feed/repositories/feed_repository.dart';
class CommentsCubit extends Cubit<CommentsState> {
@@ -9,79 +10,60 @@ class CommentsCubit extends Cubit<CommentsState> {
required this.feedRepository,
}) : super(CommentsState());
getBlogPostComments(int id) async {
emit(CommentsState.loading());
setReplyId(int? replyId) {
emit((state as CommentsStateLoaded).copyWith(replyId: replyId));
}
getComments(String id, CommentSourceType commentSourceType, [bool isFirstFetch = false]) async {
if (isFirstFetch) emit(CommentsState.loading());
try {
emit(CommentsState.loaded(comment: await feedRepository.getBlogPostComments(id)));
switch (commentSourceType) {
case CommentSourceType.blogPost:
emit(CommentsState.loaded(comment: await feedRepository.getBlogPostComments(id)));
break;
case CommentSourceType.diet:
emit(CommentsState.loaded(comment: await feedRepository.getDietsComments(id)));
break;
case CommentSourceType.recipe:
emit(CommentsState.loaded(comment: await feedRepository.getRecipesComments(id)));
break;
}
} catch (e) {
emit(CommentsState.error(errorMessage: e.toString()));
}
}
getRecipesComments(String id) async {
emit(CommentsState.loading());
try {
emit(CommentsState.loaded(comment: await feedRepository.getRecipesComments(id)));
} catch (e) {
emit(CommentsState.error(errorMessage: e.toString()));
}
}
getDietsComments(int id) async {
emit(CommentsState.loading());
try {
emit(CommentsState.loaded(comment: await feedRepository.getDietsComments(id)));
} catch (e) {
emit(CommentsState.error(errorMessage: e.toString()));
}
}
sendBlogPostComment({
required int id,
required String commentText,
int? parentCommentId,
}) async {
try {
await feedRepository.sendBlogPostComment(
id: id,
commentText: commentText,
parentCommentId: parentCommentId,
);
emit(CommentsState.loaded(comment: await feedRepository.getBlogPostComments(id)));
} catch (e) {
emit(CommentsState.error(errorMessage: e.toString()));
}
}
sendRecipesComment({
sendComment({
required String id,
required String commentText,
required CommentSourceType commentSourceType,
int? parentCommentId,
}) async {
try {
await feedRepository.sendRecipesComment(
id: id,
commentText: commentText,
parentCommentId: parentCommentId,
);
emit(CommentsState.loaded(comment: await feedRepository.getRecipesComments(id)));
} catch (e) {
emit(CommentsState.error(errorMessage: e.toString()));
}
}
sendDietsComment({
required int id,
required String commentText,
int? parentCommentId,
}) async {
try {
await feedRepository.sendDietsComment(
id: id,
commentText: commentText,
parentCommentId: parentCommentId,
);
emit(CommentsState.loaded(comment: await feedRepository.getDietsComments(id)));
switch (commentSourceType) {
case CommentSourceType.blogPost:
await feedRepository.sendBlogPostComment(
id: id,
commentText: commentText,
parentCommentId: parentCommentId,
);
break;
case CommentSourceType.diet:
await feedRepository.sendDietsComment(
id: id,
commentText: commentText,
parentCommentId: parentCommentId,
);
break;
case CommentSourceType.recipe:
await feedRepository.sendRecipesComment(
id: id,
commentText: commentText,
parentCommentId: parentCommentId,
);
break;
}
getComments(id, commentSourceType);
} catch (e) {
emit(CommentsState.error(errorMessage: e.toString()));
}

View File

@@ -11,6 +11,7 @@ abstract class CommentsState with _$CommentsState {
const factory CommentsState.loaded({
required CommentModel comment,
int? replyId,
}) = CommentsStateLoaded;
const factory CommentsState.error({

View File

@@ -20,7 +20,7 @@ mixin _$CommentsState {
TResult when<TResult extends Object?>(
TResult Function() $default, {
required TResult Function() loading,
required TResult Function(CommentModel comment) loaded,
required TResult Function(CommentModel comment, int? replyId) loaded,
required TResult Function(String errorMessage) error,
}) =>
throw _privateConstructorUsedError;
@@ -28,7 +28,7 @@ mixin _$CommentsState {
TResult? whenOrNull<TResult extends Object?>(
TResult? Function()? $default, {
TResult? Function()? loading,
TResult? Function(CommentModel comment)? loaded,
TResult? Function(CommentModel comment, int? replyId)? loaded,
TResult? Function(String errorMessage)? error,
}) =>
throw _privateConstructorUsedError;
@@ -36,7 +36,7 @@ mixin _$CommentsState {
TResult maybeWhen<TResult extends Object?>(
TResult Function()? $default, {
TResult Function()? loading,
TResult Function(CommentModel comment)? loaded,
TResult Function(CommentModel comment, int? replyId)? loaded,
TResult Function(String errorMessage)? error,
required TResult orElse(),
}) =>
@@ -126,7 +126,7 @@ class _$CommentsStateImpl implements _CommentsState {
TResult when<TResult extends Object?>(
TResult Function() $default, {
required TResult Function() loading,
required TResult Function(CommentModel comment) loaded,
required TResult Function(CommentModel comment, int? replyId) loaded,
required TResult Function(String errorMessage) error,
}) {
return $default();
@@ -137,7 +137,7 @@ class _$CommentsStateImpl implements _CommentsState {
TResult? whenOrNull<TResult extends Object?>(
TResult? Function()? $default, {
TResult? Function()? loading,
TResult? Function(CommentModel comment)? loaded,
TResult? Function(CommentModel comment, int? replyId)? loaded,
TResult? Function(String errorMessage)? error,
}) {
return $default?.call();
@@ -148,7 +148,7 @@ class _$CommentsStateImpl implements _CommentsState {
TResult maybeWhen<TResult extends Object?>(
TResult Function()? $default, {
TResult Function()? loading,
TResult Function(CommentModel comment)? loaded,
TResult Function(CommentModel comment, int? replyId)? loaded,
TResult Function(String errorMessage)? error,
required TResult orElse(),
}) {
@@ -241,7 +241,7 @@ class _$CommentsStateLoadingImpl implements CommentsStateLoading {
TResult when<TResult extends Object?>(
TResult Function() $default, {
required TResult Function() loading,
required TResult Function(CommentModel comment) loaded,
required TResult Function(CommentModel comment, int? replyId) loaded,
required TResult Function(String errorMessage) error,
}) {
return loading();
@@ -252,7 +252,7 @@ class _$CommentsStateLoadingImpl implements CommentsStateLoading {
TResult? whenOrNull<TResult extends Object?>(
TResult? Function()? $default, {
TResult? Function()? loading,
TResult? Function(CommentModel comment)? loaded,
TResult? Function(CommentModel comment, int? replyId)? loaded,
TResult? Function(String errorMessage)? error,
}) {
return loading?.call();
@@ -263,7 +263,7 @@ class _$CommentsStateLoadingImpl implements CommentsStateLoading {
TResult maybeWhen<TResult extends Object?>(
TResult Function()? $default, {
TResult Function()? loading,
TResult Function(CommentModel comment)? loaded,
TResult Function(CommentModel comment, int? replyId)? loaded,
TResult Function(String errorMessage)? error,
required TResult orElse(),
}) {
@@ -321,7 +321,7 @@ abstract class _$$CommentsStateLoadedImplCopyWith<$Res> {
$Res Function(_$CommentsStateLoadedImpl) then) =
__$$CommentsStateLoadedImplCopyWithImpl<$Res>;
@useResult
$Res call({CommentModel comment});
$Res call({CommentModel comment, int? replyId});
$CommentModelCopyWith<$Res> get comment;
}
@@ -338,12 +338,17 @@ class __$$CommentsStateLoadedImplCopyWithImpl<$Res>
@override
$Res call({
Object? comment = null,
Object? replyId = freezed,
}) {
return _then(_$CommentsStateLoadedImpl(
comment: null == comment
? _value.comment
: comment // ignore: cast_nullable_to_non_nullable
as CommentModel,
replyId: freezed == replyId
? _value.replyId
: replyId // ignore: cast_nullable_to_non_nullable
as int?,
));
}
@@ -359,14 +364,16 @@ class __$$CommentsStateLoadedImplCopyWithImpl<$Res>
/// @nodoc
class _$CommentsStateLoadedImpl implements CommentsStateLoaded {
const _$CommentsStateLoadedImpl({required this.comment});
const _$CommentsStateLoadedImpl({required this.comment, this.replyId});
@override
final CommentModel comment;
@override
final int? replyId;
@override
String toString() {
return 'CommentsState.loaded(comment: $comment)';
return 'CommentsState.loaded(comment: $comment, replyId: $replyId)';
}
@override
@@ -374,11 +381,12 @@ class _$CommentsStateLoadedImpl implements CommentsStateLoaded {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$CommentsStateLoadedImpl &&
(identical(other.comment, comment) || other.comment == comment));
(identical(other.comment, comment) || other.comment == comment) &&
(identical(other.replyId, replyId) || other.replyId == replyId));
}
@override
int get hashCode => Object.hash(runtimeType, comment);
int get hashCode => Object.hash(runtimeType, comment, replyId);
@JsonKey(ignore: true)
@override
@@ -392,10 +400,10 @@ class _$CommentsStateLoadedImpl implements CommentsStateLoaded {
TResult when<TResult extends Object?>(
TResult Function() $default, {
required TResult Function() loading,
required TResult Function(CommentModel comment) loaded,
required TResult Function(CommentModel comment, int? replyId) loaded,
required TResult Function(String errorMessage) error,
}) {
return loaded(comment);
return loaded(comment, replyId);
}
@override
@@ -403,10 +411,10 @@ class _$CommentsStateLoadedImpl implements CommentsStateLoaded {
TResult? whenOrNull<TResult extends Object?>(
TResult? Function()? $default, {
TResult? Function()? loading,
TResult? Function(CommentModel comment)? loaded,
TResult? Function(CommentModel comment, int? replyId)? loaded,
TResult? Function(String errorMessage)? error,
}) {
return loaded?.call(comment);
return loaded?.call(comment, replyId);
}
@override
@@ -414,12 +422,12 @@ class _$CommentsStateLoadedImpl implements CommentsStateLoaded {
TResult maybeWhen<TResult extends Object?>(
TResult Function()? $default, {
TResult Function()? loading,
TResult Function(CommentModel comment)? loaded,
TResult Function(CommentModel comment, int? replyId)? loaded,
TResult Function(String errorMessage)? error,
required TResult orElse(),
}) {
if (loaded != null) {
return loaded(comment);
return loaded(comment, replyId);
}
return orElse();
}
@@ -463,10 +471,12 @@ class _$CommentsStateLoadedImpl implements CommentsStateLoaded {
}
abstract class CommentsStateLoaded implements CommentsState {
const factory CommentsStateLoaded({required final CommentModel comment}) =
_$CommentsStateLoadedImpl;
const factory CommentsStateLoaded(
{required final CommentModel comment,
final int? replyId}) = _$CommentsStateLoadedImpl;
CommentModel get comment;
int? get replyId;
@JsonKey(ignore: true)
_$$CommentsStateLoadedImplCopyWith<_$CommentsStateLoadedImpl> get copyWith =>
throw _privateConstructorUsedError;
@@ -540,7 +550,7 @@ class _$CommentsStateErrorImpl implements CommentsStateError {
TResult when<TResult extends Object?>(
TResult Function() $default, {
required TResult Function() loading,
required TResult Function(CommentModel comment) loaded,
required TResult Function(CommentModel comment, int? replyId) loaded,
required TResult Function(String errorMessage) error,
}) {
return error(errorMessage);
@@ -551,7 +561,7 @@ class _$CommentsStateErrorImpl implements CommentsStateError {
TResult? whenOrNull<TResult extends Object?>(
TResult? Function()? $default, {
TResult? Function()? loading,
TResult? Function(CommentModel comment)? loaded,
TResult? Function(CommentModel comment, int? replyId)? loaded,
TResult? Function(String errorMessage)? error,
}) {
return error?.call(errorMessage);
@@ -562,7 +572,7 @@ class _$CommentsStateErrorImpl implements CommentsStateError {
TResult maybeWhen<TResult extends Object?>(
TResult Function()? $default, {
TResult Function()? loading,
TResult Function(CommentModel comment)? loaded,
TResult Function(CommentModel comment, int? replyId)? loaded,
TResult Function(String errorMessage)? error,
required TResult orElse(),
}) {