diff --git a/lib/features/home/data/datasources/unsplash_remote_datasource.dart b/lib/features/home/data/datasources/unsplash_remote_datasource.dart index 2c726f8..ad5c75c 100644 --- a/lib/features/home/data/datasources/unsplash_remote_datasource.dart +++ b/lib/features/home/data/datasources/unsplash_remote_datasource.dart @@ -2,9 +2,10 @@ import 'dart:developer'; import 'package:dio/dio.dart'; import 'package:unplash_sample/features/home/data/models/image_model.dart'; +import 'package:unplash_sample/features/home/domain/entities/image.dart'; abstract class UnsplashRemoteDataSource { - Future> fetchImages(int pageNumber); + Future> fetchImages(int pageNumber); } class UnsplashRemoteDataSourceImpl implements UnsplashRemoteDataSource { @@ -13,17 +14,15 @@ class UnsplashRemoteDataSourceImpl implements UnsplashRemoteDataSource { UnsplashRemoteDataSourceImpl(this.dio); @override - Future> fetchImages(int pageNumber) async { + Future> fetchImages(int pageNumber) async { try { final response = await dio.get('https://api.unsplash.com/photos'); if (response.statusCode == 200) { - final jsonList = List.from(response.data); - final List imageModelList = []; - for (final jsonMap in jsonList) { - final imageModel = UnsplashImageModel.fromJson(jsonMap); - imageModelList.add(imageModel); - } - return imageModelList; + final imageModelListModel = UnsplashImageListModel.fromJson( + List.from(response.data), + ); + + return imageModelListModel.images; } throw Exception("Something went wrong"); } catch (e, _) { diff --git a/lib/features/home/data/models/image_model.dart b/lib/features/home/data/models/image_model.dart index 3198bee..625c242 100644 --- a/lib/features/home/data/models/image_model.dart +++ b/lib/features/home/data/models/image_model.dart @@ -1,5 +1,18 @@ import 'package:unplash_sample/features/home/domain/entities/image.dart'; +class UnsplashImageListModel extends UnsplashImageList { + const UnsplashImageListModel(super.images); + + factory UnsplashImageListModel.fromJson(List> json) { + final List imageModelList = []; + for (final jsonMap in json) { + final imageModel = UnsplashImageModel.fromJson(jsonMap); + imageModelList.add(imageModel); + } + return UnsplashImageListModel(imageModelList); + } +} + class UnsplashImageModel extends UnsplashImage { const UnsplashImageModel({required super.id, required super.url}); diff --git a/lib/features/home/domain/entities/image.dart b/lib/features/home/domain/entities/image.dart index c42028d..d484c30 100644 --- a/lib/features/home/domain/entities/image.dart +++ b/lib/features/home/domain/entities/image.dart @@ -1,5 +1,14 @@ import 'package:equatable/equatable.dart'; +class UnsplashImageList extends Equatable { + final List images; + + const UnsplashImageList(this.images); + + @override + List get props => [images]; +} + class UnsplashImage extends Equatable { final String id; final String url; diff --git a/test/features/home/data/datasources/unsplash_remote_datasource_impl_test.dart b/test/features/home/data/datasources/unsplash_remote_datasource_impl_test.dart index 99abbdb..84eba24 100644 --- a/test/features/home/data/datasources/unsplash_remote_datasource_impl_test.dart +++ b/test/features/home/data/datasources/unsplash_remote_datasource_impl_test.dart @@ -11,14 +11,16 @@ import '../../../../fixtures/fixture_reader.dart'; void main() { late final DioMock dioMock; - late final UnsplashImageModel unsplashImageModel; + late final UnsplashImageListModel unsplashImageModel; late final UnsplashRemoteDataSourceImpl unsplashRemoteDataSourceImpl; - late final Map jsonMap; + late final dynamic jsonData; setUpAll(() { dioMock = DioMock(); - jsonMap = jsonDecode(fixture('image_model_fixture.json')); - unsplashImageModel = UnsplashImageModel.fromJson(jsonMap); + jsonData = jsonDecode(fixture('image_model_fixture.json')); + unsplashImageModel = UnsplashImageListModel.fromJson( + List>.from(jsonData), + ); unsplashRemoteDataSourceImpl = UnsplashRemoteDataSourceImpl(dioMock); }); @@ -29,14 +31,13 @@ void main() { return Response( requestOptions: RequestOptions(), statusCode: 200, - data: [jsonMap], + data: jsonData, ); }); final response = await unsplashRemoteDataSourceImpl.fetchImages(1); - expect([unsplashImageModel], response); + expect(unsplashImageModel.images, response); }); }); - } diff --git a/test/features/home/data/models/image_model_test.dart b/test/features/home/data/models/image_model_test.dart index d79e6bd..ae3a72f 100644 --- a/test/features/home/data/models/image_model_test.dart +++ b/test/features/home/data/models/image_model_test.dart @@ -16,14 +16,14 @@ void main() { expect(unsplashImageModel, isA()); }); - test('UnsplashImageModel is created from json string', () { - final map = Map.from( + test('UnsplashImageModelList is created from json string', () { + final map = List>.from( jsonDecode(fixture('image_model_fixture.json')), ); - final result = UnsplashImageModel.fromJson(map); + final result = UnsplashImageListModel.fromJson(map); - expect(result, unsplashImageModel); + expect(result.images, [unsplashImageModel]); }); }); } diff --git a/test/features/home/data/repositories/unsplash_repository_impl_test.dart b/test/features/home/data/repositories/unsplash_repository_impl_test.dart index e5d9654..7fa83af 100644 --- a/test/features/home/data/repositories/unsplash_repository_impl_test.dart +++ b/test/features/home/data/repositories/unsplash_repository_impl_test.dart @@ -4,7 +4,6 @@ import 'package:unplash_sample/core/error/error.dart'; import 'package:unplash_sample/features/home/data/datasources/unsplash_remote_datasource.dart'; import 'package:unplash_sample/features/home/data/models/image_model.dart'; import 'package:unplash_sample/features/home/data/repositories/unsplash_repository_impl.dart'; -import 'package:unplash_sample/features/home/domain/entities/image.dart'; import '../../../../core/mocks/unsplash_remote_datasource_mock.dart'; diff --git a/test/fixtures/image_model_fixture.json b/test/fixtures/image_model_fixture.json index 3467bf9..6e515c6 100644 --- a/test/fixtures/image_model_fixture.json +++ b/test/fixtures/image_model_fixture.json @@ -1,61 +1,63 @@ -{ - "id": "LBI7cgq3pbM", - "created_at": "2016-05-03T11:00:28-04:00", - "updated_at": "2016-07-10T11:00:01-05:00", - "width": 5245, - "height": 3497, - "color": "#60544D", - "blur_hash": "LoC%a7IoIVxZ_NM|M{s:%hRjWAo0", - "likes": 12, - "liked_by_user": false, - "description": "A man drinking a coffee.", - "user": { - "id": "pXhwzz1JtQU", - "username": "poorkane", - "name": "Gilbert Kane", - "portfolio_url": "https://theylooklikeeggsorsomething.com/", - "bio": "XO", - "location": "Way out there", - "total_likes": 5, - "total_photos": 74, - "total_collections": 52, - "instagram_username": "instantgrammer", - "twitter_username": "crew", - "profile_image": { - "small": "https://images.unsplash.com/face-springmorning.jpg?q=80&fm=jpg&crop=faces&fit=crop&h=32&w=32", - "medium": "https://images.unsplash.com/face-springmorning.jpg?q=80&fm=jpg&crop=faces&fit=crop&h=64&w=64", - "large": "https://images.unsplash.com/face-springmorning.jpg?q=80&fm=jpg&crop=faces&fit=crop&h=128&w=128" +[ + { + "id": "LBI7cgq3pbM", + "created_at": "2016-05-03T11:00:28-04:00", + "updated_at": "2016-07-10T11:00:01-05:00", + "width": 5245, + "height": 3497, + "color": "#60544D", + "blur_hash": "LoC%a7IoIVxZ_NM|M{s:%hRjWAo0", + "likes": 12, + "liked_by_user": false, + "description": "A man drinking a coffee.", + "user": { + "id": "pXhwzz1JtQU", + "username": "poorkane", + "name": "Gilbert Kane", + "portfolio_url": "https://theylooklikeeggsorsomething.com/", + "bio": "XO", + "location": "Way out there", + "total_likes": 5, + "total_photos": 74, + "total_collections": 52, + "instagram_username": "instantgrammer", + "twitter_username": "crew", + "profile_image": { + "small": "https://images.unsplash.com/face-springmorning.jpg?q=80&fm=jpg&crop=faces&fit=crop&h=32&w=32", + "medium": "https://images.unsplash.com/face-springmorning.jpg?q=80&fm=jpg&crop=faces&fit=crop&h=64&w=64", + "large": "https://images.unsplash.com/face-springmorning.jpg?q=80&fm=jpg&crop=faces&fit=crop&h=128&w=128" + }, + "links": { + "self": "https://api.unsplash.com/users/poorkane", + "html": "https://unsplash.com/poorkane", + "photos": "https://api.unsplash.com/users/poorkane/photos", + "likes": "https://api.unsplash.com/users/poorkane/likes", + "portfolio": "https://api.unsplash.com/users/poorkane/portfolio" + } + }, + "current_user_collections": [ + { + "id": 206, + "title": "Makers: Cat and Ben", + "published_at": "2016-01-12T18:16:09-05:00", + "last_collected_at": "2016-06-02T13:10:03-04:00", + "updated_at": "2016-07-10T11:00:01-05:00", + "cover_photo": null, + "user": null + } + ], + "urls": { + "raw": "https://images.unsplash.com/face-springmorning.jpg", + "full": "https://images.unsplash.com/face-springmorning.jpg?q=75&fm=jpg", + "regular": "https://images.unsplash.com/face-springmorning.jpg?q=75&fm=jpg&w=1080&fit=max", + "small": "https://images.unsplash.com/face-springmorning.jpg?q=75&fm=jpg&w=400&fit=max", + "thumb": "https://images.unsplash.com/face-springmorning.jpg?q=75&fm=jpg&w=200&fit=max" }, "links": { - "self": "https://api.unsplash.com/users/poorkane", - "html": "https://unsplash.com/poorkane", - "photos": "https://api.unsplash.com/users/poorkane/photos", - "likes": "https://api.unsplash.com/users/poorkane/likes", - "portfolio": "https://api.unsplash.com/users/poorkane/portfolio" - } - }, - "current_user_collections": [ - { - "id": 206, - "title": "Makers: Cat and Ben", - "published_at": "2016-01-12T18:16:09-05:00", - "last_collected_at": "2016-06-02T13:10:03-04:00", - "updated_at": "2016-07-10T11:00:01-05:00", - "cover_photo": null, - "user": null + "self": "https://api.unsplash.com/photos/LBI7cgq3pbM", + "html": "https://unsplash.com/photos/LBI7cgq3pbM", + "download": "https://unsplash.com/photos/LBI7cgq3pbM/download", + "download_location": "https://api.unsplash.com/photos/LBI7cgq3pbM/download" } - ], - "urls": { - "raw": "https://images.unsplash.com/face-springmorning.jpg", - "full": "https://images.unsplash.com/face-springmorning.jpg?q=75&fm=jpg", - "regular": "https://images.unsplash.com/face-springmorning.jpg?q=75&fm=jpg&w=1080&fit=max", - "small": "https://images.unsplash.com/face-springmorning.jpg?q=75&fm=jpg&w=400&fit=max", - "thumb": "https://images.unsplash.com/face-springmorning.jpg?q=75&fm=jpg&w=200&fit=max" - }, - "links": { - "self": "https://api.unsplash.com/photos/LBI7cgq3pbM", - "html": "https://unsplash.com/photos/LBI7cgq3pbM", - "download": "https://unsplash.com/photos/LBI7cgq3pbM/download", - "download_location": "https://api.unsplash.com/photos/LBI7cgq3pbM/download" } -} \ No newline at end of file +] \ No newline at end of file