Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@
.idea
.DS_Store

# Build artifacts
# Build Artifacts
build
release

# Flutter/Dart
# Flutter
.dart_tool
.flutter-plugins-dependencies

# AI
.agents
.cursor
.claude
.github/instructions
.github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:thunder/src/features/account/account.dart';
import 'package:thunder/src/features/comment/comment.dart';
import 'package:thunder/src/foundation/primitives/primitives.dart';
import 'package:thunder/src/features/post/post.dart';
import 'package:thunder/src/features/settings/api.dart';
import 'package:thunder/src/features/comment/api.dart';
import 'package:thunder/src/features/comment/presentation/widgets/comment_card/comment_card_replies_loader.dart';
import 'package:thunder/src/features/comment/presentation/widgets/comment_card/comment_card_surface.dart';
import 'package:thunder/src/shared/gestures/swipe_utils.dart';
import 'package:thunder/packages/ui/ui.dart' show ThunderMultiActionDismissible, ThunderSwipeAction;
import 'package:thunder/packages/ui/ui.dart';

/// A widget that displays a given comment.
///
Expand Down Expand Up @@ -71,31 +71,19 @@ class CommentCard extends StatefulWidget {
}

class _CommentCardState extends State<CommentCard> {
/// The internal comment. This is updated whenever the comment is updated.
late ThunderComment comment;

/// Whether the comment is owned by the current user
late bool isOwnComment;

/// Whether we should display the comment's raw markdown source
bool viewSource = false;

/// Whether the comment is being dragged.
bool _dragged = false;

@override
void initState() {
super.initState();

comment = widget.comment;
isOwnComment = comment.creatorId == widget.account.userId;
}
bool get _isOwnComment => widget.comment.creatorId == widget.account.userId;

/// Maps a [SwipeAction] to a [CommentAction] and performs the action
///
/// If [resolve] is true, the [SwipeAction.reply] will be resolved to [SwipeAction.edit] if the comment is owned by the current user.
Future<void> _onAction(SwipeAction action, {bool resolve = true}) async {
final resolvedSwipeAction = (action == SwipeAction.reply && isOwnComment && resolve) ? SwipeAction.edit : action;
final resolvedSwipeAction = (action == SwipeAction.reply && _isOwnComment && resolve) ? SwipeAction.edit : action;

final commentAction = switch (resolvedSwipeAction) {
SwipeAction.upvote => CommentAction.vote,
Expand All @@ -112,7 +100,7 @@ class _CommentCardState extends State<CommentCard> {
context,
widget.account,
commentAction,
comment,
widget.comment,
{
'voteType': resolvedSwipeAction == SwipeAction.upvote
? 1
Expand All @@ -127,7 +115,6 @@ class _CommentCardState extends State<CommentCard> {
return;
} else if (updatedComment != null) {
widget.onCommentUpdated?.call(updatedComment);
setState(() => comment = updatedComment);
}
}

Expand All @@ -136,15 +123,16 @@ class _CommentCardState extends State<CommentCard> {

showCommentActionBottomModalSheet(
context,
comment,
widget.comment,
isShowingSource: viewSource,
onAction: ({commentAction, communityAction, userAction, comment}) async {
if (comment != null) {
widget.onCommentUpdated?.call(comment);
setState(() => this.comment = comment);
}

if (commentAction == CommentAction.viewSource) return setState(() => viewSource = !viewSource);
if (commentAction == CommentAction.viewSource) {
return setState(() => viewSource = !viewSource);
}

// TODO: Move these into the comment bottom sheet logic
if (commentAction == CommentAction.reply || commentAction == CommentAction.edit) {
Expand All @@ -168,11 +156,6 @@ class _CommentCardState extends State<CommentCard> {

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);

final nestedCommentIndicatorStyle = context.select<CommentPreferencesCubit, NestedCommentIndicatorStyle>((cubit) => cubit.state.nestedCommentIndicatorStyle);
final nestedCommentIndicatorColor = context.select<CommentPreferencesCubit, NestedCommentIndicatorColor>((cubit) => cubit.state.nestedCommentIndicatorColor);

final showCommentButtonActions = context.select<CommentPreferencesCubit, bool>((cubit) => cubit.state.showCommentButtonActions);
final enableCommentGestures = context.select<GesturePreferencesCubit, bool>((cubit) => cubit.state.enableCommentGestures);
final leftPrimaryCommentGesture = context.select<GesturePreferencesCubit, SwipeAction>((cubit) => cubit.state.leftPrimaryCommentGesture);
Expand All @@ -186,44 +169,25 @@ class _CommentCardState extends State<CommentCard> {

final currentSwipeDirection = determineCommentSwipeDirection(!widget.account.anonymous, enableCommentGestures, leftActions, rightActions);

Widget child = Material(
color: widget.highlight ? theme.highlightColor : null,
child: Container(
decoration: _dragged ? null : CommentDepthIndicatorDecoration(context, level: widget.level, style: nestedCommentIndicatorStyle, scheme: nestedCommentIndicatorColor),
child: InkWell(
onTap: () => widget.onCollapse?.call(comment.id, !widget.collapsed),
onLongPress: _onLongPress,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CommentContent(
account: widget.account,
level: widget.level,
comment: comment,
hidden: widget.collapsed,
viewSource: viewSource,
onViewSourceToggled: () => setState(() => viewSource = !viewSource),
),
if (showCommentButtonActions && !widget.account.anonymous && !widget.collapsed)
Padding(
padding: const EdgeInsets.only(bottom: 4.0, top: 6.0, right: 4.0),
child: CommentCardButtonActions(
account: widget.account,
comment: comment,
isOwnComment: isOwnComment,
onAction: (action) => _onAction(action),
onBottomSheetOpen: _onLongPress,
),
),
],
),
),
),
Widget child = CommentCardSurface(
account: widget.account,
comment: widget.comment,
level: widget.level,
collapsed: widget.collapsed,
highlighted: widget.highlight,
dragged: _dragged,
viewSource: viewSource,
showActions: showCommentButtonActions && !widget.account.anonymous && !widget.collapsed,
isOwnComment: _isOwnComment,
onCollapse: () => widget.onCollapse?.call(widget.comment.id, !widget.collapsed),
onLongPress: _onLongPress,
onViewSourceToggled: () => setState(() => viewSource = !viewSource),
onAction: _onAction,
);

if (currentSwipeDirection != DismissDirection.none) {
child = ThunderMultiActionDismissible<SwipeAction>(
key: ObjectKey(comment.id),
key: ObjectKey(widget.comment.id),
direction: currentSwipeDirection,
leftActions: leftActions.map((action) => ThunderSwipeAction(value: action, icon: action.getIcon(), color: (context) => action.getColor(context))).toList(),
rightActions: rightActions.map((action) => ThunderSwipeAction(value: action, icon: action.getIcon(), color: (context) => action.getColor(context))).toList(),
Expand All @@ -235,7 +199,7 @@ class _CommentCardState extends State<CommentCard> {
},
onAction: (action) => _onAction(action.value),
backgroundBuilder: (context, dismissDirection, progress, action) => CommentCardBackground(
swipeAction: action?.value == SwipeAction.reply && isOwnComment ? SwipeAction.edit : action?.value,
swipeAction: action?.value == SwipeAction.reply && _isOwnComment ? SwipeAction.edit : action?.value,
dismissThreshold: progress,
firstActionThreshold: actionThresholds.first,
dismissDirection: dismissDirection,
Expand All @@ -252,18 +216,13 @@ class _CommentCardState extends State<CommentCard> {
mainAxisAlignment: MainAxisAlignment.start,
children: [
child,
if (widget.replies == 0 && comment.childCount! > 0 && !widget.hideReplyCount)
AnimatedCrossFade(
duration: Duration(milliseconds: 350),
sizeCurve: Curves.easeInOutCubicEmphasized,
firstChild: SizedBox(width: MediaQuery.sizeOf(context).width),
secondChild: AdditionalCommentCard(
depth: widget.level,
replies: comment.childCount!,
onTap: () => context.read<PostBloc>().add(GetPostCommentsEvent(commentParentId: comment.id)),
),
crossFadeState: widget.collapsed ? CrossFadeState.showFirst : CrossFadeState.showSecond,
)
CommentCardRepliesLoader(
comment: widget.comment,
level: widget.level,
replies: widget.replies,
collapsed: widget.collapsed,
hideReplyCount: widget.hideReplyCount,
),
],
),
crossFadeState: widget.hidden ? CrossFadeState.showFirst : CrossFadeState.showSecond,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:flutter/material.dart';

import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:thunder/src/features/comment/comment.dart';
import 'package:thunder/src/features/post/post.dart';

/// Displays the "load replies" action for comments with additional children.
class CommentCardRepliesLoader extends StatelessWidget {
const CommentCardRepliesLoader({super.key, required this.comment, required this.level, required this.replies, required this.collapsed, required this.hideReplyCount});

/// Comment whose replies may be loaded.
final ThunderComment comment;

/// Nesting depth used for the additional-replies card.
final int level;

/// Number of currently loaded replies.
final int replies;

/// Whether the comment is collapsed.
final bool collapsed;

/// Whether the reply loader should be suppressed.
final bool hideReplyCount;

@override
Widget build(BuildContext context) {
if (replies != 0 || (comment.childCount ?? 0) <= 0 || hideReplyCount) {
return const SizedBox.shrink();
}

return AnimatedCrossFade(
duration: const Duration(milliseconds: 350),
sizeCurve: Curves.easeInOutCubicEmphasized,
firstChild: SizedBox(width: MediaQuery.sizeOf(context).width),
secondChild: AdditionalCommentCard(
depth: level,
replies: comment.childCount!,
onTap: () => context.read<PostBloc>().add(GetPostCommentRepliesEvent(commentParentId: comment.id)),
),
crossFadeState: collapsed ? CrossFadeState.showFirst : CrossFadeState.showSecond,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import 'package:flutter/material.dart';

import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:thunder/src/features/account/account.dart';
import 'package:thunder/src/features/comment/comment.dart';
import 'package:thunder/src/features/settings/api.dart';
import 'package:thunder/src/foundation/primitives/primitives.dart';

/// Renders the visible comment body and optional inline action buttons.
class CommentCardSurface extends StatelessWidget {
const CommentCardSurface({
super.key,
required this.account,
required this.comment,
required this.level,
required this.collapsed,
required this.highlighted,
required this.dragged,
required this.viewSource,
required this.showActions,
required this.isOwnComment,
required this.onCollapse,
required this.onLongPress,
required this.onViewSourceToggled,
required this.onAction,
});

/// Account used for comment display and actions.
final Account account;

/// Comment displayed in this row.
final ThunderComment comment;

/// Nesting depth used for comment indicators.
final int level;

/// Whether the row content is collapsed.
final bool collapsed;

/// Whether the row should use the highlighted background.
final bool highlighted;

/// Whether the row is currently being swiped.
final bool dragged;

/// Whether to render the raw markdown source.
final bool viewSource;

/// Whether inline action buttons should be displayed.
final bool showActions;

/// Whether the current user owns [comment].
final bool isOwnComment;

/// Called when the user taps the comment body.
final VoidCallback onCollapse;

/// Called when the user opens the action sheet.
final VoidCallback onLongPress;

/// Called when source rendering is toggled.
final VoidCallback onViewSourceToggled;

/// Handles an inline or gesture action.
final Future<void> Function(SwipeAction action) onAction;

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);

final nestedCommentIndicatorStyle = context.select<CommentPreferencesCubit, NestedCommentIndicatorStyle>((cubit) => cubit.state.nestedCommentIndicatorStyle);
final nestedCommentIndicatorColor = context.select<CommentPreferencesCubit, NestedCommentIndicatorColor>((cubit) => cubit.state.nestedCommentIndicatorColor);

return Material(
color: highlighted ? theme.highlightColor : null,
child: Container(
decoration: dragged ? null : CommentDepthIndicatorDecoration(context, level: level, style: nestedCommentIndicatorStyle, scheme: nestedCommentIndicatorColor),
child: InkWell(
onTap: onCollapse,
onLongPress: onLongPress,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CommentContent(
account: account,
level: level,
comment: comment,
hidden: collapsed,
viewSource: viewSource,
onViewSourceToggled: onViewSourceToggled,
),
if (showActions)
Padding(
padding: const EdgeInsets.only(bottom: 4.0, top: 6.0, right: 4.0),
child: CommentCardButtonActions(
account: account,
comment: comment,
isOwnComment: isOwnComment,
onAction: onAction,
onBottomSheetOpen: onLongPress,
),
),
],
),
),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class _CommentContentState extends State<CommentContent> with SingleTickerProvid

final _selectableRegionFocusNode = FocusNode();

@override
void dispose() {
_controller.dispose();
_selectableRegionFocusNode.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
Expand Down
1 change: 1 addition & 0 deletions lib/src/features/community/community.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export 'presentation/widgets/community_header/community_header_actions.dart';
export 'presentation/widgets/community_information.dart';
export 'presentation/widgets/community_list_entry.dart';
export 'presentation/widgets/post_card.dart';
export 'presentation/widgets/post_card_action_background.dart';
export 'presentation/widgets/post_card_actions.dart';
export 'presentation/widgets/post_card_metadata.dart';
export 'presentation/widgets/post_card_view_comfortable.dart';
Expand Down
Loading
Loading