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

102 lines
3.6 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';
import '/features/core/services/logging_service.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-23 10:20:46 +00:00
class UnsplashImagesApi implements ImagesApi {
final LoggingService _loggingService = LoggingService.locate;
final random = Random();
2022-12-20 19:29:46 +00:00
@override
2022-12-25 00:55:53 +00:00
FutureOr<Iterable<ImageModelDTO>> fetchImageUri({required String token}) async {
2022-12-23 10:20:46 +00:00
// Dummy fetching delay emulation
await Future.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-23 10:20:46 +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
return fetchedImageModelDtos
.map((final emulatedModelSerialized) => ImageModelDTO.fromJson(emulatedModelSerialized));
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,
required String token,
}) async {
final numberOfResults = random.nextIntInRange(min: 0, max: ConstValues.numberOfImages);
// Dummy fetching delay emulation
await Future.delayed(
Duration(milliseconds: ConstValues.defaultEmulatedLatencyMillis * numberOfResults));
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) {
_loggingService.handleException(ex, stackTrace);
return List.empty();
}
2022-12-25 00:55:53 +00:00
return searchImageModelDtos
.map((final emulatedModelSerialized) => ImageModelDTO.fromJson(emulatedModelSerialized));
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,
host: ConstValues.backendHost,
2022-12-20 20:52:24 +00:00
pathSegments: [...ConstValues.backendUrlPathSegments, '${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
}