-
-
Save boeledi/ca53516029c2066a2c8011f4cf101f99 to your computer and use it in GitHub Desktop.
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
class ShoppingItemBloc implements BlocBase { | |
// Stream to notify if the ShoppingItemWidget is part of the shopping basket | |
final BehaviorSubject<bool> _isInShoppingBasketController = BehaviorSubject<bool>(); | |
Stream<bool> get isInShoppingBasket => _isInShoppingBasketController; | |
// Stream that receives the list of all items, part of the shopping basket | |
final PublishSubject<List<ShoppingItem>> _shoppingBasketController = PublishSubject<List<ShoppingItem>>(); | |
Function(List<ShoppingItem>) get shoppingBasket => _shoppingBasketController.sink.add; | |
// Constructor with the 'identity' of the shoppingItem | |
ShoppingItemBloc(ShoppingItem shoppingItem){ | |
// Each time a variation of the content of the shopping basket | |
_shoppingBasketController.stream | |
// we check if this shoppingItem is part of the shopping basket | |
.map((list) => list.any((ShoppingItem item) => item.id == shoppingItem.id)) | |
// if it is part | |
.listen((isInShoppingBasket) | |
// we notify the ShoppingItemWidget | |
=> _isInShoppingBasketController.add(isInShoppingBasket)); | |
} | |
@override | |
void dispose() { | |
_isInShoppingBasketController.close(); | |
_shoppingBasketController.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment