Skip to content

Commit

Permalink
improve model and entity
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushBherwani1998 committed Aug 2, 2023
1 parent 84be6a6 commit 1ef2725
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<UnsplashImageModel>> fetchImages(int pageNumber);
Future<List<UnsplashImage>> fetchImages(int pageNumber);
}

class UnsplashRemoteDataSourceImpl implements UnsplashRemoteDataSource {
Expand All @@ -13,17 +14,15 @@ class UnsplashRemoteDataSourceImpl implements UnsplashRemoteDataSource {
UnsplashRemoteDataSourceImpl(this.dio);

@override
Future<List<UnsplashImageModel>> fetchImages(int pageNumber) async {
Future<List<UnsplashImage>> 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<UnsplashImageModel> 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, _) {
Expand Down
13 changes: 13 additions & 0 deletions lib/features/home/data/models/image_model.dart
Original file line number Diff line number Diff line change
@@ -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<Map<String, dynamic>> json) {
final List<UnsplashImageModel> 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});

Expand Down
9 changes: 9 additions & 0 deletions lib/features/home/domain/entities/image.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import 'package:equatable/equatable.dart';

class UnsplashImageList extends Equatable {
final List<UnsplashImage> images;

const UnsplashImageList(this.images);

@override
List<Object?> get props => [images];
}

class UnsplashImage extends Equatable {
final String id;
final String url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> 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<Map<String, dynamic>>.from(jsonData),
);

unsplashRemoteDataSourceImpl = UnsplashRemoteDataSourceImpl(dioMock);
});
Expand All @@ -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);
});
});

}
8 changes: 4 additions & 4 deletions test/features/home/data/models/image_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ void main() {
expect(unsplashImageModel, isA<UnsplashImage>());
});

test('UnsplashImageModel is created from json string', () {
final map = Map<String, dynamic>.from(
test('UnsplashImageModelList is created from json string', () {
final map = List<Map<String, dynamic>>.from(
jsonDecode(fixture('image_model_fixture.json')),
);

final result = UnsplashImageModel.fromJson(map);
final result = UnsplashImageListModel.fromJson(map);

expect(result, unsplashImageModel);
expect(result.images, [unsplashImageModel]);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
116 changes: 59 additions & 57 deletions test/fixtures/image_model_fixture.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]

0 comments on commit 1ef2725

Please sign in to comment.