Skip to content

Commit

Permalink
rename and delete chat thread from app bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Dat-TG committed Dec 29, 2023
1 parent afba54b commit e25c940
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 15 deletions.
2 changes: 2 additions & 0 deletions lib/core/widgets/chat_thread.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ class _ChatThreadWidgetState extends State<ChatThreadWidget> {
items: [
PopupMenuItem(
child: ListTile(
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.edit_outlined),
title: Text('Rename'),
),
value: 'rename',
),
PopupMenuItem(
child: ListTile(
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.delete_outline),
title: Text('Delete'),
),
Expand Down
128 changes: 113 additions & 15 deletions lib/core/widgets/main_app_bar.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:boilerplate/core/widgets/change_chat_thread_name_dialog.dart';
import 'package:boilerplate/di/service_locator.dart';
import 'package:boilerplate/domain/entity/message/chat_thread.dart';
import 'package:boilerplate/presentation/chat_screen/store/chat_store.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
Expand All @@ -13,13 +15,61 @@ class MainAppBar extends StatefulWidget {

class _MainAppBarState extends State<MainAppBar> {
ChatStore _chatStore = getIt<ChatStore>();

Future<void> _showDeleteConfirmation(BuildContext context) async {
return showDialog<void>(
context: context,
barrierDismissible:
false, // Prevents dismissing the dialog on outside tap
builder: (BuildContext context) {
return AlertDialog(
title: Text('Confirm Delete'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Are you sure you want to delete this chat thread?'),
],
),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
},
child: Text('Cancel'),
),
ElevatedButton(
onPressed: () {
// Perform deletion logic here
// For example, you might call a function to delete the chat thread
// deleteChatThread();
int currentId = _chatStore.id;

_chatStore.setChatThreadId(-1);

_chatStore.deleteChatThread(currentId);
Navigator.of(context).pop(); // Close the dialog after deletion
},
child: Text(
'Delete',
style: TextStyle(
color: Colors.white,
),
),
),
],
);
},
);
}

@override
Widget build(BuildContext context) {
return AppBar(
title: Text('ChatGPT'),
actions: [
Observer(builder: (context) {
return IconButton(
return Observer(builder: (context) {
return AppBar(
title: Text('ChatGPT'),
actions: [
IconButton(
icon: FaIcon(FontAwesomeIcons.penToSquare),
splashRadius: 20,
iconSize: 20,
Expand All @@ -29,15 +79,63 @@ class _MainAppBarState extends State<MainAppBar> {
_chatStore.setChatThreadId(-1);
}
: null,
);
}),
IconButton(
icon: FaIcon(FontAwesomeIcons.ellipsisVertical),
splashRadius: 20,
iconSize: 20,
onPressed: () {},
),
],
);
),
IconButton(
icon: FaIcon(FontAwesomeIcons.ellipsisVertical),
splashRadius: 20,
iconSize: 20,
onPressed: _chatStore.id > 0
? () {
showMenu(
context: context,
position: RelativeRect.fromLTRB(
200,
80,
0,
0,
),
items: [
PopupMenuItem(
child: ListTile(
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.edit_outlined),
title: Text('Rename'),
),
value: 'rename',
),
PopupMenuItem(
child: ListTile(
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.delete_outline),
title: Text('Delete'),
),
value: 'delete',
),
],
elevation: 8.0,
).then((value) {
ChatThread chatThread = _chatStore.chatThreads
.firstWhere((element) => element.id == _chatStore.id);
if (value == 'rename') {
showDialog(
context: context,
builder: (BuildContext context) {
return ChangeChatThreadNameDialog(
chatThread: chatThread,
);
},
);
// Handle edit option
} else if (value == 'delete') {
// Handle delete option
_showDeleteConfirmation(context);
}
});
}
: null,
),
],
);
});
}
}

0 comments on commit e25c940

Please sign in to comment.