mirror of
https://github.com/we-promise/sure.git
synced 2026-05-24 13:04:56 +00:00
feat(mobile): add mass delete for chats (#1779)
* feat(mobile): add mass delete for chats
Long-press any chat to enter selection mode, tap items to select/deselect,
use Select All to toggle all, then delete with a single confirmation.
Swipe-to-delete continues to work outside selection mode.
* fix(mobile): address PR review comments on mass delete
- Wrap each deleteChat call in its own try-catch so a single network
failure doesn't abort the entire Future.wait operation
- Add null-safe casting for deletedCount and failedIds in provider
- Fix misleading error snackbar copy ("Some chats could not be deleted"
implied partial failure; provider only returns false on total failure)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
committed by
GitHub
parent
6402f1dd08
commit
d5bfccc4c7
@@ -321,6 +321,42 @@ class ChatProvider with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete multiple chats
|
||||
Future<bool> deleteMultipleChats({
|
||||
required String accessToken,
|
||||
required List<String> chatIds,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _chatService.deleteMultipleChats(
|
||||
accessToken: accessToken,
|
||||
chatIds: chatIds,
|
||||
);
|
||||
|
||||
final deletedCount = (result['deletedCount'] as int?) ?? 0;
|
||||
if (result['success'] == true || deletedCount > 0) {
|
||||
final failedIds = ((result['failedIds'] as List?) ?? []).cast<String>().toSet();
|
||||
final deleted = chatIds.toSet().difference(failedIds);
|
||||
_chats.removeWhere((c) => deleted.contains(c.id));
|
||||
|
||||
if (_currentChat != null && deleted.contains(_currentChat!.id)) {
|
||||
_currentChat = null;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
|
||||
_errorMessage = 'Failed to delete chats';
|
||||
notifyListeners();
|
||||
return false;
|
||||
} catch (e) {
|
||||
debugPrint('deleteMultipleChats error: $e');
|
||||
_errorMessage = 'Something went wrong. Please try again.';
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Start polling for new messages (AI responses)
|
||||
void _startPolling(String accessToken, String chatId) {
|
||||
_pollingTimer?.cancel();
|
||||
|
||||
@@ -12,6 +12,9 @@ class ChatListScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ChatListScreenState extends State<ChatListScreen> {
|
||||
bool _isSelectionMode = false;
|
||||
final Set<String> _selectedChatIds = {};
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -35,6 +38,89 @@ class _ChatListScreenState extends State<ChatListScreen> {
|
||||
await _loadChats();
|
||||
}
|
||||
|
||||
void _toggleSelectionMode() {
|
||||
setState(() {
|
||||
_isSelectionMode = !_isSelectionMode;
|
||||
_selectedChatIds.clear();
|
||||
});
|
||||
}
|
||||
|
||||
void _toggleSelectAll(List<String> allIds) {
|
||||
setState(() {
|
||||
if (_selectedChatIds.length == allIds.length) {
|
||||
_selectedChatIds.clear();
|
||||
} else {
|
||||
_selectedChatIds
|
||||
..clear()
|
||||
..addAll(allIds);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _toggleChatSelection(String id) {
|
||||
setState(() {
|
||||
if (_selectedChatIds.contains(id)) {
|
||||
_selectedChatIds.remove(id);
|
||||
} else {
|
||||
_selectedChatIds.add(id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _deleteSelectedChats() async {
|
||||
final authProvider = Provider.of<AuthProvider>(context, listen: false);
|
||||
final chatProvider = Provider.of<ChatProvider>(context, listen: false);
|
||||
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Delete Chats'),
|
||||
content: Text(
|
||||
'Delete ${_selectedChatIds.length} chat(s)? This cannot be undone.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Delete', style: TextStyle(color: Colors.red)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed != true || !mounted) return;
|
||||
|
||||
final accessToken = await authProvider.getValidAccessToken();
|
||||
if (accessToken == null) {
|
||||
await authProvider.logout();
|
||||
return;
|
||||
}
|
||||
|
||||
final success = await chatProvider.deleteMultipleChats(
|
||||
accessToken: accessToken,
|
||||
chatIds: _selectedChatIds.toList(),
|
||||
);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
setState(() {
|
||||
_isSelectionMode = false;
|
||||
_selectedChatIds.clear();
|
||||
});
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
success ? 'Chats deleted' : 'Failed to delete chats',
|
||||
),
|
||||
backgroundColor: success ? Colors.green : Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _openNewChat() async {
|
||||
if (!mounted) return;
|
||||
|
||||
@@ -74,17 +160,38 @@ class _ChatListScreenState extends State<ChatListScreen> {
|
||||
title: const Text('Chats'),
|
||||
centerTitle: false,
|
||||
actions: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 12, right: 12),
|
||||
child: InkWell(
|
||||
onTap: _handleRefresh,
|
||||
child: const SizedBox(
|
||||
width: 36,
|
||||
height: 36,
|
||||
child: Icon(Icons.refresh),
|
||||
if (_isSelectionMode) ...[
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete),
|
||||
onPressed: _selectedChatIds.isNotEmpty ? _deleteSelectedChats : null,
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.select_all),
|
||||
onPressed: () {
|
||||
final allIds = Provider.of<ChatProvider>(context, listen: false)
|
||||
.chats
|
||||
.map((c) => c.id)
|
||||
.toList();
|
||||
_toggleSelectAll(allIds);
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: _toggleSelectionMode,
|
||||
),
|
||||
] else ...[
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 12, right: 12),
|
||||
child: InkWell(
|
||||
onTap: _handleRefresh,
|
||||
child: const SizedBox(
|
||||
width: 36,
|
||||
height: 36,
|
||||
child: Icon(Icons.refresh),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
body: Consumer<ChatProvider>(
|
||||
@@ -166,9 +273,12 @@ class _ChatListScreenState extends State<ChatListScreen> {
|
||||
itemCount: chatProvider.chats.length,
|
||||
itemBuilder: (context, index) {
|
||||
final chat = chatProvider.chats[index];
|
||||
final isSelected = _selectedChatIds.contains(chat.id);
|
||||
return Dismissible(
|
||||
key: Key(chat.id),
|
||||
direction: DismissDirection.endToStart,
|
||||
direction: _isSelectionMode
|
||||
? DismissDirection.none
|
||||
: DismissDirection.endToStart,
|
||||
background: Container(
|
||||
color: Colors.red,
|
||||
alignment: Alignment.centerRight,
|
||||
@@ -208,13 +318,18 @@ class _ChatListScreenState extends State<ChatListScreen> {
|
||||
}
|
||||
},
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: colorScheme.primaryContainer,
|
||||
child: Icon(
|
||||
Icons.chat,
|
||||
color: colorScheme.onPrimaryContainer,
|
||||
),
|
||||
),
|
||||
leading: _isSelectionMode
|
||||
? Checkbox(
|
||||
value: isSelected,
|
||||
onChanged: (_) => _toggleChatSelection(chat.id),
|
||||
)
|
||||
: CircleAvatar(
|
||||
backgroundColor: colorScheme.primaryContainer,
|
||||
child: Icon(
|
||||
Icons.chat,
|
||||
color: colorScheme.onPrimaryContainer,
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
chat.title,
|
||||
maxLines: 1,
|
||||
@@ -223,7 +338,7 @@ class _ChatListScreenState extends State<ChatListScreen> {
|
||||
subtitle: chat.lastMessageAt != null
|
||||
? Text(_formatDateTime(chat.lastMessageAt!))
|
||||
: null,
|
||||
trailing: chat.messageCount != null
|
||||
trailing: chat.messageCount != null && !_isSelectionMode
|
||||
? Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
@@ -241,6 +356,10 @@ class _ChatListScreenState extends State<ChatListScreen> {
|
||||
)
|
||||
: null,
|
||||
onTap: () async {
|
||||
if (_isSelectionMode) {
|
||||
_toggleChatSelection(chat.id);
|
||||
return;
|
||||
}
|
||||
await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
@@ -249,6 +368,14 @@ class _ChatListScreenState extends State<ChatListScreen> {
|
||||
);
|
||||
_loadChats();
|
||||
},
|
||||
onLongPress: _isSelectionMode
|
||||
? null
|
||||
: () {
|
||||
setState(() {
|
||||
_isSelectionMode = true;
|
||||
_selectedChatIds.add(chat.id);
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -331,6 +331,34 @@ class ChatService {
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete multiple chats in parallel
|
||||
Future<Map<String, dynamic>> deleteMultipleChats({
|
||||
required String accessToken,
|
||||
required List<String> chatIds,
|
||||
}) async {
|
||||
final results = await Future.wait(
|
||||
chatIds.map((id) async {
|
||||
try {
|
||||
return await deleteChat(accessToken: accessToken, chatId: id);
|
||||
} catch (_) {
|
||||
return {'success': false};
|
||||
}
|
||||
}),
|
||||
eagerError: false,
|
||||
);
|
||||
final failedIds = chatIds
|
||||
.asMap()
|
||||
.entries
|
||||
.where((e) => results[e.key]['success'] != true)
|
||||
.map((e) => e.value)
|
||||
.toList();
|
||||
return {
|
||||
'success': failedIds.isEmpty,
|
||||
'deletedCount': chatIds.length - failedIds.length,
|
||||
'failedIds': failedIds,
|
||||
};
|
||||
}
|
||||
|
||||
/// Retry the last assistant response in a chat
|
||||
Future<Map<String, dynamic>> retryMessage({
|
||||
required String accessToken,
|
||||
|
||||
Reference in New Issue
Block a user