2025-08-20 22:15:26 +05:30
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import '../../../shared/theme/theme_extensions.dart';
|
|
|
|
|
import 'enhanced_image_attachment.dart';
|
2025-08-26 13:31:47 +05:30
|
|
|
import 'enhanced_attachment.dart';
|
2025-08-20 22:15:26 +05:30
|
|
|
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
import 'dart:io' show Platform;
|
2025-08-28 23:46:32 +05:30
|
|
|
import 'package:conduit/l10n/app_localizations.dart';
|
2025-10-24 00:31:29 +05:30
|
|
|
import 'package:flutter/services.dart';
|
2025-09-07 22:37:52 +05:30
|
|
|
import '../../../core/providers/app_providers.dart';
|
|
|
|
|
import '../providers/chat_providers.dart';
|
|
|
|
|
import '../../../shared/services/tasks/task_queue.dart';
|
2025-10-24 00:31:29 +05:30
|
|
|
import '../../../shared/utils/conversation_context_menu.dart';
|
2025-09-07 22:37:52 +05:30
|
|
|
import '../../tools/providers/tools_providers.dart';
|
2025-08-20 22:15:26 +05:30
|
|
|
|
2025-12-10 19:40:38 +05:30
|
|
|
// Pre-compiled regex for extracting file IDs from URLs (performance optimization)
|
|
|
|
|
// Handles both /api/v1/files/{id} and /api/v1/files/{id}/content formats
|
|
|
|
|
final _fileIdPattern = RegExp(r'/api/v1/files/([^/]+)(?:/content)?$');
|
|
|
|
|
|
2025-08-20 22:15:26 +05:30
|
|
|
class UserMessageBubble extends ConsumerStatefulWidget {
|
|
|
|
|
final dynamic message;
|
|
|
|
|
final bool isUser;
|
|
|
|
|
final bool isStreaming;
|
|
|
|
|
final String? modelName;
|
|
|
|
|
final VoidCallback? onCopy;
|
|
|
|
|
final VoidCallback? onEdit;
|
|
|
|
|
final VoidCallback? onRegenerate;
|
|
|
|
|
final VoidCallback? onLike;
|
|
|
|
|
final VoidCallback? onDislike;
|
|
|
|
|
|
|
|
|
|
const UserMessageBubble({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.message,
|
|
|
|
|
required this.isUser,
|
|
|
|
|
this.isStreaming = false,
|
|
|
|
|
this.modelName,
|
|
|
|
|
this.onCopy,
|
|
|
|
|
this.onEdit,
|
|
|
|
|
this.onRegenerate,
|
|
|
|
|
this.onLike,
|
|
|
|
|
this.onDislike,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
ConsumerState<UserMessageBubble> createState() => _UserMessageBubbleState();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 00:31:29 +05:30
|
|
|
class _UserMessageBubbleState extends ConsumerState<UserMessageBubble> {
|
2025-09-07 22:37:52 +05:30
|
|
|
bool _isEditing = false;
|
|
|
|
|
late final TextEditingController _editController;
|
|
|
|
|
final FocusNode _editFocusNode = FocusNode();
|
2025-10-24 00:31:29 +05:30
|
|
|
final GlobalKey _bubbleKey = GlobalKey();
|
2025-09-07 22:37:52 +05:30
|
|
|
|
2025-08-20 22:15:26 +05:30
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2025-09-22 23:17:23 +05:30
|
|
|
_editController = TextEditingController(
|
|
|
|
|
text: widget.message?.content ?? '',
|
|
|
|
|
);
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildUserAttachmentImages() {
|
|
|
|
|
if (widget.message.attachmentIds == null ||
|
|
|
|
|
widget.message.attachmentIds!.isEmpty) {
|
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final imageCount = widget.message.attachmentIds!.length;
|
|
|
|
|
|
2025-08-21 12:49:41 +05:30
|
|
|
// iMessage-style image layout with AnimatedSwitcher for smooth transitions
|
|
|
|
|
return AnimatedSwitcher(
|
|
|
|
|
duration: const Duration(milliseconds: 300),
|
|
|
|
|
switchInCurve: Curves.easeInOut,
|
|
|
|
|
child: _buildImageLayout(imageCount),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 13:18:15 +05:30
|
|
|
Widget _buildUserFileImages() {
|
|
|
|
|
if (widget.message.files == null || widget.message.files!.isEmpty) {
|
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-22 23:17:23 +05:30
|
|
|
final allFiles = widget.message.files!;
|
|
|
|
|
|
|
|
|
|
// Separate images and non-image files
|
|
|
|
|
final imageFiles = allFiles
|
2025-08-26 13:18:15 +05:30
|
|
|
.where(
|
|
|
|
|
(file) =>
|
|
|
|
|
file is Map && file['type'] == 'image' && file['url'] != null,
|
|
|
|
|
)
|
|
|
|
|
.toList();
|
2025-09-22 23:17:23 +05:30
|
|
|
final nonImageFiles = allFiles
|
|
|
|
|
.where(
|
|
|
|
|
(file) =>
|
|
|
|
|
file is Map && file['type'] != 'image' && file['url'] != null,
|
|
|
|
|
)
|
|
|
|
|
.toList();
|
2025-08-26 13:18:15 +05:30
|
|
|
|
2025-09-22 23:17:23 +05:30
|
|
|
final widgets = <Widget>[];
|
|
|
|
|
|
|
|
|
|
// Add images first
|
|
|
|
|
if (imageFiles.isNotEmpty) {
|
|
|
|
|
widgets.add(
|
|
|
|
|
AnimatedSwitcher(
|
|
|
|
|
duration: const Duration(milliseconds: 300),
|
|
|
|
|
switchInCurve: Curves.easeInOut,
|
|
|
|
|
child: _buildFileImageLayout(imageFiles, imageFiles.length),
|
|
|
|
|
),
|
|
|
|
|
);
|
2025-08-26 13:18:15 +05:30
|
|
|
}
|
|
|
|
|
|
2025-09-22 23:17:23 +05:30
|
|
|
// Add non-image files
|
|
|
|
|
if (nonImageFiles.isNotEmpty) {
|
|
|
|
|
if (widgets.isNotEmpty) {
|
|
|
|
|
widgets.add(const SizedBox(height: Spacing.xs));
|
|
|
|
|
}
|
|
|
|
|
widgets.add(_buildUserNonImageFiles(nonImageFiles));
|
|
|
|
|
}
|
2025-08-26 13:18:15 +05:30
|
|
|
|
2025-09-22 23:17:23 +05:30
|
|
|
if (widgets.isEmpty) {
|
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
|
|
|
children: widgets,
|
2025-08-26 13:18:15 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildFileImageLayout(List<dynamic> imageFiles, int imageCount) {
|
|
|
|
|
if (imageCount == 1) {
|
2025-12-25 20:29:38 +05:30
|
|
|
final file = imageFiles[0];
|
|
|
|
|
final String imageUrl = file['url'] as String;
|
2025-08-26 13:18:15 +05:30
|
|
|
return Row(
|
|
|
|
|
key: ValueKey('user_file_single_$imageUrl'),
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
borderRadius: BorderRadius.circular(
|
|
|
|
|
AppBorderRadius.messageBubble,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: ClipRRect(
|
|
|
|
|
borderRadius: BorderRadius.circular(
|
|
|
|
|
AppBorderRadius.messageBubble,
|
|
|
|
|
),
|
|
|
|
|
child: EnhancedImageAttachment(
|
|
|
|
|
attachmentId: imageUrl,
|
|
|
|
|
isUserMessage: true,
|
|
|
|
|
isMarkdownFormat: false,
|
|
|
|
|
constraints: const BoxConstraints(
|
|
|
|
|
maxWidth: 280,
|
|
|
|
|
maxHeight: 350,
|
|
|
|
|
),
|
|
|
|
|
disableAnimation: widget.isStreaming,
|
2025-12-25 20:29:38 +05:30
|
|
|
httpHeaders: _headersForFile(file),
|
2025-08-26 13:18:15 +05:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
} else if (imageCount == 2) {
|
|
|
|
|
return Row(
|
|
|
|
|
key: ValueKey(
|
|
|
|
|
'user_file_double_${imageFiles.map((e) => e['url']).join('_')}',
|
|
|
|
|
),
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: imageFiles.asMap().entries.map((entry) {
|
|
|
|
|
final index = entry.key;
|
2025-12-25 20:29:38 +05:30
|
|
|
final file = entry.value;
|
|
|
|
|
final String imageUrl = file['url'] as String;
|
2025-08-26 13:18:15 +05:30
|
|
|
return Padding(
|
|
|
|
|
padding: EdgeInsets.only(left: index == 0 ? 0 : Spacing.xs),
|
|
|
|
|
child: Container(
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
borderRadius: BorderRadius.circular(
|
|
|
|
|
AppBorderRadius.messageBubble,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: ClipRRect(
|
|
|
|
|
borderRadius: BorderRadius.circular(
|
|
|
|
|
AppBorderRadius.messageBubble,
|
|
|
|
|
),
|
|
|
|
|
child: EnhancedImageAttachment(
|
|
|
|
|
key: ValueKey('user_file_attachment_$imageUrl'),
|
|
|
|
|
attachmentId: imageUrl,
|
|
|
|
|
isUserMessage: true,
|
|
|
|
|
isMarkdownFormat: false,
|
|
|
|
|
constraints: const BoxConstraints(
|
|
|
|
|
maxWidth: 135,
|
|
|
|
|
maxHeight: 180,
|
|
|
|
|
),
|
|
|
|
|
disableAnimation: widget.isStreaming,
|
2025-12-25 20:29:38 +05:30
|
|
|
httpHeaders: _headersForFile(file),
|
2025-08-26 13:18:15 +05:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}).toList(),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return Row(
|
|
|
|
|
key: ValueKey(
|
|
|
|
|
'user_file_grid_${imageFiles.map((e) => e['url']).join('_')}',
|
|
|
|
|
),
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
|
children: [
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Container(
|
|
|
|
|
constraints: const BoxConstraints(maxWidth: 280),
|
|
|
|
|
child: Wrap(
|
|
|
|
|
alignment: WrapAlignment.end,
|
|
|
|
|
spacing: Spacing.xs,
|
|
|
|
|
runSpacing: Spacing.xs,
|
|
|
|
|
children: imageFiles.map((file) {
|
|
|
|
|
final String imageUrl = file['url'] as String;
|
|
|
|
|
return Container(
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
),
|
|
|
|
|
child: ClipRRect(
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
child: EnhancedImageAttachment(
|
|
|
|
|
key: ValueKey('user_file_grid_attachment_$imageUrl'),
|
|
|
|
|
attachmentId: imageUrl,
|
|
|
|
|
isUserMessage: true,
|
|
|
|
|
isMarkdownFormat: false,
|
|
|
|
|
constraints: BoxConstraints(
|
|
|
|
|
maxWidth: imageCount == 3 ? 135 : 90,
|
|
|
|
|
maxHeight: imageCount == 3 ? 135 : 90,
|
|
|
|
|
),
|
|
|
|
|
disableAnimation: widget.isStreaming,
|
2025-11-29 10:57:06 +05:30
|
|
|
httpHeaders: _headersForFile(file),
|
2025-08-26 13:18:15 +05:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}).toList(),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 12:49:41 +05:30
|
|
|
Widget _buildImageLayout(int imageCount) {
|
2025-08-20 22:15:26 +05:30
|
|
|
if (imageCount == 1) {
|
|
|
|
|
// Single image - larger display
|
|
|
|
|
return Row(
|
2025-08-21 12:49:41 +05:30
|
|
|
key: ValueKey('user_single_${widget.message.attachmentIds![0]}'),
|
2025-08-20 22:15:26 +05:30
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
|
children: [
|
2025-08-21 12:49:41 +05:30
|
|
|
Container(
|
|
|
|
|
decoration: BoxDecoration(
|
2025-08-26 13:18:15 +05:30
|
|
|
borderRadius: BorderRadius.circular(
|
|
|
|
|
AppBorderRadius.messageBubble,
|
|
|
|
|
),
|
2025-08-21 12:49:41 +05:30
|
|
|
),
|
|
|
|
|
child: ClipRRect(
|
2025-08-26 13:18:15 +05:30
|
|
|
borderRadius: BorderRadius.circular(
|
|
|
|
|
AppBorderRadius.messageBubble,
|
|
|
|
|
),
|
2025-08-26 13:31:47 +05:30
|
|
|
child: EnhancedAttachment(
|
2025-08-21 12:49:41 +05:30
|
|
|
attachmentId: widget.message.attachmentIds![0],
|
|
|
|
|
isUserMessage: true,
|
2025-08-26 13:18:15 +05:30
|
|
|
constraints: const BoxConstraints(
|
|
|
|
|
maxWidth: 280,
|
|
|
|
|
maxHeight: 350,
|
|
|
|
|
),
|
2025-08-21 12:49:41 +05:30
|
|
|
disableAnimation: widget.isStreaming,
|
|
|
|
|
),
|
2025-08-20 22:15:26 +05:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
} else if (imageCount == 2) {
|
|
|
|
|
// Two images side by side
|
|
|
|
|
return Row(
|
2025-08-21 12:49:41 +05:30
|
|
|
key: ValueKey('user_double_${widget.message.attachmentIds!.join('_')}'),
|
2025-08-20 22:15:26 +05:30
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
2025-08-26 13:18:15 +05:30
|
|
|
children: widget.message.attachmentIds!.asMap().entries.map((
|
|
|
|
|
entry,
|
|
|
|
|
) {
|
2025-08-21 12:49:41 +05:30
|
|
|
final index = entry.key;
|
|
|
|
|
final attachmentId = entry.value;
|
2025-08-20 22:15:26 +05:30
|
|
|
return Padding(
|
2025-08-21 12:49:41 +05:30
|
|
|
padding: EdgeInsets.only(left: index == 0 ? 0 : Spacing.xs),
|
|
|
|
|
child: Container(
|
|
|
|
|
decoration: BoxDecoration(
|
2025-08-26 13:18:15 +05:30
|
|
|
borderRadius: BorderRadius.circular(
|
|
|
|
|
AppBorderRadius.messageBubble,
|
|
|
|
|
),
|
2025-08-20 22:15:26 +05:30
|
|
|
),
|
2025-08-21 12:49:41 +05:30
|
|
|
child: ClipRRect(
|
2025-08-26 13:18:15 +05:30
|
|
|
borderRadius: BorderRadius.circular(
|
|
|
|
|
AppBorderRadius.messageBubble,
|
|
|
|
|
),
|
2025-08-26 13:31:47 +05:30
|
|
|
child: EnhancedAttachment(
|
2025-08-21 12:49:41 +05:30
|
|
|
key: ValueKey('user_attachment_$attachmentId'),
|
|
|
|
|
attachmentId: attachmentId,
|
|
|
|
|
isUserMessage: true,
|
|
|
|
|
constraints: const BoxConstraints(
|
|
|
|
|
maxWidth: 135,
|
|
|
|
|
maxHeight: 180,
|
|
|
|
|
),
|
|
|
|
|
disableAnimation: widget.isStreaming,
|
2025-08-20 22:15:26 +05:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}).toList(),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
// Grid layout for 3+ images
|
|
|
|
|
return Row(
|
2025-08-21 12:49:41 +05:30
|
|
|
key: ValueKey('user_grid_${widget.message.attachmentIds!.join('_')}'),
|
2025-08-20 22:15:26 +05:30
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
|
children: [
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Container(
|
|
|
|
|
constraints: const BoxConstraints(maxWidth: 280),
|
|
|
|
|
child: Wrap(
|
|
|
|
|
alignment: WrapAlignment.end,
|
|
|
|
|
spacing: Spacing.xs,
|
|
|
|
|
runSpacing: Spacing.xs,
|
|
|
|
|
children: widget.message.attachmentIds!.map((attachmentId) {
|
2025-08-21 12:49:41 +05:30
|
|
|
return Container(
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
),
|
|
|
|
|
child: ClipRRect(
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
2025-08-26 13:31:47 +05:30
|
|
|
child: EnhancedAttachment(
|
2025-08-21 12:49:41 +05:30
|
|
|
key: ValueKey('user_grid_attachment_$attachmentId'),
|
|
|
|
|
attachmentId: attachmentId,
|
|
|
|
|
isUserMessage: true,
|
|
|
|
|
constraints: BoxConstraints(
|
|
|
|
|
maxWidth: imageCount == 3 ? 135 : 90,
|
|
|
|
|
maxHeight: imageCount == 3 ? 135 : 90,
|
|
|
|
|
),
|
|
|
|
|
disableAnimation: widget.isStreaming,
|
2025-08-20 22:15:26 +05:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}).toList(),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-22 23:17:23 +05:30
|
|
|
Widget _buildUserNonImageFiles(List<dynamic> nonImageFiles) {
|
|
|
|
|
return Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
|
children: [
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Wrap(
|
|
|
|
|
alignment: WrapAlignment.end,
|
|
|
|
|
spacing: Spacing.xs,
|
|
|
|
|
runSpacing: Spacing.xs,
|
|
|
|
|
children: nonImageFiles.map<Widget>((file) {
|
|
|
|
|
final fileUrl = file['url'] as String?;
|
|
|
|
|
|
|
|
|
|
if (fileUrl == null) return const SizedBox.shrink();
|
|
|
|
|
|
2025-12-10 19:40:38 +05:30
|
|
|
// Extract file ID from URL - handle both formats:
|
|
|
|
|
// /api/v1/files/{id} and /api/v1/files/{id}/content
|
2025-09-22 23:17:23 +05:30
|
|
|
String attachmentId = fileUrl;
|
2025-12-10 19:40:38 +05:30
|
|
|
if (fileUrl.contains('/api/v1/files/')) {
|
|
|
|
|
final fileIdMatch = _fileIdPattern.firstMatch(fileUrl);
|
2025-09-22 23:17:23 +05:30
|
|
|
if (fileIdMatch != null) {
|
|
|
|
|
attachmentId = fileIdMatch.group(1)!;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return EnhancedAttachment(
|
|
|
|
|
key: ValueKey('user_file_attachment_$attachmentId'),
|
|
|
|
|
attachmentId: attachmentId,
|
|
|
|
|
isMarkdownFormat: false,
|
|
|
|
|
isUserMessage: true,
|
|
|
|
|
constraints: const BoxConstraints(maxWidth: 280, maxHeight: 80),
|
|
|
|
|
disableAnimation: widget.isStreaming,
|
|
|
|
|
);
|
|
|
|
|
}).toList(),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-29 10:57:06 +05:30
|
|
|
Map<String, String>? _headersForFile(dynamic file) {
|
|
|
|
|
if (file is! Map) return null;
|
|
|
|
|
final rawHeaders = file['headers'];
|
|
|
|
|
if (rawHeaders is! Map) return null;
|
|
|
|
|
final result = <String, String>{};
|
|
|
|
|
rawHeaders.forEach((key, value) {
|
|
|
|
|
final keyString = key?.toString();
|
|
|
|
|
final valueString = value?.toString();
|
|
|
|
|
if (keyString != null &&
|
|
|
|
|
keyString.isNotEmpty &&
|
|
|
|
|
valueString != null &&
|
|
|
|
|
valueString.isNotEmpty) {
|
|
|
|
|
result[keyString] = valueString;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return result.isEmpty ? null : result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 22:15:26 +05:30
|
|
|
// Assistant-only helpers removed; this widget renders only user bubbles.
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
2025-09-07 22:37:52 +05:30
|
|
|
_editController.dispose();
|
|
|
|
|
_editFocusNode.dispose();
|
2025-08-20 22:15:26 +05:30
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-20 18:26:03 +05:30
|
|
|
List<ConduitContextMenuAction> _buildMessageActions(BuildContext context) {
|
|
|
|
|
// Don't show menu while editing - return empty list
|
|
|
|
|
if (_isEditing) return [];
|
2025-08-20 22:15:26 +05:30
|
|
|
|
2025-10-24 00:31:29 +05:30
|
|
|
final l10n = AppLocalizations.of(context)!;
|
|
|
|
|
|
2025-12-20 18:26:03 +05:30
|
|
|
return [
|
|
|
|
|
ConduitContextMenuAction(
|
|
|
|
|
cupertinoIcon: CupertinoIcons.pencil,
|
|
|
|
|
materialIcon: Icons.edit_outlined,
|
|
|
|
|
label: l10n.edit,
|
|
|
|
|
onBeforeClose: () => HapticFeedback.selectionClick(),
|
|
|
|
|
onSelected: () async => _startInlineEdit(),
|
|
|
|
|
),
|
|
|
|
|
ConduitContextMenuAction(
|
|
|
|
|
cupertinoIcon: CupertinoIcons.doc_on_clipboard,
|
|
|
|
|
materialIcon: Icons.content_copy,
|
|
|
|
|
label: l10n.copy,
|
|
|
|
|
onBeforeClose: () => HapticFeedback.selectionClick(),
|
|
|
|
|
onSelected: () async {
|
|
|
|
|
if (widget.onCopy != null) {
|
|
|
|
|
widget.onCopy!();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
];
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return _buildUserMessage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildUserMessage() {
|
|
|
|
|
final hasImages =
|
|
|
|
|
widget.message.attachmentIds != null &&
|
|
|
|
|
widget.message.attachmentIds!.isNotEmpty;
|
|
|
|
|
final hasText = widget.message.content.isNotEmpty;
|
2025-09-22 23:17:23 +05:30
|
|
|
final hasFilesFromArray =
|
2025-08-26 13:18:15 +05:30
|
|
|
widget.message.files != null &&
|
2025-09-22 23:17:23 +05:30
|
|
|
(widget.message.files as List).any((f) => f is Map && f['url'] != null);
|
2025-09-07 22:37:52 +05:30
|
|
|
// Prefer input/textPrimary colors during inline editing to avoid low contrast
|
|
|
|
|
final inlineEditTextColor = context.conduitTheme.textPrimary;
|
2025-09-22 23:17:23 +05:30
|
|
|
final inlineEditFill = context.conduitTheme.surfaceContainer.withValues(
|
|
|
|
|
alpha: 0.92,
|
|
|
|
|
);
|
2025-12-20 18:25:55 +05:30
|
|
|
// Use rounded rectangle for multiline, pill for single-line (like chat input)
|
|
|
|
|
// Consider multiline if text exceeds ~50 chars or contains newlines
|
|
|
|
|
// Check length first (O(1)) to short-circuit before scanning for newlines
|
|
|
|
|
final content = widget.message.content;
|
|
|
|
|
final isMultiline = content.length > 50 || content.contains('\n');
|
|
|
|
|
final bubbleRadius = isMultiline ? AppBorderRadius.xl : AppBorderRadius.pill;
|
2025-08-20 22:15:26 +05:30
|
|
|
|
2025-12-20 18:26:03 +05:30
|
|
|
return ConduitContextMenu(
|
|
|
|
|
actions: _buildMessageActions(context),
|
2025-10-05 00:59:32 +05:30
|
|
|
child: Container(
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
margin: const EdgeInsets.only(
|
|
|
|
|
bottom: Spacing.md,
|
|
|
|
|
left: Spacing.xxxl,
|
|
|
|
|
right: Spacing.xs,
|
|
|
|
|
),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
|
|
|
children: [
|
|
|
|
|
// Display images outside and above the text bubble (iMessage style)
|
|
|
|
|
// Prioritize files array over attachmentIds to avoid duplication
|
|
|
|
|
if (hasFilesFromArray) ...[
|
|
|
|
|
_buildUserFileImages(),
|
|
|
|
|
] else if (hasImages) ...[
|
|
|
|
|
_buildUserAttachmentImages(),
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
// Display text bubble if there's text content
|
|
|
|
|
if (hasText) const SizedBox(height: Spacing.xs),
|
|
|
|
|
if (hasText)
|
|
|
|
|
Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
|
children: [
|
|
|
|
|
Flexible(
|
|
|
|
|
child: ConstrainedBox(
|
|
|
|
|
constraints: BoxConstraints(
|
|
|
|
|
maxWidth: MediaQuery.of(context).size.width * 0.82,
|
|
|
|
|
),
|
|
|
|
|
child: Container(
|
2025-10-24 00:31:29 +05:30
|
|
|
key: _bubbleKey,
|
2025-10-05 00:59:32 +05:30
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: Spacing.chatBubblePadding,
|
|
|
|
|
vertical: Spacing.sm,
|
|
|
|
|
),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: context.conduitTheme.chatBubbleUser,
|
2025-12-20 18:25:55 +05:30
|
|
|
borderRadius: BorderRadius.circular(bubbleRadius),
|
2025-08-20 22:15:26 +05:30
|
|
|
),
|
2025-10-05 00:59:32 +05:30
|
|
|
child: _isEditing
|
|
|
|
|
? Focus(
|
|
|
|
|
focusNode: _editFocusNode,
|
|
|
|
|
autofocus: true,
|
|
|
|
|
child: DecoratedBox(
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: inlineEditFill,
|
|
|
|
|
borderRadius: BorderRadius.circular(
|
|
|
|
|
AppBorderRadius.small,
|
|
|
|
|
),
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: context
|
|
|
|
|
.conduitTheme
|
|
|
|
|
.inputBorderFocused
|
|
|
|
|
.withValues(alpha: 0.5),
|
|
|
|
|
width: BorderWidth.thin,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: Spacing.xs,
|
|
|
|
|
vertical: Spacing.xxs,
|
|
|
|
|
),
|
|
|
|
|
child: Platform.isIOS
|
|
|
|
|
? CupertinoTextField(
|
|
|
|
|
controller: _editController,
|
|
|
|
|
maxLines: null,
|
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
|
autofillHints: const <String>[],
|
|
|
|
|
style: AppTypography
|
|
|
|
|
.chatMessageStyle
|
|
|
|
|
.copyWith(
|
|
|
|
|
color: inlineEditTextColor,
|
|
|
|
|
),
|
|
|
|
|
decoration: const BoxDecoration(),
|
|
|
|
|
cursorColor: context
|
|
|
|
|
.conduitTheme
|
|
|
|
|
.buttonPrimary,
|
|
|
|
|
onSubmitted: (_) =>
|
|
|
|
|
_saveInlineEdit(),
|
|
|
|
|
)
|
|
|
|
|
: TextField(
|
|
|
|
|
controller: _editController,
|
|
|
|
|
maxLines: null,
|
|
|
|
|
autofillHints: const <String>[],
|
|
|
|
|
style: AppTypography
|
|
|
|
|
.chatMessageStyle
|
|
|
|
|
.copyWith(
|
|
|
|
|
color: inlineEditTextColor,
|
|
|
|
|
),
|
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
|
isCollapsed: true,
|
|
|
|
|
border: InputBorder.none,
|
|
|
|
|
contentPadding: EdgeInsets.zero,
|
|
|
|
|
),
|
|
|
|
|
cursorColor: context
|
|
|
|
|
.conduitTheme
|
|
|
|
|
.buttonPrimary,
|
|
|
|
|
onSubmitted: (_) =>
|
|
|
|
|
_saveInlineEdit(),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
: Text(
|
|
|
|
|
widget.message.content,
|
|
|
|
|
style: AppTypography.chatMessageStyle.copyWith(
|
|
|
|
|
color:
|
|
|
|
|
context.conduitTheme.chatBubbleUserText,
|
|
|
|
|
),
|
|
|
|
|
softWrap: true,
|
|
|
|
|
textAlign: TextAlign.left,
|
|
|
|
|
textHeightBehavior: const TextHeightBehavior(
|
|
|
|
|
applyHeightToFirstAscent: false,
|
|
|
|
|
applyHeightToLastDescent: false,
|
|
|
|
|
leadingDistribution:
|
|
|
|
|
TextLeadingDistribution.even,
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-08-20 22:15:26 +05:30
|
|
|
),
|
2025-10-05 00:59:32 +05:30
|
|
|
),
|
2025-08-20 22:15:26 +05:30
|
|
|
),
|
|
|
|
|
],
|
2025-10-05 00:59:32 +05:30
|
|
|
),
|
|
|
|
|
|
2025-10-24 00:31:29 +05:30
|
|
|
// Edit action buttons - show Save/Cancel when editing
|
|
|
|
|
if (_isEditing) ...[
|
2025-10-05 00:59:32 +05:30
|
|
|
const SizedBox(height: Spacing.sm),
|
2025-10-24 00:31:29 +05:30
|
|
|
_buildEditActionButtons(),
|
2025-10-05 00:59:32 +05:30
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|
|
|
|
|
|
2025-10-24 00:31:29 +05:30
|
|
|
Widget _buildEditActionButtons() {
|
|
|
|
|
final l10n = AppLocalizations.of(context)!;
|
|
|
|
|
final theme = context.conduitTheme;
|
2025-08-20 22:15:26 +05:30
|
|
|
|
2025-10-24 00:31:29 +05:30
|
|
|
return Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
2025-08-20 22:15:26 +05:30
|
|
|
children: [
|
2025-10-24 00:31:29 +05:30
|
|
|
// Cancel button
|
|
|
|
|
Material(
|
|
|
|
|
color: Colors.transparent,
|
|
|
|
|
child: InkWell(
|
2025-09-07 22:37:52 +05:30
|
|
|
onTap: _cancelInlineEdit,
|
2025-10-24 00:31:29 +05:30
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.small),
|
|
|
|
|
child: Container(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: Spacing.md,
|
|
|
|
|
vertical: Spacing.xs,
|
|
|
|
|
),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.surfaceContainer,
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.small),
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: theme.cardBorder,
|
|
|
|
|
width: BorderWidth.thin,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Icon(
|
|
|
|
|
Platform.isIOS ? CupertinoIcons.xmark : Icons.close,
|
|
|
|
|
size: IconSize.xs,
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: Spacing.xs),
|
|
|
|
|
Text(
|
|
|
|
|
l10n.cancel,
|
|
|
|
|
style: AppTypography.standard.copyWith(
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-09-07 22:37:52 +05:30
|
|
|
),
|
2025-10-24 00:31:29 +05:30
|
|
|
),
|
|
|
|
|
const SizedBox(width: Spacing.sm),
|
|
|
|
|
// Save button
|
|
|
|
|
Material(
|
|
|
|
|
color: Colors.transparent,
|
|
|
|
|
child: InkWell(
|
|
|
|
|
onTap: _saveInlineEdit,
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.small),
|
|
|
|
|
child: Container(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: Spacing.md,
|
|
|
|
|
vertical: Spacing.xs,
|
|
|
|
|
),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.buttonPrimary,
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.small),
|
|
|
|
|
),
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Icon(
|
|
|
|
|
Platform.isIOS ? CupertinoIcons.check_mark : Icons.check,
|
|
|
|
|
size: IconSize.xs,
|
|
|
|
|
color: theme.buttonPrimaryText,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: Spacing.xs),
|
|
|
|
|
Text(
|
|
|
|
|
l10n.save,
|
|
|
|
|
style: AppTypography.standard.copyWith(
|
|
|
|
|
color: theme.buttonPrimaryText,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-09-07 22:37:52 +05:30
|
|
|
),
|
2025-10-24 00:31:29 +05:30
|
|
|
),
|
2025-08-20 22:15:26 +05:30
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-09-07 22:37:52 +05:30
|
|
|
|
2025-10-24 00:31:29 +05:30
|
|
|
// Assistant-only message renderer removed.
|
|
|
|
|
|
2025-09-07 22:37:52 +05:30
|
|
|
void _startInlineEdit() {
|
|
|
|
|
if (_isEditing) return;
|
|
|
|
|
setState(() {
|
|
|
|
|
_isEditing = true;
|
|
|
|
|
_editController.text = widget.message.content ?? '';
|
|
|
|
|
});
|
|
|
|
|
// Request focus after frame to show keyboard
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
if (mounted) {
|
|
|
|
|
_editFocusNode.requestFocus();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _cancelInlineEdit() {
|
|
|
|
|
if (!_isEditing) return;
|
|
|
|
|
setState(() {
|
|
|
|
|
_isEditing = false;
|
|
|
|
|
_editController.text = widget.message.content ?? '';
|
|
|
|
|
});
|
|
|
|
|
_editFocusNode.unfocus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _saveInlineEdit() async {
|
|
|
|
|
final newText = _editController.text.trim();
|
|
|
|
|
final oldText = (widget.message.content ?? '').toString();
|
|
|
|
|
if (newText.isEmpty || newText == oldText) {
|
|
|
|
|
_cancelInlineEdit();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Remove messages after this one
|
|
|
|
|
final messages = ref.read(chatMessagesProvider);
|
|
|
|
|
final idx = messages.indexOf(widget.message);
|
|
|
|
|
if (idx >= 0) {
|
|
|
|
|
final keep = messages.take(idx).toList(growable: false);
|
|
|
|
|
ref.read(chatMessagesProvider.notifier).setMessages(keep);
|
|
|
|
|
|
|
|
|
|
// Enqueue edited text as a new message
|
|
|
|
|
final activeConv = ref.read(activeConversationProvider);
|
2025-09-22 23:17:23 +05:30
|
|
|
final List<String>? attachments =
|
|
|
|
|
(widget.message.attachmentIds != null &&
|
2025-09-07 22:37:52 +05:30
|
|
|
(widget.message.attachmentIds as List).isNotEmpty)
|
|
|
|
|
? List<String>.from(widget.message.attachmentIds as List)
|
|
|
|
|
: null;
|
|
|
|
|
final toolIds = ref.read(selectedToolIdsProvider);
|
|
|
|
|
await ref
|
|
|
|
|
.read(taskQueueProvider.notifier)
|
|
|
|
|
.enqueueSendText(
|
|
|
|
|
conversationId: activeConv?.id,
|
|
|
|
|
text: newText,
|
|
|
|
|
attachments: attachments,
|
|
|
|
|
toolIds: toolIds.isNotEmpty ? toolIds : null,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch (_) {
|
|
|
|
|
// Swallow errors; upstream error handling will surface if needed
|
|
|
|
|
} finally {
|
|
|
|
|
if (mounted) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isEditing = false;
|
|
|
|
|
});
|
|
|
|
|
_editFocusNode.unfocus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|