-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: introduce datasource abstraction layer
- Loading branch information
Showing
4 changed files
with
102 additions
and
63 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
lib/features/picture_of_the_day/data/datasources/nasa_datasource.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import 'package:nasa_potday/features/picture_of_the_day/domain/entities/pictures_page_entity.dart'; | ||
|
||
abstract interface class NasaDataSource { | ||
Future<PicturesPageEntity> loadPictureOfTheDay({ | ||
required DateTime startDate, | ||
}); | ||
|
||
Future<PicturesPageEntity> loadNextPage({ | ||
required DateTime currentStartDate, | ||
}); | ||
} |
79 changes: 79 additions & 0 deletions
79
lib/features/picture_of_the_day/data/datasources/remote_nasa_datasource.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:http/http.dart'; | ||
import 'package:nasa_potday/features/picture_of_the_day/data/datasources/nasa_datasource.dart'; | ||
import 'package:nasa_potday/features/picture_of_the_day/data/models/load_picture_of_the_day_request_model.dart'; | ||
import 'package:nasa_potday/features/picture_of_the_day/data/models/picture_model.dart'; | ||
import 'package:nasa_potday/features/picture_of_the_day/domain/entities/pictures_page_entity.dart'; | ||
|
||
final class RemoteNasaDataSource implements NasaDataSource { | ||
const RemoteNasaDataSource({ | ||
required this.baseUrl, | ||
required this.apiKey, | ||
}); | ||
|
||
final String baseUrl; | ||
final String apiKey; | ||
|
||
@override | ||
Future<PicturesPageEntity> loadPictureOfTheDay({ | ||
required DateTime startDate, | ||
}) async { | ||
try { | ||
final pictures = await _request( | ||
LoadPictureOfTheDayRequestModel( | ||
apiKey: apiKey, | ||
startDate: startDate, | ||
), | ||
); | ||
return PicturesPageEntity( | ||
pictures: pictures, | ||
startDate: startDate, | ||
); | ||
} catch (error) { | ||
debugPrint('Could not load picture of the day: $error'); | ||
} | ||
return PicturesPageEntity( | ||
pictures: [], | ||
startDate: startDate, | ||
); | ||
} | ||
|
||
@override | ||
Future<PicturesPageEntity> loadNextPage({ | ||
required DateTime currentStartDate, | ||
}) async { | ||
final newStartDate = currentStartDate.subtract(const Duration(days: 7)); | ||
try { | ||
final pictures = await _request( | ||
LoadPictureOfTheDayRequestModel( | ||
apiKey: apiKey, | ||
startDate: newStartDate, | ||
endDate: currentStartDate.subtract(const Duration(days: 1)), | ||
), | ||
); | ||
return PicturesPageEntity(pictures: pictures, startDate: newStartDate); | ||
} catch (error) { | ||
debugPrint('Could not load picture of the day: $error'); | ||
} | ||
return PicturesPageEntity( | ||
pictures: [], | ||
startDate: newStartDate, | ||
); | ||
} | ||
|
||
Future<List<PictureModel>> _request( | ||
LoadPictureOfTheDayRequestModel request) async { | ||
final uri = Uri.parse(baseUrl).replace( | ||
queryParameters: request.toJson(), | ||
); | ||
final response = await get(uri); | ||
return (jsonDecode(response.body) as List) | ||
.cast<Map<String, dynamic>>() | ||
.map(PictureModel.fromJson) | ||
.toList() | ||
.reversed | ||
.toList(); | ||
} | ||
} |
68 changes: 7 additions & 61 deletions
68
lib/features/picture_of_the_day/data/repositories/remote_nasa_repository.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,25 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:http/http.dart'; | ||
import 'package:nasa_potday/features/picture_of_the_day/data/models/load_picture_of_the_day_request_model.dart'; | ||
import 'package:nasa_potday/features/picture_of_the_day/data/models/picture_model.dart'; | ||
import 'package:nasa_potday/features/picture_of_the_day/data/datasources/remote_nasa_datasource.dart'; | ||
import 'package:nasa_potday/features/picture_of_the_day/domain/entities/pictures_page_entity.dart'; | ||
import 'package:nasa_potday/features/picture_of_the_day/domain/repositories/nasa_repository.dart'; | ||
|
||
class RemoteNasaRepository implements NasaRepository { | ||
RemoteNasaRepository({ | ||
required this.baseUrl, | ||
required this.apiKey, | ||
required this.remoteDatasource, | ||
}); | ||
|
||
final String baseUrl; | ||
final String apiKey; | ||
final RemoteNasaDataSource remoteDatasource; | ||
|
||
@override | ||
Future<PicturesPageEntity> loadPictureOfTheDay({ | ||
required DateTime startDate, | ||
}) async { | ||
try { | ||
final pictures = await _request( | ||
LoadPictureOfTheDayRequestModel( | ||
apiKey: apiKey, | ||
startDate: startDate, | ||
), | ||
); | ||
return PicturesPageEntity( | ||
pictures: pictures, | ||
startDate: startDate, | ||
); | ||
} catch (error) { | ||
debugPrint('Could not load picture of the day: $error'); | ||
} | ||
return PicturesPageEntity( | ||
pictures: [], | ||
startDate: startDate, | ||
); | ||
}) { | ||
return remoteDatasource.loadPictureOfTheDay(startDate: startDate); | ||
} | ||
|
||
@override | ||
Future<PicturesPageEntity> loadNextPage({ | ||
required DateTime currentStartDate, | ||
}) async { | ||
final newStartDate = currentStartDate.subtract(const Duration(days: 7)); | ||
try { | ||
final pictures = await _request( | ||
LoadPictureOfTheDayRequestModel( | ||
apiKey: apiKey, | ||
startDate: newStartDate, | ||
endDate: currentStartDate.subtract(const Duration(days: 1)), | ||
), | ||
); | ||
return PicturesPageEntity(pictures: pictures, startDate: newStartDate); | ||
} catch (error) { | ||
debugPrint('Could not load picture of the day: $error'); | ||
} | ||
return PicturesPageEntity( | ||
pictures: [], | ||
startDate: newStartDate, | ||
); | ||
} | ||
|
||
Future<List<PictureModel>> _request( | ||
LoadPictureOfTheDayRequestModel request) async { | ||
final uri = Uri.parse(baseUrl).replace( | ||
queryParameters: request.toJson(), | ||
); | ||
final response = await get(uri); | ||
return (jsonDecode(response.body) as List) | ||
.cast<Map<String, dynamic>>() | ||
.map(PictureModel.fromJson) | ||
.toList() | ||
.reversed | ||
.toList(); | ||
}) { | ||
return remoteDatasource.loadNextPage(currentStartDate: currentStartDate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters