refactor(chat): 重构聊天模块并集成历史消息加载功能
- 删除冗余的 chat_history_repository 和 home_mock_data - 简化 ag_ui_event fromJson 使用工厂映射表 - 提取 ChatBloc 事件处理方法,添加 loadHistory/loadMoreHistory - HomeScreen 集成 ChatBloc 实现历史消息加载和下拉刷新 - 更新 AGENTS.md 文档约束
This commit is contained in:
@@ -1,295 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../../shared/widgets/chat_bubble.dart';
|
||||
|
||||
enum ChatItemType { message, schedule }
|
||||
|
||||
abstract class ChatListItem {
|
||||
String get id;
|
||||
DateTime get timestamp;
|
||||
ChatItemType get type;
|
||||
MessageSender get sender;
|
||||
}
|
||||
|
||||
class HomeMockData {
|
||||
static List<ChatListItem> getTodayItems() {
|
||||
final now = DateTime.now();
|
||||
final today = DateTime(now.year, now.month, now.day);
|
||||
return _getMockItems().where((item) {
|
||||
final itemDate = DateTime(
|
||||
item.timestamp.year,
|
||||
item.timestamp.month,
|
||||
item.timestamp.day,
|
||||
);
|
||||
return itemDate == today;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
static Future<List<ChatListItem>> loadMoreItems(DateTime beforeDate) async {
|
||||
return _getOlderMockItems(beforeDate);
|
||||
}
|
||||
|
||||
static List<ChatListItem> _getMockItems() {
|
||||
final now = DateTime.now();
|
||||
final today = DateTime(now.year, now.month, now.day);
|
||||
final todayStart = DateTime(today.year, today.month, today.day);
|
||||
|
||||
return [
|
||||
ChatMessageItem(
|
||||
id: 'm4',
|
||||
content: '明天提醒我开会',
|
||||
timestamp: todayStart.add(const Duration(hours: 14)),
|
||||
sender: MessageSender.user,
|
||||
),
|
||||
ScheduleItemWrapper(
|
||||
id: 's1',
|
||||
scheduleItem: ScheduleItemModel(
|
||||
id: 's1',
|
||||
title: '产品评审会议',
|
||||
description: '讨论Q2产品路线图',
|
||||
startAt: todayStart.add(const Duration(days: 1, hours: 10)),
|
||||
endAt: todayStart.add(const Duration(days: 1, hours: 11)),
|
||||
timezone: 'Asia/Shanghai',
|
||||
sourceType: ScheduleSourceType.agentGenerated,
|
||||
status: ScheduleStatus.active,
|
||||
metadata: ScheduleMetadata(
|
||||
color: '#4F46E5',
|
||||
location: '会议室A / 在线',
|
||||
notes: '需要提前准备Q2数据',
|
||||
attachments: [
|
||||
Attachment(
|
||||
name: 'Q2路线图.pdf',
|
||||
type: AttachmentType.document,
|
||||
url: 'https://example.com/q2.pdf',
|
||||
),
|
||||
],
|
||||
),
|
||||
createdAt: todayStart.subtract(const Duration(hours: 5)),
|
||||
),
|
||||
timestamp: todayStart.add(const Duration(hours: 14, minutes: 2)),
|
||||
sender: MessageSender.ai,
|
||||
),
|
||||
ChatMessageItem(
|
||||
id: 'm5',
|
||||
content: '已为你创建日程"产品评审会议",明天上午10:00。我还会提前15分钟提醒你。',
|
||||
timestamp: todayStart.add(const Duration(hours: 14, minutes: 2)),
|
||||
sender: MessageSender.ai,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
static List<ChatListItem> _getOlderMockItems(DateTime beforeDate) {
|
||||
final before = DateTime(beforeDate.year, beforeDate.month, beforeDate.day);
|
||||
final dayBefore = before.subtract(const Duration(days: 1));
|
||||
|
||||
return [
|
||||
ChatMessageItem(
|
||||
id: 'm1',
|
||||
content: '你好,我有什么可以帮你的?',
|
||||
timestamp: dayBefore.add(const Duration(hours: 10)),
|
||||
sender: MessageSender.ai,
|
||||
),
|
||||
ChatMessageItem(
|
||||
id: 'm2',
|
||||
content: '下周一之前提交项目报告',
|
||||
timestamp: dayBefore.add(const Duration(hours: 9, minutes: 55)),
|
||||
sender: MessageSender.user,
|
||||
),
|
||||
ScheduleItemWrapper(
|
||||
id: 's0',
|
||||
scheduleItem: ScheduleItemModel(
|
||||
id: 's0',
|
||||
title: '提交项目报告',
|
||||
description: '完成并提交Q2项目报告',
|
||||
startAt: before.subtract(const Duration(days: 3)),
|
||||
endAt: null,
|
||||
timezone: 'Asia/Shanghai',
|
||||
sourceType: ScheduleSourceType.agentGenerated,
|
||||
status: ScheduleStatus.active,
|
||||
metadata: ScheduleMetadata(
|
||||
color: '#F59E0B',
|
||||
location: null,
|
||||
notes: '记得附上数据附件',
|
||||
attachments: [],
|
||||
),
|
||||
createdAt: dayBefore.add(const Duration(hours: 9, minutes: 50)),
|
||||
),
|
||||
timestamp: dayBefore.add(const Duration(hours: 9, minutes: 50)),
|
||||
sender: MessageSender.ai,
|
||||
),
|
||||
ChatMessageItem(
|
||||
id: 'm3',
|
||||
content: '好的,我已帮你创建待办事项"提交项目报告",截止日期为下周一。我还会提醒你完成这项任务。',
|
||||
timestamp: dayBefore.add(const Duration(hours: 9, minutes: 50)),
|
||||
sender: MessageSender.ai,
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class ChatMessageItem extends ChatListItem {
|
||||
@override
|
||||
final String id;
|
||||
final String content;
|
||||
@override
|
||||
final DateTime timestamp;
|
||||
@override
|
||||
final MessageSender sender;
|
||||
|
||||
ChatMessageItem({
|
||||
required this.id,
|
||||
required this.content,
|
||||
required this.timestamp,
|
||||
required this.sender,
|
||||
});
|
||||
|
||||
@override
|
||||
ChatItemType get type => ChatItemType.message;
|
||||
}
|
||||
|
||||
class ScheduleItemWrapper extends ChatListItem {
|
||||
@override
|
||||
final String id;
|
||||
final ScheduleItemModel scheduleItem;
|
||||
@override
|
||||
final DateTime timestamp;
|
||||
@override
|
||||
final MessageSender sender;
|
||||
|
||||
ScheduleItemWrapper({
|
||||
required this.id,
|
||||
required this.scheduleItem,
|
||||
required this.timestamp,
|
||||
required this.sender,
|
||||
});
|
||||
|
||||
@override
|
||||
ChatItemType get type => ChatItemType.schedule;
|
||||
}
|
||||
|
||||
enum ScheduleSourceType { manual, imported, agentGenerated }
|
||||
|
||||
enum ScheduleStatus { active, completed, canceled, archived }
|
||||
|
||||
class ScheduleItemModel {
|
||||
final String id;
|
||||
final String title;
|
||||
final String? description;
|
||||
final DateTime startAt;
|
||||
final DateTime? endAt;
|
||||
final String timezone;
|
||||
final ScheduleSourceType sourceType;
|
||||
final ScheduleStatus status;
|
||||
final ScheduleMetadata? metadata;
|
||||
final DateTime createdAt;
|
||||
|
||||
ScheduleItemModel({
|
||||
required this.id,
|
||||
required this.title,
|
||||
this.description,
|
||||
required this.startAt,
|
||||
this.endAt,
|
||||
required this.timezone,
|
||||
required this.sourceType,
|
||||
required this.status,
|
||||
this.metadata,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
factory ScheduleItemModel.fromJson(Map<String, dynamic> json) {
|
||||
return ScheduleItemModel(
|
||||
id: json['id'],
|
||||
title: json['title'],
|
||||
description: json['description'],
|
||||
startAt: DateTime.parse(json['start_at']),
|
||||
endAt: json['end_at'] != null ? DateTime.parse(json['end_at']) : null,
|
||||
timezone: json['timezone'] ?? 'UTC',
|
||||
sourceType: ScheduleSourceType.values.firstWhere(
|
||||
(e) => e.name == json['source_type'],
|
||||
orElse: () => ScheduleSourceType.manual,
|
||||
),
|
||||
status: ScheduleStatus.values.firstWhere(
|
||||
(e) => e.name == json['status'],
|
||||
orElse: () => ScheduleStatus.active,
|
||||
),
|
||||
metadata: json['metadata'] != null
|
||||
? ScheduleMetadata(
|
||||
color: json['metadata']['color'],
|
||||
location: json['metadata']['location'],
|
||||
notes: json['metadata']['notes'],
|
||||
attachments:
|
||||
(json['metadata']['attachments'] as List<dynamic>?)
|
||||
?.map(
|
||||
(a) => Attachment(
|
||||
name: a['name'],
|
||||
type: a['type'] == 'document'
|
||||
? AttachmentType.document
|
||||
: AttachmentType.reminder,
|
||||
url: a['url'],
|
||||
content: a['content'],
|
||||
note: a['note'],
|
||||
),
|
||||
)
|
||||
.toList() ??
|
||||
[],
|
||||
)
|
||||
: null,
|
||||
createdAt: DateTime.parse(json['created_at']),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ScheduleMetadata {
|
||||
final String? color;
|
||||
final String? location;
|
||||
final String? notes;
|
||||
final List<Attachment> attachments;
|
||||
|
||||
ScheduleMetadata({
|
||||
this.color,
|
||||
this.location,
|
||||
this.notes,
|
||||
this.attachments = const [],
|
||||
});
|
||||
}
|
||||
|
||||
enum AttachmentType { document, reminder }
|
||||
|
||||
class Attachment {
|
||||
final String name;
|
||||
final AttachmentType type;
|
||||
final String? url;
|
||||
final String? content;
|
||||
final String? note;
|
||||
|
||||
Attachment({
|
||||
required this.name,
|
||||
required this.type,
|
||||
this.url,
|
||||
this.content,
|
||||
this.note,
|
||||
});
|
||||
}
|
||||
|
||||
extension ScheduleSourceTypeExtension on ScheduleSourceType {
|
||||
String get displayName {
|
||||
switch (this) {
|
||||
case ScheduleSourceType.manual:
|
||||
return '手动创建';
|
||||
case ScheduleSourceType.imported:
|
||||
return '导入';
|
||||
case ScheduleSourceType.agentGenerated:
|
||||
return 'AI生成';
|
||||
}
|
||||
}
|
||||
|
||||
IconData get icon {
|
||||
switch (this) {
|
||||
case ScheduleSourceType.manual:
|
||||
return Icons.edit_calendar;
|
||||
case ScheduleSourceType.imported:
|
||||
return Icons.download;
|
||||
case ScheduleSourceType.agentGenerated:
|
||||
return Icons.auto_awesome;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,6 @@ const _defaultPadding = 20.0;
|
||||
const _itemSpacing = 16.0;
|
||||
const _inputPadding = 16.0;
|
||||
const _iconSize = 24.0;
|
||||
const _avatarSize = 32.0;
|
||||
const _botIconSize = 18.0;
|
||||
const _messagePaddingH = 13.0;
|
||||
const _messagePaddingV = 9.0;
|
||||
const _cornerRadius = 12.0;
|
||||
@@ -39,6 +37,7 @@ class HomeScreen extends StatefulWidget {
|
||||
class _HomeScreenState extends State<HomeScreen> {
|
||||
final TextEditingController _messageController = TextEditingController();
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
late final ChatBloc _chatBloc;
|
||||
|
||||
bool get _hasMessage => _messageController.text.trim().isNotEmpty;
|
||||
|
||||
@@ -46,6 +45,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
_messageController.addListener(_onMessageChanged);
|
||||
_chatBloc = ChatBloc();
|
||||
_chatBloc.loadHistory();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -53,6 +54,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
_messageController.removeListener(_onMessageChanged);
|
||||
_messageController.dispose();
|
||||
_scrollController.dispose();
|
||||
_chatBloc.close();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -62,8 +64,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) => ChatBloc(),
|
||||
return BlocProvider.value(
|
||||
value: _chatBloc,
|
||||
child: BlocConsumer<ChatBloc, ChatState>(
|
||||
listener: (context, state) {
|
||||
if (state.error != null) {
|
||||
@@ -132,6 +134,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
}
|
||||
|
||||
Widget _buildChatArea(BuildContext context, ChatState state) {
|
||||
if (state.isLoading && state.items.isEmpty) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (state.items.isEmpty) {
|
||||
return const Center(
|
||||
child: Text(
|
||||
@@ -141,30 +147,96 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (_scrollController.hasClients) {
|
||||
_scrollController.animateTo(
|
||||
_scrollController.position.maxScrollExtent,
|
||||
duration: const Duration(milliseconds: _scrollDurationMs),
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
}
|
||||
});
|
||||
return RefreshIndicator(
|
||||
onRefresh: () => _onRefresh(context),
|
||||
child: ListView.builder(
|
||||
controller: _scrollController,
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
padding: const EdgeInsets.all(_defaultPadding),
|
||||
itemCount: state.items.length + (state.hasEarlierHistory ? 1 : 0),
|
||||
itemBuilder: (context, index) {
|
||||
if (index == 0 && state.hasEarlierHistory) {
|
||||
return _buildLoadMoreButton(context, state.isLoading);
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
controller: _scrollController,
|
||||
padding: const EdgeInsets.all(_defaultPadding),
|
||||
itemCount: state.items.length,
|
||||
itemBuilder: (context, index) {
|
||||
final item = state.items[index];
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: _itemSpacing),
|
||||
child: _buildChatItem(item),
|
||||
);
|
||||
},
|
||||
final itemIndex = state.hasEarlierHistory ? index - 1 : index;
|
||||
final item = state.items[itemIndex];
|
||||
|
||||
final showDateDivider =
|
||||
itemIndex == 0 ||
|
||||
!_isSameDay(state.items[itemIndex - 1].timestamp, item.timestamp);
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
if (showDateDivider) _buildDateDivider(item.timestamp),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: _itemSpacing),
|
||||
child: _buildChatItem(item),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
bool _isSameDay(DateTime a, DateTime b) {
|
||||
return a.year == b.year && a.month == b.month && a.day == b.day;
|
||||
}
|
||||
|
||||
Widget _buildDateDivider(DateTime date) {
|
||||
final now = DateTime.now();
|
||||
final weekdays = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
|
||||
final weekday = weekdays[date.weekday - 1];
|
||||
|
||||
// For all dates (today/yesterday/this year), use the same format
|
||||
// Only add year prefix for dates from previous years
|
||||
final label = date.year == now.year
|
||||
? '${date.month}月${date.day}日 $weekday'
|
||||
: '${date.year}年${date.month}月${date.day}日 $weekday';
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
alignment: Alignment.center,
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 12, color: AppColors.slate400),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLoadMoreButton(BuildContext context, bool isLoading) {
|
||||
return GestureDetector(
|
||||
onTap: isLoading ? null : () => _onLoadMore(context),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
alignment: Alignment.center,
|
||||
child: isLoading
|
||||
? const SizedBox(
|
||||
width: 14,
|
||||
height: 14,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 1.5,
|
||||
color: AppColors.slate400,
|
||||
),
|
||||
)
|
||||
: const Text(
|
||||
'查看历史',
|
||||
style: TextStyle(fontSize: 12, color: AppColors.slate400),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onRefresh(BuildContext context) async {
|
||||
await context.read<ChatBloc>().loadMoreHistory();
|
||||
}
|
||||
|
||||
void _onLoadMore(BuildContext context) {
|
||||
context.read<ChatBloc>().loadMoreHistory();
|
||||
}
|
||||
|
||||
Widget _buildChatItem(ChatListItem item) {
|
||||
switch (item.type) {
|
||||
case ChatItemType.message:
|
||||
@@ -182,24 +254,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
mainAxisAlignment: isUser
|
||||
? MainAxisAlignment.end
|
||||
: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (!isUser) ...[
|
||||
Container(
|
||||
width: _avatarSize,
|
||||
height: _avatarSize,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.blue100,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: const Icon(
|
||||
LucideIcons.bot,
|
||||
size: _botIconSize,
|
||||
color: AppColors.blue600,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
Flexible(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
@@ -222,8 +278,6 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
),
|
||||
),
|
||||
),
|
||||
if (isUser) const SizedBox(width: 40),
|
||||
if (!isUser) const SizedBox(width: 40),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -365,6 +419,16 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
if (content.isEmpty) return;
|
||||
_messageController.clear();
|
||||
context.read<ChatBloc>().sendMessage(content);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (_scrollController.hasClients) {
|
||||
_scrollController.animateTo(
|
||||
_scrollController.position.maxScrollExtent,
|
||||
duration: const Duration(milliseconds: _scrollDurationMs),
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _showBottomSheet(BuildContext context) {
|
||||
|
||||
Reference in New Issue
Block a user