feat: Implement clear chat, delete conversation, mute, and report options within the chat screen.

This commit is contained in:
pradeepkumar
2026-03-19 13:57:14 +05:30
parent b848b0260b
commit a837e5ac12
5 changed files with 217 additions and 23 deletions

View File

@@ -527,6 +527,48 @@ class MessagingNotifier extends StateNotifier<MessagingState> {
return null;
}
}
Future<void> clearChat(String conversationId) async {
try {
await _repository.clearChat(conversationId);
// Clear local messages
final updatedMessages =
Map<String, List<ChatMessage>>.from(state.messages)
..[conversationId] = [];
// Update conversation preview
final updatedConversations = state.conversations.map((c) {
if (c.id == conversationId) {
return c.copyWith(lastMessageText: '', unreadCount: 0);
}
return c;
}).toList();
state = state.copyWith(
messages: updatedMessages,
conversations: updatedConversations,
);
} catch (_) {
state = state.copyWith(error: 'Failed to clear chat');
}
}
Future<void> deleteConversation(String conversationId) async {
try {
await _repository.deleteConversation(conversationId);
// Remove from local state
final updatedMessages =
Map<String, List<ChatMessage>>.from(state.messages)
..remove(conversationId);
final updatedConversations =
state.conversations.where((c) => c.id != conversationId).toList();
state = state.copyWith(
messages: updatedMessages,
conversations: updatedConversations,
clearActiveConversation: true,
);
} catch (_) {
state = state.copyWith(error: 'Failed to delete conversation');
}
}
}
final messagingProvider =

View File

@@ -29,6 +29,7 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
final ScrollController _scrollController = ScrollController();
final FocusNode _messageFocusNode = FocusNode();
bool _isComposing = false;
bool _isMuted = false;
int _previousMessageCount = 0;
final SocketService _socket = SocketService();
@@ -145,6 +146,107 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
});
}
void _handleMenuAction(String action, String? agentProfileId) {
switch (action) {
case 'clear_chat':
_showConfirmDialog(
title: 'Clear Chat',
message: 'Are you sure you want to clear all messages? This cannot be undone.',
onConfirm: () {
ref.read(messagingProvider.notifier).clearChat(widget.conversationId);
},
);
break;
case 'mute':
setState(() => _isMuted = !_isMuted);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(_isMuted ? 'Notifications muted' : 'Notifications unmuted'),
duration: const Duration(seconds: 2),
),
);
break;
case 'report':
_showReportDialog();
break;
case 'delete':
_showConfirmDialog(
title: 'Delete Conversation',
message: 'Are you sure you want to delete this conversation? This cannot be undone.',
onConfirm: () {
ref.read(messagingProvider.notifier).deleteConversation(widget.conversationId);
context.go('/messages');
},
isDanger: true,
);
break;
}
}
void _showConfirmDialog({
required String title,
required String message,
required VoidCallback onConfirm,
bool isDanger = false,
}) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text(title),
content: Text(message),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.pop(ctx);
onConfirm();
},
child: Text(
'Confirm',
style: TextStyle(color: isDanger ? Colors.red : null),
),
),
],
),
);
}
void _showReportDialog() {
final controller = TextEditingController();
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Report User'),
content: TextField(
controller: controller,
maxLines: 3,
decoration: const InputDecoration(
hintText: 'Describe the issue...',
border: OutlineInputBorder(),
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.pop(ctx);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Report submitted. Thank you.')),
);
},
child: const Text('Submit'),
),
],
),
);
}
void _handleBack() {
ref.read(messagingProvider.notifier).setActiveConversation(null);
context.go('/messages');
@@ -255,6 +357,7 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
name: otherPartyName,
isOnline: isOnline,
avatar: otherPartyAvatar,
agentProfileId: conversation?.agentProfileId,
),
Expanded(
child: messagingState.isLoadingMessages && messages.isEmpty
@@ -289,6 +392,7 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
required String name,
required bool isOnline,
String? avatar,
String? agentProfileId,
}) {
final initials = _getInitials(name);
@@ -385,30 +489,44 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
],
),
),
// Three dots (vertical)
GestureDetector(
onTap: () {},
behavior: HitTestBehavior.opaque,
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Icon(
Icons.more_vert,
size: 20,
color: AppColors.primaryDark,
),
// Three dots menu (matches web: Clear Chat, Mute, Report, Delete)
PopupMenuButton<String>(
icon: const Icon(
Icons.more_vert,
size: 20,
color: AppColors.primaryDark,
),
padding: EdgeInsets.zero,
onSelected: (value) => _handleMenuAction(value, agentProfileId),
itemBuilder: (_) => [
const PopupMenuItem(
value: 'clear_chat',
child: Text('Clear Chat'),
),
PopupMenuItem(
value: 'mute',
child: Text(_isMuted ? 'Unmute Notifications' : 'Mute Notifications'),
),
const PopupMenuItem(
value: 'report',
child: Text('Report User'),
),
const PopupMenuItem(
value: 'delete',
child: Text(
'Delete Conversation',
style: TextStyle(color: Colors.red),
),
),
],
),
// Star icon
GestureDetector(
onTap: () {},
behavior: HitTestBehavior.opaque,
child: Padding(
padding: const EdgeInsets.only(right: 4),
child: Icon(
Icons.star_rounded,
size: 24,
color: AppColors.accentOrange,
),
Padding(
padding: const EdgeInsets.only(right: 4),
child: Icon(
Icons.star_rounded,
size: 24,
color: AppColors.accentOrange,
),
),
],