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:
parent
a0ed894016
commit
6a84a9bef0
9 changed files with 255 additions and 25 deletions
|
@ -1,15 +1,13 @@
|
|||
import 'dart:async';
|
||||
|
||||
import '../data/models/image_model.dart';
|
||||
|
||||
/// Interface for implementing image-fetching strategies, specific to a resource location on the internet.
|
||||
///
|
||||
/// Since I used a site that was more obscure than the ones in the examples, this (otherwise pointless
|
||||
/// and convoluting) interface is for adding a bit of flexibility to change strategy to some other site.
|
||||
abstract class ImagesApi {
|
||||
FutureOr<Iterable<ImageModel>> fetchImageUri({required String token});
|
||||
FutureOr<Iterable<Map<String, dynamic>>> fetchImageUri({required String token});
|
||||
|
||||
FutureOr<List<ImageModel>> searchImages({
|
||||
FutureOr<Iterable<Map<String, dynamic>>> searchImages({
|
||||
required String searchStr,
|
||||
required String token,
|
||||
});
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'image_model.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class ImageModel {
|
||||
const ImageModel({
|
||||
required this.uri,
|
||||
|
@ -15,4 +20,9 @@ class ImageModel {
|
|||
|
||||
/// Given name of the image.
|
||||
final String imageName;
|
||||
|
||||
factory ImageModel.fromJson(Map<String, dynamic> json) => _$ImageModelFromJson(json);
|
||||
|
||||
/// Connect the generated [_$PersonToJson] function to the `toJson` method.
|
||||
Map<String, dynamic> toJson() => _$ImageModelToJson(this);
|
||||
}
|
||||
|
|
20
lib/features/home/data/models/image_model.g.dart
Normal file
20
lib/features/home/data/models/image_model.g.dart
Normal file
|
@ -0,0 +1,20 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'image_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
ImageModel _$ImageModelFromJson(Map<String, dynamic> json) => ImageModel(
|
||||
uri: Uri.parse(json['uri'] as String),
|
||||
imageIndex: json['imageIndex'] as int,
|
||||
imageName: json['imageName'] as String,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$ImageModelToJson(ImageModel instance) =>
|
||||
<String, dynamic>{
|
||||
'uri': instance.uri.toString(),
|
||||
'imageIndex': instance.imageIndex,
|
||||
'imageName': instance.imageName,
|
||||
};
|
|
@ -42,7 +42,8 @@ class ImagesService {
|
|||
Future<void> _init() async {
|
||||
_loggingService.info('Fetching and creating image models...');
|
||||
_imageModels = {
|
||||
for (final imageModel in await _imagesApi.fetchImageUri(token: ''))
|
||||
for (final imageModel in (await _imagesApi.fetchImageUri(token: ''))
|
||||
.map((final emulatedModelSerialized) => ImageModel.fromJson(emulatedModelSerialized)))
|
||||
imageModel.imageName: imageModel
|
||||
};
|
||||
|
||||
|
@ -90,10 +91,13 @@ class ImagesService {
|
|||
..reversed;
|
||||
return _imageModels.valuesByKeys(keys: rankedKeys).toList(growable: false);
|
||||
case SearchOption.web:
|
||||
return await _imagesApi.searchImages(
|
||||
return (await _imagesApi.searchImages(
|
||||
searchStr: imageNamePart,
|
||||
token: '',
|
||||
);
|
||||
))
|
||||
.map(
|
||||
(final emulatedModelSerialized) => ImageModel.fromJson(emulatedModelSerialized))
|
||||
.toList(growable: false);
|
||||
}
|
||||
} finally {
|
||||
unlock();
|
||||
|
|
|
@ -46,9 +46,9 @@ class GalleryView extends StatelessWidget {
|
|||
],
|
||||
builder: (context, final values, child) => !model.isDisplayingPressingPrompt.value
|
||||
? IconButton(
|
||||
icon: !model.isSearchingListenable.value
|
||||
? const Icon(Icons.search)
|
||||
: const Icon(Icons.close),
|
||||
isSelected: model.isSearchingListenable.value,
|
||||
icon: const Icon(Icons.search),
|
||||
selectedIcon: const Icon(Icons.close),
|
||||
onPressed: model.searchPressed,
|
||||
)
|
||||
: const SizedBox.shrink(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue