refactor: centralize conversation cache management

- Introduced a new method `refreshConversationsCache` to streamline the invalidation of the conversations provider and optionally the folders provider.
- Updated various components to utilize the new cache management method, enhancing code clarity and reducing redundancy.
- This refactor improves the efficiency of conversation and folder synchronization across the application.
This commit is contained in:
cogwheel0
2025-10-02 00:30:14 +05:30
parent 73ccb14b20
commit ff02af1e89
7 changed files with 122 additions and 68 deletions

View File

@@ -3,7 +3,6 @@ import 'dart:io' show Platform;
import 'package:conduit/core/providers/app_providers.dart';
import 'package:conduit/l10n/app_localizations.dart';
import 'package:conduit/shared/theme/theme_extensions.dart';
import 'package:conduit/shared/utils/ui_utils.dart';
import 'package:conduit/shared/widgets/conduit_components.dart';
import 'package:conduit/shared/widgets/modal_safe_area.dart';
import 'package:conduit/shared/widgets/sheet_handle.dart';
@@ -123,7 +122,7 @@ Future<void> showConversationContextMenu({
await chat.pinConversation(ref, conversation.id, !isPinned);
} catch (_) {
if (!context.mounted) return;
UiUtils.showMessage(context, errorMessage, isError: true);
await _showConversationError(context, errorMessage);
}
}
@@ -133,7 +132,7 @@ Future<void> showConversationContextMenu({
await chat.archiveConversation(ref, conversation.id, !isArchived);
} catch (_) {
if (!context.mounted) return;
UiUtils.showMessage(context, errorMessage, isError: true);
await _showConversationError(context, errorMessage);
}
}
@@ -221,7 +220,7 @@ Future<void> _renameConversation(
if (api == null) throw Exception('No API service');
await api.updateConversation(conversationId, title: newName);
HapticFeedback.selectionClick();
ref.invalidate(conversationsProvider);
refreshConversationsCache(ref);
final active = ref.read(activeConversationProvider);
if (active?.id == conversationId) {
ref
@@ -230,7 +229,7 @@ Future<void> _renameConversation(
}
} catch (_) {
if (!context.mounted) return;
UiUtils.showMessage(context, renameError, isError: true);
await _showConversationError(context, renameError);
}
}
@@ -262,9 +261,29 @@ Future<void> _confirmAndDeleteConversation(
ref.read(activeConversationProvider.notifier).clear();
ref.read(chat.chatMessagesProvider.notifier).clearMessages();
}
ref.invalidate(conversationsProvider);
refreshConversationsCache(ref);
} catch (_) {
if (!context.mounted) return;
UiUtils.showMessage(context, deleteError, isError: true);
await _showConversationError(context, deleteError);
}
}
Future<void> _showConversationError(
BuildContext context,
String message,
) async {
if (!context.mounted) return;
final l10n = AppLocalizations.of(context)!;
final theme = context.conduitTheme;
await ThemedDialogs.show<void>(
context,
title: l10n.errorMessage,
content: Text(message, style: TextStyle(color: theme.textSecondary)),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(l10n.ok),
),
],
);
}