added image cache managing
This commit is contained in:
parent
b7045fc242
commit
2ff4d44d25
14 changed files with 188 additions and 80 deletions
37
lib/features/home/services/image_cache_manager_service.dart
Normal file
37
lib/features/home/services/image_cache_manager_service.dart
Normal file
|
@ -0,0 +1,37 @@
|
|||
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();
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
import 'package:mc_gallery/features/core/services/logging_service.dart';
|
||||
import 'package:mc_gallery/features/home/data/models/image_model.dart';
|
||||
import 'package:mc_gallery/locator.dart';
|
||||
|
||||
|
@ -8,20 +9,33 @@ import '../abstracts/images_api.dart';
|
|||
/// Since this is very simple use-case, this is the only interface. For complex (actual CRUD-based) I/O,
|
||||
/// an additional Repository layer interface can be used between [ImagesService] and [ImagesApi].
|
||||
class ImagesService {
|
||||
ImagesService({required ImagesApi imagesApi}) : _imagesApi = imagesApi {
|
||||
ImagesService({
|
||||
required ImagesApi imagesApi,
|
||||
required LoggingService loggingService,
|
||||
}) : _imagesApi = imagesApi,
|
||||
_loggingService = loggingService {
|
||||
_init();
|
||||
}
|
||||
|
||||
final ImagesApi _imagesApi;
|
||||
final LoggingService _loggingService;
|
||||
|
||||
late final Iterable<ImageModel> _imageModels;
|
||||
Iterable<ImageModel> get imageModels => _imageModels;
|
||||
|
||||
Future<void> _init() async {
|
||||
_loggingService.info('Fetching and creating image models...');
|
||||
_imageModels = await _imagesApi.fetchImageUri(token: '');
|
||||
|
||||
_imageModels.isNotEmpty
|
||||
? _loggingService.good("Created ${_imageModels.length} images' models")
|
||||
: _loggingService.warning('No images found');
|
||||
|
||||
Locator.instance().signalReady(this);
|
||||
}
|
||||
|
||||
int get firstAvailableImageIndex => 0;
|
||||
int get lastAvailableImageIndex => _imageModels.length - 1;
|
||||
|
||||
static ImagesService get locate => Locator.locate();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue