Skip to content

Instantly share code, notes, and snippets.

@boeledi
Last active March 27, 2023 14:26
Show Gist options
  • Save boeledi/ca53516029c2066a2c8011f4cf101f99 to your computer and use it in GitHub Desktop.
Save boeledi/ca53516029c2066a2c8011f4cf101f99 to your computer and use it in GitHub Desktop.
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