93 lines
3.2 KiB
Dart
93 lines
3.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:math';
|
|
|
|
import '/features/core/data/constants/const_values.dart';
|
|
import '/features/core/data/extensions/random_extensions.dart';
|
|
import '/features/core/services/logging_service.dart';
|
|
import '/l10n/generated/l10n.dart';
|
|
import '/locator.dart';
|
|
import '../abstracts/images_api.dart';
|
|
import '../data/models/image_model.dart';
|
|
|
|
class UnsplashImagesApi implements ImagesApi {
|
|
final LoggingService _loggingService = LoggingService.locate;
|
|
final random = Random();
|
|
|
|
@override
|
|
FutureOr<Iterable<Map<String, dynamic>>> fetchImageUri({required String token}) async {
|
|
// Dummy fetching delay emulation
|
|
await Future.delayed(const Duration(
|
|
milliseconds: ConstValues.defaultEmulatedLatencyMillis * ConstValues.numberOfImages));
|
|
|
|
try {
|
|
// Create fixed number of images
|
|
final dummyImageModels =
|
|
Iterable<int>.generate(ConstValues.numberOfImages).map((final imageIndex) {
|
|
// Drawing from a normal distribution
|
|
final imageSide =
|
|
random.nextIntInRange(min: ConstValues.minImageSize, max: ConstValues.maxImageSize);
|
|
|
|
final imageUri = _imageUrlGenerator(imageSide: imageSide);
|
|
|
|
return ImageModel(
|
|
imageIndex: imageIndex,
|
|
uri: imageUri,
|
|
// Custom dummy name for the image
|
|
|
|
imageName: Strings.current.imageNameFetch(imageIndex + 1, imageSide),
|
|
);
|
|
});
|
|
|
|
// Emulating serialization
|
|
return dummyImageModels.map((final dummyModel) => dummyModel.toJson());
|
|
} on Exception catch (ex, stackTrace) {
|
|
_loggingService.handleException(ex, stackTrace);
|
|
return const Iterable.empty();
|
|
}
|
|
}
|
|
|
|
@override
|
|
FutureOr<Iterable<Map<String, dynamic>>> searchImages({
|
|
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));
|
|
|
|
try {
|
|
// Create (randomly-bounded) dummy number of images
|
|
final dummyImageModels = Iterable<int>.generate(numberOfResults).map((final imageIndex) {
|
|
// Drawing from a normal distribution
|
|
final imageSide =
|
|
random.nextIntInRange(min: ConstValues.minImageSize, max: ConstValues.maxImageSize);
|
|
|
|
final imageUri = _imageUrlGenerator(imageSide: imageSide);
|
|
|
|
return ImageModel(
|
|
imageIndex: imageIndex,
|
|
uri: imageUri,
|
|
// Custom dummy name for the image
|
|
imageName: Strings.current.imageNameSearch(searchStr, imageIndex + 1),
|
|
);
|
|
});
|
|
|
|
// Emulating serialization
|
|
return dummyImageModels.map((final dummyModel) => dummyModel.toJson());
|
|
} on Exception catch (ex, stackTrace) {
|
|
_loggingService.handleException(ex, stackTrace);
|
|
return List.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();
|
|
}
|