mc_gallery/lib/features/home/api/unsplash_images_api.dart

106 lines
3.5 KiB
Dart
Raw Normal View History

2022-12-20 19:29:46 +00:00
import 'dart:async';
import 'dart:math';
import '/features/core/data/constants/const_values.dart';
2022-12-23 10:20:46 +00:00
import '/features/core/data/extensions/random_extensions.dart';
2022-12-20 19:29:46 +00:00
import '/l10n/generated/l10n.dart';
2022-12-23 10:20:46 +00:00
import '/locator.dart';
2022-12-20 19:29:46 +00:00
import '../abstracts/images_api.dart';
2022-12-25 00:55:53 +00:00
import '../data/dtos/image_model_dto.dart';
2022-12-20 19:29:46 +00:00
2022-12-25 21:17:59 +00:00
class UnsplashImagesApi extends ImagesApi {
2023-01-01 12:04:22 +00:00
UnsplashImagesApi({required super.token});
2022-12-26 18:39:47 +00:00
//final LoggingService _loggingService = LoggingService.locate;
2022-12-23 10:20:46 +00:00
final random = Random();
2022-12-20 19:29:46 +00:00
@override
2022-12-25 21:17:59 +00:00
FutureOr<Iterable<ImageModelDTO>> fetchImageUri() async {
2022-12-23 10:20:46 +00:00
// Dummy fetching delay emulation
2023-01-01 12:04:22 +00:00
await Future<void>.delayed(
const Duration(
milliseconds: ConstValues.defaultEmulatedLatencyMillis * ConstValues.numberOfImages,
),
);
2022-12-20 19:29:46 +00:00
2022-12-25 00:55:53 +00:00
final Iterable<Map<String, dynamic>> fetchedImageModelDtos;
2022-12-21 22:40:39 +00:00
try {
2022-12-23 10:20:46 +00:00
// Create fixed number of images
final dummyImageModels =
Iterable<int>.generate(ConstValues.numberOfImages).map((final imageIndex) {
2022-12-21 22:40:39 +00:00
// Drawing from a normal distribution
2022-12-23 10:20:46 +00:00
final imageSide =
random.nextIntInRange(min: ConstValues.minImageSize, max: ConstValues.maxImageSize);
2022-12-20 19:29:46 +00:00
2022-12-21 22:40:39 +00:00
final imageUri = _imageUrlGenerator(imageSide: imageSide);
2022-12-20 19:29:46 +00:00
2022-12-25 00:55:53 +00:00
return ImageModelDTO(
2022-12-21 22:40:39 +00:00
imageIndex: imageIndex,
uri: imageUri,
2022-12-23 10:20:46 +00:00
// Custom dummy name for the image
imageName: Strings.current.imageNameFetch(imageIndex + 1, imageSide),
2022-12-21 22:40:39 +00:00
);
});
// Emulating serialization
2022-12-25 00:55:53 +00:00
fetchedImageModelDtos = dummyImageModels.map((final dummyModel) => dummyModel.toJson());
2022-12-21 22:40:39 +00:00
} on Exception catch (ex, stackTrace) {
2022-12-26 18:39:47 +00:00
loggingService.handleException(ex, stackTrace);
2022-12-21 22:40:39 +00:00
return const Iterable.empty();
}
2022-12-25 00:55:53 +00:00
// Emulating deserialization
2023-01-01 12:04:22 +00:00
return fetchedImageModelDtos.map(ImageModelDTO.fromJson);
2022-12-20 19:29:46 +00:00
}
2022-12-23 10:20:46 +00:00
@override
2022-12-25 00:55:53 +00:00
FutureOr<Iterable<ImageModelDTO>> searchImages({
2022-12-23 10:20:46 +00:00
required String searchStr,
}) async {
final numberOfResults = random.nextIntInRange(min: 0, max: ConstValues.numberOfImages);
// Dummy fetching delay emulation
2023-01-01 12:04:22 +00:00
await Future<void>.delayed(
Duration(
milliseconds: ConstValues.defaultEmulatedLatencyMillis * numberOfResults,
),
);
2022-12-23 10:20:46 +00:00
2022-12-25 00:55:53 +00:00
final Iterable<Map<String, dynamic>> searchImageModelDtos;
2022-12-23 10:20:46 +00:00
try {
// Create (randomly-bounded) dummy number of images
final dummyImageModels = Iterable<int>.generate(numberOfResults).map((final imageIndex) {
2022-12-23 10:20:46 +00:00
// Drawing from a normal distribution
final imageSide =
random.nextIntInRange(min: ConstValues.minImageSize, max: ConstValues.maxImageSize);
final imageUri = _imageUrlGenerator(imageSide: imageSide);
2022-12-25 00:55:53 +00:00
return ImageModelDTO(
2022-12-23 10:20:46 +00:00
imageIndex: imageIndex,
uri: imageUri,
// Custom dummy name for the image
imageName: Strings.current.imageNameSearch(searchStr, imageIndex + 1),
);
});
// Emulating serialization
2022-12-25 00:55:53 +00:00
searchImageModelDtos = dummyImageModels.map((final dummyModel) => dummyModel.toJson());
2022-12-23 10:20:46 +00:00
} on Exception catch (ex, stackTrace) {
2022-12-26 18:39:47 +00:00
loggingService.handleException(ex, stackTrace);
2022-12-23 10:20:46 +00:00
return List.empty();
}
2022-12-25 00:55:53 +00:00
2023-01-01 12:04:22 +00:00
return searchImageModelDtos.map(ImageModelDTO.fromJson);
2022-12-23 10:20:46 +00:00
}
2022-12-20 19:29:46 +00:00
Uri _imageUrlGenerator({required int imageSide}) => Uri(
scheme: ConstValues.httpsScheme,
2022-12-26 18:39:47 +00:00
host: ConstValues.imagesHostServer,
pathSegments: [...ConstValues.imagesHostUrlPathSegments, '${imageSide}x$imageSide'],
2022-12-20 19:29:46 +00:00
);
2022-12-21 22:40:39 +00:00
static UnsplashImagesApi get locate => Locator.locate();
2022-12-20 19:29:46 +00:00
}