Skip to content

Commit

Permalink
feat: delete msg
Browse files Browse the repository at this point in the history
  • Loading branch information
daodao97 committed Dec 16, 2024
1 parent e3d7a95 commit 92f4f27
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 210 deletions.
3 changes: 3 additions & 0 deletions lib/llm/claude_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ class ClaudeClient extends BaseLLMClient {

switch (eventType) {
case 'content_block_start':
final content = event['content_block'];
final text = content['text'];
yield LLMResponse(content: text);
case 'content_block_delta':
final delta = event['delta'];
if (delta['type'] == 'text_delta') {
Expand Down
122 changes: 90 additions & 32 deletions lib/page/layout/chat_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class ChatHistoryPanel extends StatelessWidget {
child: Stack(
children: [
Container(
padding: const EdgeInsets.only(top: 35, left: 8, right: 8),
padding:
const EdgeInsets.only(top: 35, left: 8, right: 8, bottom: 50),
child: ListView.builder(
itemCount: chatProvider.chats.length,
itemBuilder: (context, index) {
Expand All @@ -25,22 +26,25 @@ class ChatHistoryPanel extends StatelessWidget {
return Container(
margin: const EdgeInsets.symmetric(vertical: 2),
decoration: BoxDecoration(
color: isActive ? Colors.grey.withOpacity(0.1) : null,
boxShadow: isActive
? [
BoxShadow(
color: Colors.grey.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 2,
offset: const Offset(0, 1),
)
]
: null,
color: isActive ? Colors.grey.withAlpha(25) : null,
borderRadius: BorderRadius.circular(4),
),
child: ListTile(
dense: true,
visualDensity: const VisualDensity(vertical: -4),
leading: chatProvider.isSelectMode
? Checkbox(
value:
chatProvider.selectedChats.contains(chat.id),
onChanged: (bool? value) {
if (value == true) {
chatProvider.selectChat(chat.id);
} else {
chatProvider.unselectChat(chat.id);
}
},
)
: null,
title: Row(
children: [
Expanded(
Expand All @@ -57,40 +61,94 @@ class ChatHistoryPanel extends StatelessWidget {
),
],
),
subtitle: null,
onTap: () => chatProvider.setActiveChat(chat),
onTap: () => chatProvider.isSelectMode
? chatProvider.toggleSelectChat(chat.id)
: chatProvider.setActiveChat(chat),
),
);
},
),
),
// 右上角的开关按钮
Positioned(
top: 0,
right: 4,
child: IconButton(
icon: const Icon(Icons.menu_open),
icon: const Icon(Icons.menu),
onPressed: onToggle,
),
),
// 左下角的设置按钮
// 底部操作栏
Positioned(
bottom: 8,
left: 8,
child: IconButton(
icon: const Icon(Icons.settings),
onPressed: () {
showDialog(
context: context,
builder: (context) => Dialog(
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.8,
height: MediaQuery.of(context).size.height * 0.8,
child: const SettingPage(),
),
bottom: 0,
left: 0,
right: 0,
child: Container(
height: 50,
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Row(
children: [
IconButton(
icon: const Icon(Icons.settings),
onPressed: () {
showDialog(
context: context,
builder: (context) => Dialog(
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.8,
height: MediaQuery.of(context).size.height * 0.8,
child: const SettingPage(),
),
),
);
},
),
);
},
IconButton(
icon: Icon(chatProvider.isSelectMode
? Icons.close
: Icons.delete),
onPressed: () {
if (chatProvider.isSelectMode) {
chatProvider.exitSelectMode();
} else {
chatProvider.enterSelectMode();
}
},
),
if (chatProvider.isSelectMode) ...[
IconButton(
icon: const Icon(Icons.select_all),
onPressed: () => chatProvider.toggleSelectAll(),
),
ElevatedButton(
onPressed: chatProvider.selectedChats.isNotEmpty
? () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('确认删除'),
content: const Text('确定要删除选中的对话吗?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('取消'),
),
TextButton(
onPressed: () {
chatProvider.deleteSelectedChats();
Navigator.pop(context);
},
child: const Text('确定'),
),
],
),
);
}
: null,
child: const Text('删除'),
),
],
],
),
),
),
],
Expand Down
67 changes: 40 additions & 27 deletions lib/page/layout/layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,47 @@ class _LayoutPageState extends State<LayoutPage> {
}),
),
// model select
DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
value: chatModelProvider.currentModel,
items: ProviderManager
.chatModelProvider.availableModels
.map((model) => DropdownMenuItem(
value: model.name,
child: Text(model.label),
))
.toList(),
onChanged: (String? value) {
if (value != null) {
setState(() {
ProviderManager
.chatModelProvider.currentModel = value;
});
}
},
menuMaxHeight: 200,
elevation: 20,
isDense: true,
underline: Container(
height: 0,
Container(
constraints: const BoxConstraints(maxWidth: 150),
decoration: BoxDecoration(
// color: Colors.grey[50],
borderRadius: BorderRadius.circular(4),
),
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
value: chatModelProvider.currentModel,
items: ProviderManager
.chatModelProvider.availableModels
.map((model) => DropdownMenuItem(
value: model.name,
child: Tooltip(
message: model.label,
child: Text(
model.label,
overflow: TextOverflow.ellipsis,
),
),
))
.toList(),
onChanged: (String? value) {
if (value != null) {
setState(() {
ProviderManager.chatModelProvider
.currentModel = value;
});
}
},
menuMaxHeight: 200,
elevation: 20,
isDense: true,
underline: Container(
height: 0,
),
isExpanded: true,
alignment: AlignmentDirectional.centerStart,
),
isExpanded: false,
alignment: AlignmentDirectional.centerStart,
),
),
),
Expand Down
68 changes: 68 additions & 0 deletions lib/provider/chat_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ChatProvider extends ChangeNotifier {

Chat? _activeChat;
List<Chat> _chats = [];
bool isSelectMode = false;
Set<int?> selectedChats = {};

Chat? get activeChat => _activeChat;
List<Chat> get chats => _chats;
Expand Down Expand Up @@ -81,4 +83,70 @@ class ChatProvider extends ChangeNotifier {
}
notifyListeners();
}

void enterSelectMode() {
isSelectMode = true;
selectedChats.clear();
notifyListeners();
}

void exitSelectMode() {
isSelectMode = false;
selectedChats.clear();
notifyListeners();
}

void selectChat(int? chatId) {
selectedChats.add(chatId);
notifyListeners();
}

void unselectChat(int? chatId) {
selectedChats.remove(chatId);
notifyListeners();
}

void toggleSelectChat(int? chatId) {
if (selectedChats.contains(chatId)) {
selectedChats.remove(chatId);
} else {
selectedChats.add(chatId);
}
notifyListeners();
}

void toggleSelectAll() {
if (selectedChats.length == chats.length) {
selectedChats.clear();
} else {
selectedChats = chats.map((chat) => chat.id).toSet();
}
notifyListeners();
}

Future<void> deleteSelectedChats() async {
final chatDao = ChatDao();

// 从数据库中删除选中的聊天记录
for (var chatId in selectedChats) {
if (chatId != null) {
await chatDao.delete(chatId.toString());
}
}

// 重新加载聊天列表
await loadChats();

// 如果当前活动的聊天被删除,需要更新activeChat
if (selectedChats.contains(activeChat?.id)) {
if (_chats.isNotEmpty) {
await setActiveChat(_chats.first);
} else {
await clearActiveChat();
}
}

// 退出选择模式
exitSelectMode();
}
}
4 changes: 1 addition & 3 deletions lib/widgets/markit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ class HighlightView extends StatelessWidget {
fontFamily: _defaultFontFamily,
color: theme[_rootKey]?.color ?? _defaultFontColor,
);
if (textStyle != null) {
textStyle = textStyle.merge(textStyle);
}
textStyle = textStyle.merge(textStyle);

return Container(
color: theme[_rootKey]?.backgroundColor ?? _defaultBackgroundColor,
Expand Down
Loading

0 comments on commit 92f4f27

Please sign in to comment.