mc_gallery/lib/features/home/services/images_service.dart

42 lines
1.4 KiB
Dart
Raw Normal View History

2022-12-21 22:40:39 +00:00
import 'package:mc_gallery/features/core/services/logging_service.dart';
2022-12-20 19:29:46 +00:00
import 'package:mc_gallery/features/home/data/models/image_model.dart';
import 'package:mc_gallery/locator.dart';
import '../abstracts/images_api.dart';
/// Handles fetching and storing of Images.
///
/// 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 {
2022-12-21 22:40:39 +00:00
ImagesService({
required ImagesApi imagesApi,
required LoggingService loggingService,
}) : _imagesApi = imagesApi,
_loggingService = loggingService {
2022-12-20 19:29:46 +00:00
_init();
}
final ImagesApi _imagesApi;
2022-12-21 22:40:39 +00:00
final LoggingService _loggingService;
2022-12-20 19:29:46 +00:00
late final Iterable<ImageModel> _imageModels;
2022-12-20 20:52:24 +00:00
Iterable<ImageModel> get imageModels => _imageModels;
2022-12-20 19:29:46 +00:00
Future<void> _init() async {
2022-12-21 22:40:39 +00:00
_loggingService.info('Fetching and creating image models...');
2022-12-20 19:29:46 +00:00
_imageModels = await _imagesApi.fetchImageUri(token: '');
2022-12-21 22:40:39 +00:00
_imageModels.isNotEmpty
? _loggingService.good("Created ${_imageModels.length} images' models")
: _loggingService.warning('No images found');
2022-12-20 19:29:46 +00:00
Locator.instance().signalReady(this);
}
2022-12-21 22:40:39 +00:00
int get firstAvailableImageIndex => 0;
int get lastAvailableImageIndex => _imageModels.length - 1;
2022-12-20 19:29:46 +00:00
static ImagesService get locate => Locator.locate();
}