feat/architecture/make_usecases_more_modular_and_adaptable #39
| @ -35,46 +35,47 @@ class PhotoRepositoryImpl extends PhotoRepository { | |||||||
|   ); |   ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<void> addPhotoToFavorites(Photo photo) => Result.tryCatchAsync( |   FutureOrResult<void> addPhotoToFavorites(Photo photo) => Result.tryCatchAsync( | ||||||
|         () => _favoriteLocalDataSource.addPhotoToFavorites(photo), |         () => _favoriteLocalDataSource.addPhotoToFavorites(photo), | ||||||
|         (error) => ClientException('Cannot add photo to favorites.'), |         (error) => ClientException('Cannot add photo to favorites.'), | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<bool> checkIfPhotoIsInFavorites(int id) => Result.tryCatchAsync( |   FutureOrResult<bool> checkIfPhotoIsInFavorites(int id) => | ||||||
|  |       Result.tryCatchAsync( | ||||||
|         () => _favoriteLocalDataSource.checkIfPhotoIsInFavorites(id), |         () => _favoriteLocalDataSource.checkIfPhotoIsInFavorites(id), | ||||||
|         (error) => |         (error) => | ||||||
|             ClientException('Cannot check if photo `$id` is in favorites.'), |             ClientException('Cannot check if photo `$id` is in favorites.'), | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<void> deletePhotoFromFavorites(int id) => Result.tryCatchAsync( |   FutureOrResult<void> deletePhotoFromFavorites(int id) => Result.tryCatchAsync( | ||||||
|         () => _favoriteLocalDataSource.deletePhotoFromFavorites(id), |         () => _favoriteLocalDataSource.deletePhotoFromFavorites(id), | ||||||
|         (error) => ClientException('Cannot delete photo `$id` from favorites.'), |         (error) => ClientException('Cannot delete photo `$id` from favorites.'), | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<Album> getAlbum(int id) => Result.tryCatchAsync( |   FutureOrResult<Album> getAlbum(int id) => Result.tryCatchAsync( | ||||||
|         () => _albumRemoteDataSource.getAlbum(id), |         () => _albumRemoteDataSource.getAlbum(id), | ||||||
|         (error) => ServerException('Cannot retrieve album $id.'), |         (error) => ServerException('Cannot retrieve album $id.'), | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<List<Album>> getAllAlbums({int? start, int? limit}) => |   FutureOrResult<List<Album>> getAllAlbums({int? start, int? limit}) => | ||||||
|       Result.tryCatchAsync( |       Result.tryCatchAsync( | ||||||
|         () => _albumRemoteDataSource.getAllAlbums(start: start, limit: limit), |         () => _albumRemoteDataSource.getAllAlbums(start: start, limit: limit), | ||||||
|         (error) => ServerException('Cannot retrieve all albums.'), |         (error) => ServerException('Cannot retrieve all albums.'), | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<List<Photo>> getAllPhotos({int? start, int? limit}) async => |   FutureOrResult<List<Photo>> getAllPhotos({int? start, int? limit}) async => | ||||||
|       Result.tryCatchAsync( |       Result.tryCatchAsync( | ||||||
|         () => _photoRemoteDataSource.getAllPhotos(start: start, limit: limit), |         () => _photoRemoteDataSource.getAllPhotos(start: start, limit: limit), | ||||||
|         (error) => ServerException('Cannot retrieve all photos.'), |         (error) => ServerException('Cannot retrieve all photos.'), | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<List<Photo>> getAllPhotosFromFavorites() async { |   FutureOrResult<List<Photo>> getAllPhotosFromFavorites() async { | ||||||
|     try { |     try { | ||||||
|       final response = <Photo>[]; |       final response = <Photo>[]; | ||||||
|       final favorites = |       final favorites = | ||||||
| @ -95,13 +96,13 @@ class PhotoRepositoryImpl extends PhotoRepository { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<Photo> getPhoto(int id) => Result.tryCatchAsync( |   FutureOrResult<Photo> getPhoto(int id) => Result.tryCatchAsync( | ||||||
|         () => _photoRemoteDataSource.getPhoto(id), |         () => _photoRemoteDataSource.getPhoto(id), | ||||||
|         (error) => ServerException('Cannot retrieve photo $id.'), |         (error) => ServerException('Cannot retrieve photo $id.'), | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<List<Photo>> getPhotosFromAlbum( |   FutureOrResult<List<Photo>> getPhotosFromAlbum( | ||||||
|     int albumId, { |     int albumId, { | ||||||
|     int? start, |     int? start, | ||||||
|     int? limit, |     int? limit, | ||||||
|  | |||||||
| @ -19,17 +19,17 @@ import 'package:architecture_example/domain/entities/photo.dart'; | |||||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||||
| 
 | 
 | ||||||
| abstract class PhotoRepository extends BaseRepository { | abstract class PhotoRepository extends BaseRepository { | ||||||
|   FutureResult<Album> getAlbum(int id); |   FutureOrResult<Album> getAlbum(int id); | ||||||
|   FutureResult<List<Album>> getAllAlbums({int? start, int? limit}); |   FutureOrResult<List<Album>> getAllAlbums({int? start, int? limit}); | ||||||
|   FutureResult<Photo> getPhoto(int id); |   FutureOrResult<Photo> getPhoto(int id); | ||||||
|   FutureResult<List<Photo>> getAllPhotos({int? start, int? limit}); |   FutureOrResult<List<Photo>> getAllPhotos({int? start, int? limit}); | ||||||
|   FutureResult<List<Photo>> getPhotosFromAlbum( |   FutureOrResult<List<Photo>> getPhotosFromAlbum( | ||||||
|     int albumId, { |     int albumId, { | ||||||
|     int? start, |     int? start, | ||||||
|     int? limit, |     int? limit, | ||||||
|   }); |   }); | ||||||
|   FutureResult<void> addPhotoToFavorites(Photo photo); |   FutureOrResult<void> addPhotoToFavorites(Photo photo); | ||||||
|   FutureResult<void> deletePhotoFromFavorites(int id); |   FutureOrResult<void> deletePhotoFromFavorites(int id); | ||||||
|   FutureResult<List<Photo>> getAllPhotosFromFavorites(); |   FutureOrResult<List<Photo>> getAllPhotosFromFavorites(); | ||||||
|   FutureResult<bool> checkIfPhotoIsInFavorites(int id); |   FutureOrResult<bool> checkIfPhotoIsInFavorites(int id); | ||||||
| } | } | ||||||
|  | |||||||
| @ -14,18 +14,27 @@ | |||||||
| // You should have received a copy of the GNU General Public License | // You should have received a copy of the GNU General Public License | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
| 
 | 
 | ||||||
|  | import 'dart:async'; | ||||||
|  | 
 | ||||||
| import 'package:architecture_example/domain/entities/photo.dart'; | import 'package:architecture_example/domain/entities/photo.dart'; | ||||||
| import 'package:architecture_example/domain/repositories/photo_repository.dart'; | import 'package:architecture_example/domain/repositories/photo_repository.dart'; | ||||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||||
| 
 | 
 | ||||||
| class AddPhotoToFavorites extends UseCase<Photo, List<Photo>> { | class AddPhotoToFavorites extends AsyncUseCase<Photo, List<Photo>> { | ||||||
|   final PhotoRepository _photoRepository; |   final PhotoRepository _photoRepository; | ||||||
| 
 | 
 | ||||||
|   AddPhotoToFavorites(this._photoRepository); |   AddPhotoToFavorites(this._photoRepository); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<List<Photo>> call(Photo params) async { |   FutureOrResult<List<Photo>> call(Photo? params) async { | ||||||
|     await _photoRepository.addPhotoToFavorites(params); |     await _photoRepository.addPhotoToFavorites(params!); | ||||||
|     return _photoRepository.getAllPhotosFromFavorites(); |     return _photoRepository.getAllPhotosFromFavorites(); | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   @override | ||||||
|  |   FutureOr<void> onStart(Photo? params) { | ||||||
|  |     if (params == null) { | ||||||
|  |       throw ClientException('Photo cannot be null'); | ||||||
|  |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -14,15 +14,24 @@ | |||||||
| // You should have received a copy of the GNU General Public License | // You should have received a copy of the GNU General Public License | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
| 
 | 
 | ||||||
|  | import 'dart:async'; | ||||||
|  | 
 | ||||||
| import 'package:architecture_example/domain/repositories/photo_repository.dart'; | import 'package:architecture_example/domain/repositories/photo_repository.dart'; | ||||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||||
| 
 | 
 | ||||||
| class CheckIfPhotoIsInFavorites extends UseCase<int, bool> { | class CheckIfPhotoIsInFavorites extends AsyncUseCase<int, bool> { | ||||||
|   final PhotoRepository _photoRepository; |   final PhotoRepository _photoRepository; | ||||||
| 
 | 
 | ||||||
|   CheckIfPhotoIsInFavorites(this._photoRepository); |   CheckIfPhotoIsInFavorites(this._photoRepository); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<bool> call(int params) async => |   FutureOrResult<bool> call(int? params) async => | ||||||
|       _photoRepository.checkIfPhotoIsInFavorites(params); |       _photoRepository.checkIfPhotoIsInFavorites(params!); | ||||||
|  | 
 | ||||||
|  |   @override | ||||||
|  |   FutureOr<void> onStart(int? params) { | ||||||
|  |     if (params == null) { | ||||||
|  |       throw ClientException('id cannot be null'); | ||||||
|  |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -18,13 +18,13 @@ import 'package:architecture_example/domain/entities/photo.dart'; | |||||||
| import 'package:architecture_example/domain/repositories/photo_repository.dart'; | import 'package:architecture_example/domain/repositories/photo_repository.dart'; | ||||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||||
| 
 | 
 | ||||||
| class DisplayFavorites extends UseCase<void, List<Photo>> { | class DisplayFavorites extends AsyncUseCase<NoParam, List<Photo>> { | ||||||
|   final PhotoRepository _photoRepository; |   final PhotoRepository _photoRepository; | ||||||
| 
 | 
 | ||||||
|   DisplayFavorites(this._photoRepository); |   DisplayFavorites(this._photoRepository); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<List<Photo>> call(void params) { |   FutureOrResult<List<Photo>> call(void params) { | ||||||
|     final photos = _photoRepository.getAllPhotosFromFavorites(); |     final photos = _photoRepository.getAllPhotosFromFavorites(); | ||||||
|     return photos; |     return photos; | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -14,18 +14,27 @@ | |||||||
| // You should have received a copy of the GNU General Public License | // You should have received a copy of the GNU General Public License | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
| 
 | 
 | ||||||
|  | import 'dart:async'; | ||||||
|  | 
 | ||||||
| import 'package:architecture_example/domain/entities/photo.dart'; | import 'package:architecture_example/domain/entities/photo.dart'; | ||||||
| import 'package:architecture_example/domain/repositories/photo_repository.dart'; | import 'package:architecture_example/domain/repositories/photo_repository.dart'; | ||||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||||
| 
 | 
 | ||||||
| class DisplayPhoto extends UseCase<int, Photo> { | class DisplayPhoto extends AsyncUseCase<int, Photo> { | ||||||
|   final PhotoRepository _photoRepository; |   final PhotoRepository _photoRepository; | ||||||
| 
 | 
 | ||||||
|   DisplayPhoto(this._photoRepository); |   DisplayPhoto(this._photoRepository); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<Photo> call(int params) { |   FutureOrResult<Photo> call(int? params) { | ||||||
|     final photo = _photoRepository.getPhoto(params); |     final photo = _photoRepository.getPhoto(params!); | ||||||
|     return photo; |     return photo; | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   @override | ||||||
|  |   FutureOr<void> onStart(int? params) { | ||||||
|  |     if (params == null) { | ||||||
|  |       throw ClientException('id cannot be null'); | ||||||
|  |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -14,24 +14,33 @@ | |||||||
| // You should have received a copy of the GNU General Public License | // You should have received a copy of the GNU General Public License | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
| 
 | 
 | ||||||
|  | import 'dart:async'; | ||||||
|  | 
 | ||||||
| import 'package:architecture_example/domain/entities/photo.dart'; | import 'package:architecture_example/domain/entities/photo.dart'; | ||||||
| import 'package:architecture_example/domain/repositories/photo_repository.dart'; | import 'package:architecture_example/domain/repositories/photo_repository.dart'; | ||||||
| import 'package:architecture_example/domain/usecases/photos/params/query_parameters.dart'; | import 'package:architecture_example/domain/usecases/photos/params/query_parameters.dart'; | ||||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||||
| 
 | 
 | ||||||
| class OpenAlbum extends UseCase<QueryParameters, List<Photo>> { | class OpenAlbum extends AsyncUseCase<QueryParameters, List<Photo>> { | ||||||
|   final PhotoRepository _photoRepository; |   final PhotoRepository _photoRepository; | ||||||
| 
 | 
 | ||||||
|   OpenAlbum(this._photoRepository); |   OpenAlbum(this._photoRepository); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<List<Photo>> call(QueryParameters params) { |   FutureOrResult<List<Photo>> call(QueryParameters? params) { | ||||||
|     final photos = _photoRepository.getPhotosFromAlbum( |     final photos = _photoRepository.getPhotosFromAlbum( | ||||||
|       params.albumId, |       params!.albumId, | ||||||
|       start: params.start, |       start: params.start, | ||||||
|       limit: params.limit, |       limit: params.limit, | ||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
|     return photos; |     return photos; | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   @override | ||||||
|  |   FutureOr<void> onStart(QueryParameters? params) { | ||||||
|  |     if (params == null) { | ||||||
|  |       throw ClientException('params cannot be null'); | ||||||
|  |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -14,18 +14,27 @@ | |||||||
| // You should have received a copy of the GNU General Public License | // You should have received a copy of the GNU General Public License | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
| 
 | 
 | ||||||
|  | import 'dart:async'; | ||||||
|  | 
 | ||||||
| import 'package:architecture_example/domain/entities/photo.dart'; | import 'package:architecture_example/domain/entities/photo.dart'; | ||||||
| import 'package:architecture_example/domain/repositories/photo_repository.dart'; | import 'package:architecture_example/domain/repositories/photo_repository.dart'; | ||||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||||
| 
 | 
 | ||||||
| class RemovePhotoFromFavorites extends UseCase<int, List<Photo>> { | class RemovePhotoFromFavorites extends AsyncUseCase<int, List<Photo>> { | ||||||
|   final PhotoRepository _photoRepository; |   final PhotoRepository _photoRepository; | ||||||
| 
 | 
 | ||||||
|   RemovePhotoFromFavorites(this._photoRepository); |   RemovePhotoFromFavorites(this._photoRepository); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<List<Photo>> call(int params) async { |   FutureOrResult<List<Photo>> call(int? params) async { | ||||||
|     await _photoRepository.deletePhotoFromFavorites(params); |     await _photoRepository.deletePhotoFromFavorites(params!); | ||||||
|     return _photoRepository.getAllPhotosFromFavorites(); |     return _photoRepository.getAllPhotosFromFavorites(); | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   @override | ||||||
|  |   FutureOr<void> onStart(int? params) { | ||||||
|  |     if (params == null) { | ||||||
|  |       throw ClientException('id cannot be null'); | ||||||
|  |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -14,22 +14,31 @@ | |||||||
| // You should have received a copy of the GNU General Public License | // You should have received a copy of the GNU General Public License | ||||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||
| 
 | 
 | ||||||
|  | import 'dart:async'; | ||||||
|  | 
 | ||||||
| import 'package:architecture_example/domain/entities/album.dart'; | import 'package:architecture_example/domain/entities/album.dart'; | ||||||
| import 'package:architecture_example/domain/repositories/photo_repository.dart'; | import 'package:architecture_example/domain/repositories/photo_repository.dart'; | ||||||
| import 'package:architecture_example/domain/usecases/photos/params/query_parameters.dart'; | import 'package:architecture_example/domain/usecases/photos/params/query_parameters.dart'; | ||||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||||
| 
 | 
 | ||||||
| class RetrieveAllAlbums extends UseCase<QueryParameters, List<Album>> { | class RetrieveAllAlbums extends AsyncUseCase<QueryParameters, List<Album>> { | ||||||
|   final PhotoRepository _photoRepository; |   final PhotoRepository _photoRepository; | ||||||
| 
 | 
 | ||||||
|   RetrieveAllAlbums(this._photoRepository); |   RetrieveAllAlbums(this._photoRepository); | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   FutureResult<List<Album>> call(QueryParameters params) { |   FutureOrResult<List<Album>> call(QueryParameters? params) { | ||||||
|     final albums = _photoRepository.getAllAlbums( |     final albums = _photoRepository.getAllAlbums( | ||||||
|       start: params.start, |       start: params!.start, | ||||||
|       limit: params.limit, |       limit: params.limit, | ||||||
|     ); |     ); | ||||||
|     return albums; |     return albums; | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   @override | ||||||
|  |   FutureOr<void> onStart(QueryParameters? params) { | ||||||
|  |     if (params == null) { | ||||||
|  |       throw ClientException('params cannot be null'); | ||||||
|  |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user