sojorn/sojorn_app/lib/models/thread_node.dart
Patrick Britton 3c4680bdd7 Initial commit: Complete threaded conversation system with inline replies
**Major Features Added:**
- **Inline Reply System**: Replace compose screen with inline reply boxes
- **Thread Navigation**: Parent/child navigation with jump functionality
- **Chain Flow UI**: Reply counts, expand/collapse animations, visual hierarchy
- **Enhanced Animations**: Smooth transitions, hover effects, micro-interactions

 **Frontend Changes:**
- **ThreadedCommentWidget**: Complete rewrite with animations and navigation
- **ThreadNode Model**: Added parent references and descendant counting
- **ThreadedConversationScreen**: Integrated navigation handlers
- **PostDetailScreen**: Replaced with threaded conversation view
- **ComposeScreen**: Added reply indicators and context
- **PostActions**: Fixed visibility checks for chain buttons

 **Backend Changes:**
- **API Route**: Added /posts/:id/thread endpoint
- **Post Repository**: Include allow_chain and visibility fields in feed
- **Thread Handler**: Support for fetching post chains

 **UI/UX Improvements:**
- **Reply Context**: Clear indication when replying to specific posts
- **Character Counting**: 500 character limit with live counter
- **Visual Hierarchy**: Depth-based indentation and styling
- **Smooth Animations**: SizeTransition, FadeTransition, hover states
- **Chain Navigation**: Parent/child buttons with visual feedback

 **Technical Enhancements:**
- **Animation Controllers**: Proper lifecycle management
- **State Management**: Clean separation of concerns
- **Navigation Callbacks**: Reusable navigation system
- **Error Handling**: Graceful fallbacks and user feedback

This creates a Reddit-style threaded conversation experience with smooth
animations, inline replies, and intuitive navigation between posts in a chain.
2026-01-30 07:40:19 -06:00

94 lines
2.3 KiB
Dart

import '../models/post.dart';
/// Tree node for building threaded conversation structure
class ThreadNode {
final Post post;
final List<ThreadNode> children;
int depth;
ThreadNode? parent;
ThreadNode({
required this.post,
required this.children,
required this.depth,
this.parent,
});
/// Build tree structure from flat list of posts
static ThreadNode buildTree(List<Post> posts) {
if (posts.isEmpty) {
throw ArgumentError('Posts list cannot be empty');
}
// Create a map of posts by ID for quick lookup
final postMap = <String, ThreadNode>{};
// Create nodes for all posts
for (final post in posts) {
postMap[post.id] = ThreadNode(
post: post,
children: [],
depth: 0, // Will be calculated later
);
}
// Build the tree structure
ThreadNode? root;
for (final post in posts) {
final node = postMap[post.id]!;
if (post.chainParentId == null || post.chainParentId!.isEmpty) {
// This is the root post
root = node;
node.depth = 0;
} else {
// This is a reply - find its parent
final parent = postMap[post.chainParentId];
if (parent != null) {
parent.children.add(node);
node.parent = parent;
node.depth = parent.depth + 1;
} else {
// Orphan post - treat as root
if (root == null) {
root = node;
node.depth = 0;
}
}
}
}
// Sort children by creation time
for (final node in postMap.values) {
node.children.sort((a, b) => a.post.createdAt.compareTo(b.post.createdAt));
}
return root ?? postMap.values.first;
}
/// Check if this node has any children
bool get hasChildren => children.isNotEmpty;
/// Get total number of posts in this subtree
int get totalCount {
int count = 1; // Count this post
for (final child in children) {
count += child.totalCount;
}
return count;
}
/// Get total number of descendants (excluding this post)
int get totalDescendants => totalCount - 1;
/// Get all posts in this subtree as a flat list (in order)
List<Post> get allPosts {
final posts = <Post>[post];
for (final child in children) {
posts.addAll(child.allPosts);
}
return posts;
}
}