import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../../core/utils/debug_logger.dart'; // Global attachment cache state class AttachmentCacheState { final Map imageDataCache; final Map loadingStates; final Map errorStates; AttachmentCacheState({ required this.imageDataCache, required this.loadingStates, required this.errorStates, }); AttachmentCacheState copyWith({ Map? imageDataCache, Map? loadingStates, Map? errorStates, }) { return AttachmentCacheState( imageDataCache: imageDataCache ?? this.imageDataCache, loadingStates: loadingStates ?? this.loadingStates, errorStates: errorStates ?? this.errorStates, ); } } class AttachmentCacheNotifier extends StateNotifier { AttachmentCacheNotifier() : super(AttachmentCacheState( imageDataCache: {}, loadingStates: {}, errorStates: {}, )); void cacheImageData(String attachmentId, String imageData) { DebugLogger.log('Caching image data for: $attachmentId'); state = state.copyWith( imageDataCache: { ...state.imageDataCache, attachmentId: imageData, }, ); // Limit cache size to prevent memory issues if (state.imageDataCache.length > 100) { final newCache = Map.from(state.imageDataCache); final keysToRemove = newCache.keys.take(20).toList(); for (final key in keysToRemove) { newCache.remove(key); state.loadingStates.remove(key); state.errorStates.remove(key); } state = state.copyWith(imageDataCache: newCache); } } String? getCachedImageData(String attachmentId) { return state.imageDataCache[attachmentId]; } void setLoadingState(String attachmentId, bool isLoading) { state = state.copyWith( loadingStates: { ...state.loadingStates, attachmentId: isLoading, }, ); } bool isLoading(String attachmentId) { return state.loadingStates[attachmentId] ?? false; } void setErrorState(String attachmentId, String? error) { if (error == null) { final newErrorStates = Map.from(state.errorStates); newErrorStates.remove(attachmentId); state = state.copyWith(errorStates: newErrorStates); } else { state = state.copyWith( errorStates: { ...state.errorStates, attachmentId: error, }, ); } } String? getErrorState(String attachmentId) { return state.errorStates[attachmentId]; } void clearCache() { state = AttachmentCacheState( imageDataCache: {}, loadingStates: {}, errorStates: {}, ); } void clearAttachmentCache(String attachmentId) { final newImageCache = Map.from(state.imageDataCache); final newLoadingStates = Map.from(state.loadingStates); final newErrorStates = Map.from(state.errorStates); newImageCache.remove(attachmentId); newLoadingStates.remove(attachmentId); newErrorStates.remove(attachmentId); state = AttachmentCacheState( imageDataCache: newImageCache, loadingStates: newLoadingStates, errorStates: newErrorStates, ); } } final attachmentCacheProvider = StateNotifierProvider((ref) { return AttachmentCacheNotifier(); }); // Helper providers for easier access final cachedImageDataProvider = Provider.family((ref, attachmentId) { final cache = ref.watch(attachmentCacheProvider); return cache.imageDataCache[attachmentId]; }); final attachmentLoadingStateProvider = Provider.family((ref, attachmentId) { final cache = ref.watch(attachmentCacheProvider); return cache.loadingStates[attachmentId] ?? false; }); final attachmentErrorStateProvider = Provider.family((ref, attachmentId) { final cache = ref.watch(attachmentCacheProvider); return cache.errorStates[attachmentId]; });