import 'dart:ui'; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:mc_gallery/features/core/services/app_lifecycle_service.dart'; import 'package:mc_gallery/features/core/services/local_storage_service.dart'; import 'package:mc_gallery/features/core/services/logging_service.dart'; import 'package:mc_gallery/locator.dart'; class ImageCacheManagerService with LoggingService { ImageCacheManagerService( {required AppLifecycleService appLifecycleService, required LocalStorageService localStorageService}) : _appLifecycleService = appLifecycleService, _localStorageService = localStorageService { _init(); } final AppLifecycleService _appLifecycleService; final LocalStorageService _localStorageService; final _cacheManager = DefaultCacheManager(); Future emptyCache() async => await _cacheManager.emptyCache(); Future _init() async { _appLifecycleService.addListener( tag: runtimeType.toString(), listener: (final appLifecycleState) async { switch (appLifecycleState) { case AppLifecycleState.resumed: break; case AppLifecycleState.inactive: case AppLifecycleState.paused: case AppLifecycleState.detached: info('Discarding cached images'); await _cacheManager.emptyCache(); _localStorageService.resetFavourites(); } }, ); } static ImageCacheManagerService get locate => Locator.locate(); }