Skip to content

Commit

Permalink
Add products to favourites.
Browse files Browse the repository at this point in the history
  • Loading branch information
4seer committed May 5, 2020
1 parent 7e7abd4 commit 9620f01
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 22 deletions.
3 changes: 2 additions & 1 deletion lib/data/abstract/favorites_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:collection';

import 'package:openflutterecommerce/config/theme.dart';
import 'package:openflutterecommerce/data/abstract/model/favorite_product.dart';
import 'package:openflutterecommerce/data/abstract/model/product.dart';

import 'model/filter_rules.dart';
import 'model/product_attribute.dart';
Expand All @@ -26,7 +27,7 @@ abstract class FavoritesRepository {
///that all selectable properties should be set so it will be easy to order
///it then
Future addToFavorites(
int productId, HashMap<ProductAttribute, String> selectedAttributes);
Product product, HashMap<ProductAttribute, String> selectedAttributes);

///removes product with [productId] from the list of favorites
Future removeFromFavorites(int productId);
Expand Down
2 changes: 1 addition & 1 deletion lib/data/abstract/model/product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Product extends Equatable {
this.description,
@required this.price,
this.discountPercent = 0,
this.amountAvailable = 10,
this.amountAvailable = 0,
DateTime created,
this.averageRating,
this.ratingCount = 0,
Expand Down
13 changes: 3 additions & 10 deletions lib/data/repositories/product_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,17 @@ class ProductRepositoryImpl extends ProductRepository with FavoritesRepository {
}

@override
Future addToFavorites(int productId, HashMap<ProductAttribute, String> selectedAttributes) async {
Product product;

_dataStorage.products?.forEach((tempProduct) =>{
if ( tempProduct.id == productId)
product = tempProduct
});

Future addToFavorites(Product product, HashMap<ProductAttribute, String> selectedAttributes) async {
_dataStorage.favProducts.add(FavoriteProduct(product, selectedAttributes));
}

@override
Future<List<FavoriteProduct>> getFavoriteProducts({int pageIndex = 0, int pageSize = AppConsts.page_size,
SortRules sortRules = const SortRules(), FilterRules filterRules}) async {
//TODO: remove when favorite feature will be implemented
_dataStorage.products = await getProducts();
/*_dataStorage.products = await getProducts();
_dataStorage.products.forEach((product) =>
_dataStorage.favProducts.add(FavoriteProduct(product, HashMap())));
_dataStorage.favProducts.add(FavoriteProduct(product, HashMap())));*/
return _dataStorage.favProducts;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ProductBloc extends Bloc<ProductEvent, ProductState> {
similarProducts: data.similarProducts);
} else if (event is ProductAddToFavoritesEvent) {
await favoriesRepository.addToFavorites(
productId, event.selectedAttributes);
event.product, event.selectedAttributes);
} else if (event is ProductAddToFavoritesEvent) {
await favoriesRepository.removeFromFavorites(productId);
} else if (event is ProductAddToCartEvent) {
Expand Down
4 changes: 3 additions & 1 deletion lib/presentation/features/product_details/product_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:collection';
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:openflutterecommerce/data/abstract/model/product.dart';
import 'package:openflutterecommerce/data/abstract/model/product_attribute.dart';

@immutable
Expand All @@ -24,9 +25,10 @@ class ProductScreenLoadedEvent extends ProductEvent {

@immutable
class ProductAddToFavoritesEvent extends ProductEvent {
final Product product;
final HashMap<ProductAttribute, String> selectedAttributes;

ProductAddToFavoritesEvent(this.selectedAttributes);
ProductAddToFavoritesEvent(this.product, this.selectedAttributes);
}

@immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class _ProductDetailsViewState extends State<ProductDetailsView> {
void setFavourite(ProductBloc bloc) {
if (!favorite) {
bloc.add(ProductAddToFavoritesEvent(
widget.product,
HashMap())); //TODO ask for real parameters if required
} else {
bloc.add(ProductRemoveFromFavoritesEvent());
Expand Down
6 changes: 3 additions & 3 deletions lib/presentation/features/products/products_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ class ProductsBloc extends Bloc<ProductsListEvent, ProductsState> {
} else if (event is ProductMakeFavoriteEvent) {
if (event.isFavorite) {
await favoritesRepository.addToFavorites(
event.productId, event.favoriteAttributes);
event.product, event.favoriteAttributes);
} else {
await favoritesRepository.removeFromFavorites(event.productId);
await favoritesRepository.removeFromFavorites(event.product.id);
}
final List<Product> data = state.data.products;
yield state.copyWith(
data: state.data.copyWith(data.map((item) {
if (event.productId == item.id) {
if (event.product.id == item.id) {
return item.favorite(event.isFavorite);
} else {
return item;
Expand Down
7 changes: 4 additions & 3 deletions lib/presentation/features/products/products_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:openflutterecommerce/data/abstract/model/filter_rules.dart';
import 'package:openflutterecommerce/data/abstract/model/hashtag.dart';
import 'package:openflutterecommerce/data/abstract/model/product.dart';
import 'package:openflutterecommerce/data/abstract/model/product_attribute.dart';
import 'package:openflutterecommerce/data/abstract/model/sort_rules.dart';

Expand Down Expand Up @@ -70,11 +71,11 @@ class ProductSearchEvent extends ProductsListEvent {

class ProductMakeFavoriteEvent extends ProductsListEvent {
final bool isFavorite;
final int productId;
final Product product;
final HashMap<ProductAttribute, String> favoriteAttributes;

ProductMakeFavoriteEvent(this.isFavorite, this.productId, {this.favoriteAttributes});
ProductMakeFavoriteEvent(this.isFavorite, this.product, {this.favoriteAttributes});

@override
List<Object> get props => [isFavorite, productId];
List<Object> get props => [isFavorite, product, favoriteAttributes];
}
2 changes: 1 addition & 1 deletion lib/presentation/features/products/views/list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ProductsListView extends StatelessWidget {
BlocProvider.of<ProductsBloc>(context).add(
ProductMakeFavoriteEvent(
!state.data.products[index].isFavorite,
state.data.products[index].id));
state.data.products[index]));
},
)) ;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/features/products/views/tile_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ProductsTileView extends StatelessWidget {
BlocProvider.of<ProductsBloc>(context).add(
ProductMakeFavoriteEvent(
!state.data.products[index].isFavorite,
state.data.products[index].id));
state.data.products[index]));
},
showProductInfo: () {
Navigator.of(context).pushNamed(
Expand Down

0 comments on commit 9620f01

Please sign in to comment.