Skip to content

Commit

Permalink
feat: copy and import conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
PeronGH committed Dec 13, 2023
1 parent 1101253 commit 86a366b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 16 deletions.
28 changes: 28 additions & 0 deletions lib/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:sydney_webui/models/message.dart';
import 'package:sydney_webui/services/sydney_service.dart';
Expand Down Expand Up @@ -224,4 +225,31 @@ class Controller extends GetxController {
type: Message.typeAdditionalInstructions,
content: sydneyService.systemMessage);
}

void copyConversation() {
try {
final content = jsonEncode(messages);
Clipboard.setData(ClipboardData(text: content));
Get.snackbar('Copied', 'Conversation has been copied to clipboard');
} catch (e) {
Get.snackbar('Error occurred', 'Failed to copy conversation: $e');
e.printError();
}
}

void importConversation() async {
if (isGenerating.value) return;

try {
final data = await Clipboard.getData('text/plain');
final content = data?.text ?? '';
final messages = jsonDecode(content) as List<dynamic>;
final newMessages = messages.map((message) => Message.fromJson(message));
this.messages.value = newMessages.toList();
Get.snackbar('Imported', 'Conversation has been imported');
} catch (e) {
Get.snackbar('Error occurred', 'Failed to import conversation: $e');
e.printError();
}
}
}
18 changes: 18 additions & 0 deletions lib/models/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ class Message {
int get hashCode {
return Object.hash(role, type, content, imageUrls);
}

Map<String, dynamic> toJson() {
return {
'role': role,
'type': type,
'content': content,
'imageUrls': imageUrls,
};
}

factory Message.fromJson(Map<String, dynamic> json) {
return Message(
role: json['role'],
type: json['type'],
content: json['content'],
imageUrls: json['imageUrls']?.cast<String>(),
);
}
}

extension ToContext on List<Message> {
Expand Down
18 changes: 2 additions & 16 deletions lib/pages/chat_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sydney_webui/controller.dart';
import 'package:sydney_webui/widgets/chat_drawer.dart';
import 'package:sydney_webui/widgets/message_list.dart';
import 'package:sydney_webui/widgets/prompt_input.dart';
import 'package:sydney_webui/widgets/settings_dialog.dart';
Expand All @@ -24,22 +25,7 @@ class ChatPage extends StatelessWidget {
icon: const Icon(Icons.settings))
],
),
drawer: Drawer(
child: ListView(
children: [
Obx(() => ListTile(
leading: const Icon(Icons.add),
title: const Text('New Conversation'),
onTap: controller.isGenerating.value
? null
: () {
controller.newConversation();
Get.back();
},
)),
],
),
),
drawer: const ChatDrawer(),
body: const Padding(
padding: EdgeInsets.all(16.0),
child: Column(
Expand Down
41 changes: 41 additions & 0 deletions lib/widgets/chat_drawer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sydney_webui/controller.dart';

class ChatDrawer extends StatelessWidget {
const ChatDrawer({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
final controller = Get.find<Controller>();

return Drawer(
child: ListView(
children: [
Obx(() => ListTile(
leading: const Icon(Icons.add),
title: const Text('New Conversation'),
onTap: controller.isGenerating.value
? null
: () {
controller.newConversation();
Get.back();
},
)),
ListTile(
leading: const Icon(Icons.copy_all),
title: const Text('Copy Conversation'),
onTap: controller.copyConversation,
),
Obx(() => ListTile(
leading: const Icon(Icons.add_box_outlined),
title: const Text('Import Conversation'),
onTap: controller.isGenerating.value
? null
: controller.importConversation,
)),
],
),
);
}
}

0 comments on commit 86a366b

Please sign in to comment.