-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dafe568
commit 22202e8
Showing
14 changed files
with
284 additions
and
24 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ enum Routes { | |
drinkDetails, | ||
onBoarding, | ||
drinksByCategory, | ||
favorites, | ||
} |
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
11 changes: 11 additions & 0 deletions
11
lib/features/favourites/domain/repositories/favourites_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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import '../../../drinks/infrastructure/dto/drink_details/drink_details_model.dart'; | ||
|
||
abstract class FavoriteDrinksRepository { | ||
void addFavorite(DrinkDetails drink); | ||
|
||
void removeFavorite(String idDrink); | ||
|
||
List<DrinkDetails> getFavorites(); | ||
|
||
bool isFavorite(String idDrink); | ||
} |
12 changes: 12 additions & 0 deletions
12
lib/features/favourites/domain/use_cases/add_to_favourites.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,12 @@ | ||
import '../../../drinks/infrastructure/dto/drink_details/drink_details_model.dart'; | ||
import '../repositories/favourites_repository.dart'; | ||
|
||
class AddFavoriteUseCase { | ||
final FavoriteDrinksRepository _repository; | ||
|
||
AddFavoriteUseCase(this._repository); | ||
|
||
void call(DrinkDetails drink) { | ||
_repository.addFavorite(drink); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
lib/features/favourites/domain/use_cases/get_favourites.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,12 @@ | ||
import '../../../drinks/infrastructure/dto/drink_details/drink_details_model.dart'; | ||
import '../repositories/favourites_repository.dart'; | ||
|
||
class GetFavoritesUseCase { | ||
final FavoriteDrinksRepository _repository; | ||
|
||
GetFavoritesUseCase(this._repository); | ||
|
||
List<DrinkDetails> call() { | ||
return _repository.getFavorites(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
lib/features/favourites/domain/use_cases/is_favourite.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 '../repositories/favourites_repository.dart'; | ||
|
||
class IsFavoriteUseCase { | ||
final FavoriteDrinksRepository _repository; | ||
|
||
IsFavoriteUseCase(this._repository); | ||
|
||
bool call(String idDrink) { | ||
return _repository.isFavorite(idDrink); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
lib/features/favourites/domain/use_cases/remove_from_favourites.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 '../repositories/favourites_repository.dart'; | ||
|
||
class RemoveFavoriteUseCase { | ||
final FavoriteDrinksRepository _repository; | ||
|
||
RemoveFavoriteUseCase(this._repository); | ||
|
||
void call(String idDrink) { | ||
_repository.removeFavorite(idDrink); | ||
} | ||
} |
15 changes: 14 additions & 1 deletion
15
lib/features/favourites/infrastructure/data_sources/local_data_source.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
29 changes: 29 additions & 0 deletions
29
lib/features/favourites/infrastructure/repositories/favourites_repository_impl.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,29 @@ | ||
import '../../../drinks/infrastructure/dto/drink_details/drink_details_model.dart'; | ||
import '../../domain/repositories/favourites_repository.dart'; | ||
import '../data_sources/local_data_source.dart'; | ||
|
||
class FavoriteDrinksRepositoryImpl implements FavoriteDrinksRepository { | ||
final FavoriteDrinksDataSource _localDataSource; | ||
|
||
FavoriteDrinksRepositoryImpl(this._localDataSource); | ||
|
||
@override | ||
void addFavorite(DrinkDetails drink) { | ||
_localDataSource.addFavorite(drink); | ||
} | ||
|
||
@override | ||
void removeFavorite(String idDrink) { | ||
_localDataSource.removeFavorite(idDrink); | ||
} | ||
|
||
@override | ||
List<DrinkDetails> getFavorites() { | ||
return _localDataSource.getFavorites(); | ||
} | ||
|
||
@override | ||
bool isFavorite(String idDrink) { | ||
return _localDataSource.isFavorite(idDrink); | ||
} | ||
} |
28 changes: 20 additions & 8 deletions
28
lib/features/favourites/presentation/riverpod/favourites_notifier.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,25 +1,37 @@ | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
import '../../../drinks/infrastructure/dto/drink_details/drink_details_model.dart'; | ||
import '../../infrastructure/data_sources/local_data_source.dart'; | ||
import '../../domain/use_cases/add_to_favourites.dart'; | ||
import '../../domain/use_cases/get_favourites.dart'; | ||
import '../../domain/use_cases/is_favourite.dart'; | ||
import '../../domain/use_cases/remove_from_favourites.dart'; | ||
|
||
|
||
class FavoriteDrinksNotifier extends StateNotifier<List<DrinkDetails>> { | ||
final FavoriteDrinksLocalDataSource _localDataSource; | ||
final AddFavoriteUseCase _addFavoriteUseCase; | ||
final RemoveFavoriteUseCase _removeFavoriteUseCase; | ||
final GetFavoritesUseCase _getFavoritesUseCase; | ||
final IsFavoriteUseCase _isFavoriteUseCase; | ||
|
||
FavoriteDrinksNotifier(this._localDataSource) : super(_localDataSource.getFavorites()); | ||
FavoriteDrinksNotifier( | ||
this._addFavoriteUseCase, | ||
this._removeFavoriteUseCase, | ||
this._getFavoritesUseCase, | ||
this._isFavoriteUseCase, | ||
) : super(_getFavoritesUseCase()); | ||
|
||
void addFavorite(DrinkDetails drink) { | ||
_localDataSource.addFavorite(drink); | ||
state = _localDataSource.getFavorites(); | ||
_addFavoriteUseCase(drink); | ||
state = _getFavoritesUseCase(); | ||
} | ||
|
||
void removeFavorite(String idDrink) { | ||
_localDataSource.removeFavorite(idDrink); | ||
state = _localDataSource.getFavorites(); | ||
_removeFavoriteUseCase(idDrink); | ||
state = _getFavoritesUseCase(); | ||
} | ||
|
||
bool isFavorite(String idDrink) { | ||
return _localDataSource.isFavorite(idDrink); | ||
return _isFavoriteUseCase(idDrink); | ||
} | ||
} | ||
|
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
74 changes: 74 additions & 0 deletions
74
lib/features/favourites/presentation/screens/favourites.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,74 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
import '../../../../core/router/routes.dart'; | ||
import '../../../drinks/presentation/riverpod/drink_details/selected_drink_provider.dart'; | ||
import '../riverpod/favourites_provider.dart'; | ||
import '../widgets/favourite_drink_item.dart'; | ||
|
||
class FavoritesScreen extends ConsumerWidget { | ||
const FavoritesScreen({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context, WidgetRef ref) { | ||
final favorites = ref.watch(favoriteDrinksNotifierProvider); | ||
|
||
return Scaffold( | ||
appBar: AppBar( | ||
automaticallyImplyLeading: false, | ||
leading: IconButton( | ||
icon: const Icon(Icons.arrow_back_ios), | ||
onPressed: () { | ||
Navigator.pop(context); | ||
}, | ||
), | ||
elevation: 2, | ||
title: const Text( | ||
'Favorites', | ||
style: TextStyle( | ||
fontSize: 24, | ||
fontWeight: FontWeight.bold, | ||
), | ||
), | ||
), | ||
body: SingleChildScrollView( | ||
child: favorites.isEmpty | ||
? const Center( | ||
child: Text( | ||
'No favorites yet', | ||
style: TextStyle(fontSize: 18), | ||
), | ||
) | ||
: RefreshIndicator( | ||
onRefresh: () async { | ||
// ref.refresh(favoriteDrinksNotifierProvider); | ||
}, | ||
child: StaggeredGrid.count( | ||
crossAxisCount: 2, | ||
mainAxisSpacing: 10, | ||
crossAxisSpacing: 10, | ||
children: List.generate( | ||
favorites.length, | ||
(index) { | ||
return GestureDetector( | ||
onTap: () { | ||
ref.read(selectedDrinkProvider.notifier).state = | ||
favorites[index].idDrink; | ||
|
||
context.goNamed( | ||
Routes.drinkDetails.name, | ||
); | ||
}, | ||
child: FavouriteDrinkItem( | ||
drink: favorites[index], | ||
), | ||
); | ||
}, | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.