Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:collection';
- import 'dart:convert';
- import 'package:bloc_pattern/bloc_pattern.dart';
- import 'package:rxdart/rxdart.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import 'package:tutorial_nox_vscode/models/Products.dart';
- class FavoriteBloc extends BlocBase {
- ///
- /// Unique list of all favorite movies
- ///
- Set<ProductsModel> _favorites = Set<ProductsModel>();
- // ########## STREAMS ##############
- ///
- /// Interface that allows to add a new favorite product
- ///
- BehaviorSubject<ProductsModel> _favoriteAddController =
- BehaviorSubject<ProductsModel>();
- Sink<ProductsModel> get inAddFavorite => _favoriteAddController.sink;
- ///
- /// Interface that allows to remove a product from the list of favorites
- ///
- BehaviorSubject<ProductsModel> _favoriteRemoveController =
- BehaviorSubject<ProductsModel>();
- Sink<ProductsModel> get inRemoveFavorite => _favoriteRemoveController.sink;
- ///
- /// Interface that allows to get the total number of favorites
- ///
- BehaviorSubject<int> _favoriteTotalController =
- BehaviorSubject<int>.seeded(0);
- Sink<int> get _inTotalFavorites => _favoriteTotalController.sink;
- Stream<int> get outTotalFavorites => _favoriteTotalController.stream;
- ///
- /// Interface that allows to get the list of all favorite prdoducts
- ///
- final _favoritesController = BehaviorSubject<List<ProductsModel>>.seeded([]);
- Sink<List<ProductsModel>> get _inFavorites => _favoritesController.sink;
- Stream<List<ProductsModel>> get outFavorites => _favoritesController.stream;
- ///
- /// Constructor
- ///
- FavoriteBloc() {
- _favoriteAddController.listen(_handleAddFavorite);
- _favoriteRemoveController.listen(_handleRemoveFavorite);
- SharedPreferences.getInstance().then((prefs) {
- if (prefs.getKeys().contains('fav')) {
- _favorites = json.decode(prefs.getString('fav')).map((k, v) {
- return MapEntry(k, ProductsModel.fromJson(v));
- }).cast<String, ProductsModel>();
- _favoritesController.add(_favorites.toList());
- }
- });
- }
- void _saveFav() {
- SharedPreferences.getInstance().then((prefs) {
- prefs.setString('fav', json.encode(_favorites.toList()));
- });
- }
- void dispose() {
- _favoriteAddController.close();
- _favoriteRemoveController.close();
- _favoriteTotalController.close();
- _favoritesController.close();
- super.dispose();
- }
- // ############# HANDLING #####################
- void _handleAddFavorite(ProductsModel product) {
- // Add the product to the list of favorite ones
- _favorites.add(product);
- _saveFav();
- _notify();
- }
- void _handleRemoveFavorite(ProductsModel product) {
- _favorites.remove(product);
- _saveFav();
- _notify();
- }
- void _notify() {
- // Send to whomever is interested...
- // The total number of favorites
- _inTotalFavorites.add(_favorites.length);
- // The new list of all favorite products
- _inFavorites.add(UnmodifiableListView(_favorites));
- }
- }
- // Buttons products_page
- StreamBuilder<bool>(
- stream: favoritesBloc.outIsFavorite,
- initialData: false,
- builder: (context, snapshot) {
- return IconButton(
- icon: Icon(
- snapshot.data
- ? Icons.favorite
- : Icons.favorite_border),
- color: Colors.amber[500],
- onPressed: () {
- snapshot.data
- ? bloc.inRemoveFavorite.add(item)
- : bloc.inAddFavorite.add(item);
- },
- );
- }),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement