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

38 lines
1.2 KiB
Dart

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/logging_service.dart';
import 'package:mc_gallery/locator.dart';
class ImageCacheManagerService with LoggingService {
ImageCacheManagerService({
required AppLifecycleService appLifecycleService,
}) : _appLifecycleService = appLifecycleService {
_init();
}
final AppLifecycleService _appLifecycleService;
Future<void> emptyCache() async => await DefaultCacheManager().emptyCache();
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:
info('Discarding cached images');
await DefaultCacheManager().emptyCache();
}
},
);
}
static ImageCacheManagerService get locate => Locator.locate();
}