Skip to content

Commit

Permalink
Add to favorites from list.
Browse files Browse the repository at this point in the history
  • Loading branch information
4seer committed May 5, 2020
1 parent 9620f01 commit d95078d
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 14 deletions.
1 change: 1 addition & 0 deletions ios/Flutter/.last_build_id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0e81f4131a64923bd5ca5804c4d3b13f
3 changes: 0 additions & 3 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@
/* Begin XCBuildConfiguration section */
249021D3217E4FDB00AE95B9 /* Profile */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
Expand Down Expand Up @@ -384,7 +383,6 @@
};
97C147031CF9000F007C117D /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 9740EEB21CF90195004384FC /* Debug.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
Expand Down Expand Up @@ -440,7 +438,6 @@
};
97C147041CF9000F007C117D /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
Expand Down
5 changes: 4 additions & 1 deletion lib/data/abstract/favorites_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ abstract class FavoritesRepository {
Product product, HashMap<ProductAttribute, String> selectedAttributes);

///removes product with [productId] from the list of favorites
Future removeFromFavorites(int productId);
Future<List<FavoriteProduct>> removeFromFavorites(int productId);

//check if product was added to favorite
bool checkFavorite(int productId);

///returns filter options available for favorite products.
///All rules should be set with initial (unselected) values
Expand Down
20 changes: 20 additions & 0 deletions lib/data/abstract/model/product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,24 @@ class Product extends Equatable {
CommerceImage get mainImage => (images != null && images.isNotEmpty)
? images.first
: CommerceImage.placeHolder();

Product copyWith({bool isFavorite}) {
return Product(
id,
title: title,
subTitle: subTitle,
shortDescription: description,
description: description,
price: price ?? 0,
discountPercent: discountPercent,
amountAvailable: amountAvailable,
averageRating: averageRating,
categories: categories,
hashTags: hashTags,
ratingCount: ratingCount,
images: images,
isFavorite: isFavorite ?? isFavorite,
selectableAttributes: selectableAttributes
);
}
}
42 changes: 33 additions & 9 deletions lib/data/repositories/product_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'package:openflutterecommerce/locator.dart';
//Uses remote or local data depending on NetworkStatus
class ProductRepositoryImpl extends ProductRepository with FavoritesRepository {

final ProductDataStorage _dataStorage = ProductDataStorage();
static ProductDataStorage dataStorage = ProductDataStorage();

@override
Future<Product> getProduct(int id) {
Expand Down Expand Up @@ -64,17 +64,27 @@ class ProductRepositoryImpl extends ProductRepository with FavoritesRepository {
productRepository = LocalProductRepository();
}

_dataStorage.products = await productRepository.getProducts();
List<Product> products = await productRepository.getProducts();

return _dataStorage.products;
//check favorites
dataStorage.products = [];
products.forEach( (product) =>{
dataStorage.products.add(
product.copyWith(
isFavorite: checkFavorite(product.id)
)
)
});

return dataStorage.products;
} on HttpRequestException {
throw RemoteServerException();
}
}

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

@override
Expand All @@ -84,19 +94,33 @@ class ProductRepositoryImpl extends ProductRepository with FavoritesRepository {
/*_dataStorage.products = await getProducts();
_dataStorage.products.forEach((product) =>
_dataStorage.favProducts.add(FavoriteProduct(product, HashMap())));*/
return _dataStorage.favProducts;
return dataStorage.favProducts;
}

@override
Future<FilterRules> getFavoritesFilterOptions() async {
//TODO: remove when favorite feature will be implemented
return FilterRules.getSelectableAttributes(_dataStorage.products);
return FilterRules.getSelectableAttributes(dataStorage.products);
}

@override
Future<List<FavoriteProduct>> removeFromFavorites(int productId) async {
//TODO: remove from database in the future
dataStorage.favProducts.removeWhere((product) => product.product.id == productId);
return dataStorage.favProducts;
}

@override
Future removeFromFavorites(int productId) {
// TODO: implement removeFromFavorites
return null;
bool checkFavorite(int productId) {
// TODO: implement checkFavorite
bool isFavorite = false;
for( int i = 0; i < dataStorage.favProducts.length; i++) {
if ( dataStorage.favProducts[i].product.id == productId) {
isFavorite = true;
break;
}
}
return isFavorite;
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/presentation/features/favorites/favorites_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class FavouriteBloc extends Bloc<FavouriteEvent, FavouriteState> {
yield state.copyWith(filterRules: event.filterRules, data: filteredData);
} else if (event is AddToCartEvent) {
//TODO add to cart
} else if ( event is RemoveFromFavoriteEvent ) {
yield state.getLoading();
final filteredData = await favoritesRepository.removeFromFavorites(event.productId);
yield state.copyWith(data: filteredData);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class FavoritesListView extends StatelessWidget {
));
}
},
childCount: state.data?.length
),
);
});
Expand Down
6 changes: 6 additions & 0 deletions lib/presentation/widgets/extensions/product_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ extension FavoriteView on FavoriteProduct {
? null
: 'Sorry, this item is currently sold out',
bottomRoundButton: FloatingActionButton(
heroTag: 'Remove from Cart' +Random()
.nextInt(1000000)
.toString(),
backgroundColor: AppColors.red,
onPressed: onAddToCart,
child: Icon(
Expand Down Expand Up @@ -192,6 +195,9 @@ extension FavoriteView on FavoriteProduct {
? null
: 'Sorry, this item is currently sold out',
bottomRoundButton: FloatingActionButton(
heroTag: 'Add to Cart' + Random()
.nextInt(1000000)
.toString(),
backgroundColor: AppColors.red,
onPressed: onAddToCart,
child: Icon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BaseProductListItem extends StatelessWidget {
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width,
height: imageHeight + 20,
height: imageHeight + 30,
padding: EdgeInsets.symmetric(horizontal: AppSizes.widgetSidePadding / 2),
child: Opacity(
opacity: inactiveMessage == null ? 1 : 0.6,
Expand Down

0 comments on commit d95078d

Please sign in to comment.