sojorn/sojorn_app/lib/widgets/post/post_body.dart
Patrick Britton 4512ff11d1 Grand Unification: centralize design tokens, add sponsored PostViewMode, delete 9 dead files
Phase 1 - Design Tokens:
- tokens.dart: Add NSFW, sponsored, overlay, surface state color tokens
- tokens.dart: Add granular spacing (s6, s8) and card gap constants
- tokens.dart: Add SojornRadii.xl
- app_theme.dart: Bridge new tokens (nsfwWarning*, sponsored*, overlay*, media*)

Phase 2 - Super Post Unification:
- post_view_mode.dart: Add PostViewMode.sponsored enum value
- sojorn_post_card.dart: Handle sponsored mode with badge, use token refs for NSFW colors
- feed_sojorn_screen.dart: Replace SponsoredPostCard with sojornPostCard(mode: sponsored)
- post_body/media/link_preview: Add sponsored case to all PostViewMode switches
- post_media.dart: Replace inline Colors.* with SojornColors/AppTheme tokens
- post_header.dart: Replace Colors.white with SojornColors.basicWhite
- post_menu.dart: Replace Colors.red with SojornColors.destructive
- post_actions.dart: Replace Colors.transparent with SojornColors.transparent

Phase 3 - Tree Shake (9 files deleted):
- widgets/post/sponsored_post_card.dart (absorbed into sojornPostCard)
- widgets/post/chain_quote_widget.dart (duplicate, unused)
- widgets/post_with_video_widget.dart (dead)
- widgets/video_player_widget.dart (dead)
- widgets/video_thumbnail_widget.dart (dead)
- widgets/composer_field.dart (dead)
- widgets/compose_fab.dart (dead)
- widgets/category_tile.dart (dead)
- theme/sojorn_feed_palette.dart (deprecated, unused)
2026-02-10 13:55:00 -06:00

173 lines
5 KiB
Dart

import 'package:flutter/material.dart';
import '../../theme/app_theme.dart';
import '../sojorn_rich_text.dart';
import 'markdown_post_body.dart';
import 'post_view_mode.dart';
/// Post body text with reading-optimized typography.
///
/// Design Intent:
/// - Post body is the hero - clear visual hierarchy
/// - Line height adjusts based on content length
/// - Typography communicates emotional tone, not just content
/// - Supports both plain text and Markdown formatting
/// - ViewMode controls truncation behavior
class PostBody extends StatelessWidget {
final String text;
final String? bodyFormat; // 'plain' or 'markdown'
final String? backgroundId; // theme id
final bool isReflective;
final PostViewMode mode;
final bool hideUrls;
const PostBody({
super.key,
required this.text,
this.bodyFormat,
this.backgroundId,
this.isReflective = false,
this.mode = PostViewMode.feed,
this.hideUrls = false,
});
/// Check if text contains Markdown syntax
bool _hasMarkdownSyntax(String text) {
// Check for common Markdown patterns
return text.contains('**') || // Bold
text.contains('_') || // Italic
text.startsWith('#') || // Headers
text.contains('[') && text.contains('](') || // Links
text.contains('```') || // Code blocks
text.contains('- ') || // Lists
text.contains('> '); // Blockquotes
}
/// Determine max lines based on view mode
int? get _maxLines {
switch (mode) {
case PostViewMode.feed:
case PostViewMode.sponsored:
return 12; // Truncate in feed
case PostViewMode.detail:
return null; // Show all in detail
case PostViewMode.compact:
return 6; // More compact in profile lists
case PostViewMode.thread:
return 4; // Very compact in thread replies
}
}
@override
Widget build(BuildContext context) {
// 1. Trim trailing whitespace
// 2. Collapse 3+ newlines into 2 (max one empty line)
final cleanedText = text
.replaceAll(RegExp(r'\s+$'), '')
.replaceAll(RegExp(r'\n{3,}'), '\n\n');
final isMarkdown = bodyFormat == 'markdown' || _hasMarkdownSyntax(cleanedText);
final TextStyle style;
if (isReflective) {
style = AppTheme.postBodyReflective;
} else {
final estimatedLines = (cleanedText.length / 45).ceil();
if (estimatedLines <= 3) {
style = AppTheme.postBodyShort;
} else if (estimatedLines >= 10) {
style = AppTheme.postBodyLong;
} else {
style = AppTheme.postBody;
}
}
final int? maxLines = _maxLines;
// If we have a maxLines limit, we want to show "Expand post..." if it's exceeded
if (maxLines != null) {
// Approximate line height (fontSize * 1.5 height + a bit of buffer)
final double lineHeight = (style.fontSize ?? 17.0) * 1.6;
final double maxHeight = maxLines * lineHeight;
return LayoutBuilder(
builder: (context, constraints) {
final content = isMarkdown
? MarkdownPostBody(
markdown: text,
baseStyle: style,
// We don't pass maxLines to MarkdownPostBody here because we handle clipping ourselves
// to show the "Expand" button at the bottom correctly
)
: sojornRichText(
text: cleanedText,
style: style,
hideUrls: hideUrls,
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ConstrainedBox(
constraints: BoxConstraints(maxHeight: maxHeight),
child: ClipRect(
child: content,
),
),
_ExpandIndicator(maxLines: maxLines, text: text),
],
);
},
);
}
if (isMarkdown) {
return MarkdownPostBody(
markdown: text,
baseStyle: style,
maxLines: maxLines,
);
}
return sojornRichText(
text: cleanedText,
style: style,
maxLines: maxLines,
hideUrls: hideUrls,
);
}
}
class _ExpandIndicator extends StatelessWidget {
final int maxLines;
final String text;
const _ExpandIndicator({required this.maxLines, required this.text});
@override
Widget build(BuildContext context) {
// Basic heuristic: check if line count is high or text is long
final lineCount = '\n'.allMatches(text).length + 1;
final isLong = text.length > 400 || lineCount > maxLines;
if (!isLong) return const SizedBox.shrink();
return Padding(
padding: const EdgeInsets.only(top: 8),
child: Row(
children: [
Text(
'Expand post...',
style: TextStyle(
color: AppTheme.brightNavy,
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
const SizedBox(width: 4),
Icon(Icons.keyboard_arrow_down, size: 16, color: AppTheme.brightNavy),
],
),
);
}
}