mc_gallery/lib/features/home/api/unsplash_images_api.dart
2022-12-22 20:35:45 +01:00

44 lines
1.5 KiB
Dart

import 'dart:async';
import 'dart:math';
import 'package:mc_gallery/features/core/services/logging_service.dart';
import 'package:mc_gallery/locator.dart';
import '/features/core/data/constants/const_values.dart';
import '/l10n/generated/l10n.dart';
import '../abstracts/images_api.dart';
import '../data/models/image_model.dart';
class UnsplashImagesApi with LoggingService implements ImagesApi {
@override
FutureOr<Iterable<ImageModel>> fetchImageUri({required String token}) {
final random = Random();
try {
return Iterable<int>.generate(ConstValues.numberOfImages).map((final imageIndex) {
// Drawing from a normal distribution
final imageSide = ConstValues.minImageSize +
random.nextInt((ConstValues.maxImageSize + 1) - ConstValues.minImageSize);
final imageUri = _imageUrlGenerator(imageSide: imageSide);
return ImageModel(
imageIndex: imageIndex,
uri: imageUri,
imageName: '${Strings.current.image} ${imageIndex + 1}: size=$imageSide',
);
});
} on Exception catch (ex, stackTrace) {
handleException(ex, stackTrace);
return const Iterable.empty();
}
}
Uri _imageUrlGenerator({required int imageSide}) => Uri(
scheme: ConstValues.httpsScheme,
host: ConstValues.backendHost,
pathSegments: [...ConstValues.backendUrlPathSegments, '${imageSide}x$imageSide'],
);
static UnsplashImagesApi get locate => Locator.locate();
}