Skip to content

Commit 9c37ae9

Browse files
committed
refactor(user_content): improve code structure in comments bottom sheet
- Remove line breaks from constructor parameters - Reorder code lines to enhance readability - Introduce local variables for user and locale - Remove unnecessary watches and selects
1 parent eb8fa43 commit 9c37ae9

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

lib/user_content/engagement/view/comments_bottom_sheet.dart

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ import 'package:ui_kit/ui_kit.dart';
1515
/// {@endtemplate}
1616
class CommentsBottomSheet extends StatelessWidget {
1717
/// {@macro comments_bottom_sheet}
18-
const CommentsBottomSheet({
19-
required this.headlineId,
20-
super.key,
21-
});
18+
const CommentsBottomSheet({required this.headlineId, super.key});
2219

2320
/// The ID of the headline for which comments are being displayed.
2421
final String headlineId;
@@ -34,9 +31,7 @@ class CommentsBottomSheet extends StatelessWidget {
3431
}
3532

3633
class _CommentsBottomSheetView extends StatefulWidget {
37-
const _CommentsBottomSheetView({
38-
required this.headlineId,
39-
});
34+
const _CommentsBottomSheetView({required this.headlineId});
4035
// A key to manage the state of the input field, allowing parent widgets
4136
// to trigger actions like editing.
4237
static final _inputFieldKey = GlobalKey<__CommentInputFieldState>();
@@ -96,6 +91,9 @@ class __CommentsBottomSheetViewState extends State<_CommentsBottomSheetView> {
9691
final theme = Theme.of(context);
9792
final l10n = AppLocalizations.of(context);
9893

94+
final user = context.select((AppBloc bloc) => bloc.state.user);
95+
final currentLocale = context.watch<AppBloc>().state.locale;
96+
9997
final engagements = state.engagementsMap[widget.headlineId] ?? [];
10098
final comments = engagements.where((e) => e.comment != null).toList()
10199
..sort((a, b) => b.createdAt.compareTo(a.createdAt));
@@ -122,13 +120,11 @@ class __CommentsBottomSheetViewState extends State<_CommentsBottomSheetView> {
122120
itemBuilder: (context, index) {
123121
final engagement = comments[index];
124122
final comment = engagement.comment!;
125-
final currentLocale = context.watch<AppBloc>().state.locale;
126123
final formattedDate = timeago.format(
127124
engagement.updatedAt,
128125
locale: currentLocale.languageCode,
129126
);
130127

131-
final user = context.select((AppBloc bloc) => bloc.state.user);
132128
final isOwnComment = user != null && engagement.userId == user.id;
133129

134130
return ListTile(
@@ -183,10 +179,7 @@ class __CommentsBottomSheetViewState extends State<_CommentsBottomSheetView> {
183179
}
184180

185181
class _CommentInputField extends StatefulWidget {
186-
const _CommentInputField({
187-
required this.headlineId,
188-
super.key,
189-
});
182+
const _CommentInputField({required this.headlineId, super.key});
190183

191184
final String headlineId;
192185

@@ -221,8 +214,11 @@ class __CommentInputFieldState extends State<_CommentInputField> {
221214
if (user == null) return;
222215

223216
final engagements =
224-
context.read<HeadlinesFeedBloc>().state.engagementsMap[widget.headlineId] ??
225-
[];
217+
context
218+
.read<HeadlinesFeedBloc>()
219+
.state
220+
.engagementsMap[widget.headlineId] ??
221+
[];
226222
final userEngagement = engagements.firstWhereOrNull(
227223
(e) => e.userId == user.id,
228224
);
@@ -273,11 +269,11 @@ class __CommentInputFieldState extends State<_CommentInputField> {
273269
decoration: InputDecoration(
274270
hintText: isEnabled
275271
? (_isEditing
276-
? l10n.commentEditButtonLabel
277-
: l10n.commentInputHint)
272+
? l10n.commentEditButtonLabel
273+
: l10n.commentInputHint)
278274
: (isGuest
279-
? l10n.commentInputDisabledHint
280-
: l10n.commentInputExistingHint),
275+
? l10n.commentInputDisabledHint
276+
: l10n.commentInputExistingHint),
281277
border: const OutlineInputBorder(
282278
borderRadius: BorderRadius.all(Radius.circular(24)),
283279
),
@@ -298,20 +294,20 @@ class __CommentInputFieldState extends State<_CommentInputField> {
298294
? () {
299295
if (_isEditing) {
300296
context.read<HeadlinesFeedBloc>().add(
301-
HeadlinesFeedCommentUpdated(
302-
widget.headlineId,
303-
_controller.text,
304-
context: context,
305-
),
306-
);
297+
HeadlinesFeedCommentUpdated(
298+
widget.headlineId,
299+
_controller.text,
300+
context: context,
301+
),
302+
);
307303
} else {
308304
context.read<HeadlinesFeedBloc>().add(
309-
HeadlinesFeedCommentPosted(
310-
widget.headlineId,
311-
_controller.text,
312-
context: context,
313-
),
314-
);
305+
HeadlinesFeedCommentPosted(
306+
widget.headlineId,
307+
_controller.text,
308+
context: context,
309+
),
310+
);
315311
}
316312
resetAfterSubmit();
317313
}

0 commit comments

Comments
 (0)