You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
4.8 KiB
Dart
130 lines
4.8 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:test_sa/extensions/int_extensions.dart';
|
|
import 'package:test_sa/extensions/text_extensions.dart';
|
|
import 'package:test_sa/extensions/widget_extensions.dart';
|
|
import 'package:test_sa/modules/copilot/model/copilot_message_model.dart';
|
|
import 'package:test_sa/new_views/app_style/app_color.dart';
|
|
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
|
|
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
|
|
import 'package:test_sa/views/widgets/item_views/info_header_widget.dart';
|
|
import 'package:test_sa/views/widgets/item_views/info_text_widget.dart';
|
|
|
|
import '../controller/copilot_provider.dart';
|
|
import 'copilot_chat_page.dart' show CopilotChatPage;
|
|
class CopilotHistoryPage extends StatefulWidget {
|
|
static const String routeName = "/copilot-history";
|
|
const CopilotHistoryPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<CopilotHistoryPage> createState() => _CopilotHistoryPageState();
|
|
}
|
|
class _CopilotHistoryPageState extends State<CopilotHistoryPage> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
context.read<CopilotProvider>().loadHistory();
|
|
});
|
|
}
|
|
|
|
void _openConversation(String threadId) {
|
|
Navigator.push(context, CupertinoPageRoute(builder: (_) => const CopilotChatPage(isNewConversation: false))).then((_) => context.read<CopilotProvider>().loadHistory());
|
|
context.read<CopilotProvider>().loadConversation(threadId);
|
|
}
|
|
|
|
void _startNewConversation() {
|
|
context.read<CopilotProvider>().startNewConversation();
|
|
Navigator.push(context, CupertinoPageRoute(builder: (_) => const CopilotChatPage(isNewConversation: true))).then((_) => context.read<CopilotProvider>().loadHistory());
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: DefaultAppBar(
|
|
title: "Copilot History",
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.chat_bubble),
|
|
onPressed: _startNewConversation,
|
|
),
|
|
],
|
|
),
|
|
body: Consumer<CopilotProvider>(
|
|
builder: (context, provider, _) {
|
|
if (provider.historyLoading) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
if (provider.history.isEmpty) {
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(Icons.chat_bubble_outline, size: 56, color: AppColor.neutral120),
|
|
16.height,
|
|
Text(
|
|
"No conversations yet",
|
|
style: AppTextStyles.heading6.copyWith(color: AppColor.neutral120),
|
|
),
|
|
24.height,
|
|
AppFilledButton(
|
|
label: "Start a new conversation",
|
|
maxWidth: true,
|
|
buttonColor: AppColor.primary10,
|
|
onPressed: _startNewConversation,
|
|
).paddingOnly(start: 48, end: 48),
|
|
],
|
|
).center;
|
|
}
|
|
|
|
return ListView.separated(
|
|
padding: const EdgeInsets.all(16),
|
|
itemCount: provider.history.length,
|
|
separatorBuilder: (czt, index) => 12.height,
|
|
itemBuilder: (context, index) {
|
|
final item = provider.history[index];
|
|
return messageHistoryCard(item: item, provider: provider);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget messageHistoryCard({required CopilotHistoryItem item, required CopilotProvider provider}) {
|
|
return Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Icon(
|
|
Icons.chat_bubble,
|
|
color: AppColor.icon2Color(context),
|
|
),
|
|
16.width,
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
InfoHeaderWidget(item.firstHumanMessage),
|
|
4.height,
|
|
InfoTextLabelWidget(label: _formatDate(item.updatedAt)),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
"delete_icon".toSvgAsset(height: 20, width: 20).onPress(() => provider.deleteHistory(item.threadId)),
|
|
],
|
|
).toShadowContainer(context, showShadow: false).onPress(() {
|
|
_openConversation(item.threadId);
|
|
});
|
|
}
|
|
|
|
String _formatDate(DateTime date) {
|
|
return "${date.day.toString().padLeft(2, '0')}/${date.month.toString().padLeft(2, '0')}/${date.year} "
|
|
"${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}";
|
|
}
|
|
}
|