mc_gallery/lib/features/home/services/image_cache_manager_service...

47 lines
1.5 KiB
Dart
Raw Normal View History

2022-12-21 22:40:39 +00:00
import 'dart:ui';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
2022-12-25 21:17:59 +00:00
import '/features/core/services/app_lifecycle_service.dart';
import '/features/core/services/local_storage_service.dart';
import '/features/core/services/logging_service.dart';
import '/locator.dart';
/// Handles maintaining the caching of downloaded images
class ImageCacheManagerService {
2022-12-25 00:55:53 +00:00
ImageCacheManagerService(
{required AppLifecycleService appLifecycleService,
required LocalStorageService localStorageService})
: _appLifecycleService = appLifecycleService,
_localStorageService = localStorageService {
2022-12-21 22:40:39 +00:00
_init();
}
final AppLifecycleService _appLifecycleService;
2022-12-25 00:55:53 +00:00
final LocalStorageService _localStorageService;
2022-12-25 21:17:59 +00:00
final LoggingService _loggingService = LoggingService.locate;
2022-12-25 00:55:53 +00:00
final _cacheManager = DefaultCacheManager();
2022-12-21 22:40:39 +00:00
2023-01-01 12:04:22 +00:00
void emptyCache() => _cacheManager.emptyCache();
2022-12-21 22:40:39 +00:00
Future<void> _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:
2022-12-25 21:17:59 +00:00
_loggingService.info('Discarding cached images');
2022-12-25 00:55:53 +00:00
await _cacheManager.emptyCache();
_localStorageService.resetFavourites();
2022-12-21 22:40:39 +00:00
}
},
);
}
static ImageCacheManagerService get locate => Locator.locate();
}