abdicate object assigning responsibility

The ImagesService itself handles the conversion of Json to the object itself, instead of the ImagesApi
This commit is contained in:
Mguy13 2022-12-24 00:32:53 +01:00
parent a0ed894016
commit 6a84a9bef0
9 changed files with 255 additions and 25 deletions

View file

@ -14,14 +14,15 @@ class UnsplashImagesApi implements ImagesApi {
final random = Random();
@override
FutureOr<Iterable<ImageModel>> fetchImageUri({required String token}) async {
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
return Iterable<int>.generate(ConstValues.numberOfImages).map((final imageIndex) {
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);
@ -36,6 +37,9 @@ class UnsplashImagesApi implements ImagesApi {
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();
@ -43,7 +47,7 @@ class UnsplashImagesApi implements ImagesApi {
}
@override
FutureOr<List<ImageModel>> searchImages({
FutureOr<Iterable<Map<String, dynamic>>> searchImages({
required String searchStr,
required String token,
}) async {
@ -55,7 +59,7 @@ class UnsplashImagesApi implements ImagesApi {
try {
// Create (randomly-bounded) dummy number of images
return Iterable<int>.generate(numberOfResults).map((final imageIndex) {
final dummyImageModels = Iterable<int>.generate(numberOfResults).map((final imageIndex) {
// Drawing from a normal distribution
final imageSide =
random.nextIntInRange(min: ConstValues.minImageSize, max: ConstValues.maxImageSize);
@ -68,7 +72,10 @@ class UnsplashImagesApi implements ImagesApi {
// Custom dummy name for the image
imageName: Strings.current.imageNameSearch(searchStr, imageIndex + 1),
);
}).toList(growable: false);
});
// Emulating serialization
return dummyImageModels.map((final dummyModel) => dummyModel.toJson());
} on Exception catch (ex, stackTrace) {
_loggingService.handleException(ex, stackTrace);
return List.empty();