Advertisement
multiarts

FavoriteBloc

Dec 27th, 2019
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.47 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:collection';
  3. import 'dart:convert';
  4.  
  5. import 'package:bloc_pattern/bloc_pattern.dart';
  6. import 'package:rxdart/rxdart.dart';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. import 'package:tutorial_nox_vscode/models/Products.dart';
  9.  
  10. class FavoriteBloc extends BlocBase {
  11.   ///
  12.   /// Unique list of all favorite movies
  13.   ///
  14.   Set<ProductsModel> _favorites = Set<ProductsModel>();
  15.  
  16.   // ##########  STREAMS  ##############
  17.   ///
  18.   /// Interface that allows to add a new favorite product
  19.   ///
  20.   BehaviorSubject<ProductsModel> _favoriteAddController =
  21.       BehaviorSubject<ProductsModel>();
  22.   Sink<ProductsModel> get inAddFavorite => _favoriteAddController.sink;
  23.  
  24.   ///
  25.   /// Interface that allows to remove a product from the list of favorites
  26.   ///
  27.   BehaviorSubject<ProductsModel> _favoriteRemoveController =
  28.       BehaviorSubject<ProductsModel>();
  29.   Sink<ProductsModel> get inRemoveFavorite => _favoriteRemoveController.sink;
  30.  
  31.   ///
  32.   /// Interface that allows to get the total number of favorites
  33.   ///
  34.   BehaviorSubject<int> _favoriteTotalController =
  35.       BehaviorSubject<int>.seeded(0);
  36.   Sink<int> get _inTotalFavorites => _favoriteTotalController.sink;
  37.   Stream<int> get outTotalFavorites => _favoriteTotalController.stream;
  38.  
  39.   ///
  40.   /// Interface that allows to get the list of all favorite prdoducts
  41.   ///
  42.   final _favoritesController = BehaviorSubject<List<ProductsModel>>.seeded([]);
  43.   Sink<List<ProductsModel>> get _inFavorites => _favoritesController.sink;
  44.   Stream<List<ProductsModel>> get outFavorites => _favoritesController.stream;
  45.  
  46.   ///
  47.   /// Constructor
  48.   ///
  49.   FavoriteBloc() {
  50.     _favoriteAddController.listen(_handleAddFavorite);
  51.     _favoriteRemoveController.listen(_handleRemoveFavorite);
  52.  
  53.     SharedPreferences.getInstance().then((prefs) {
  54.       if (prefs.getKeys().contains('fav')) {
  55.         _favorites = json.decode(prefs.getString('fav')).map((k, v) {
  56.           return MapEntry(k, ProductsModel.fromJson(v));
  57.         }).cast<String, ProductsModel>();
  58.         _favoritesController.add(_favorites.toList());
  59.       }
  60.     });
  61.   }
  62.  
  63.   void _saveFav() {
  64.     SharedPreferences.getInstance().then((prefs) {
  65.       prefs.setString('fav', json.encode(_favorites.toList()));
  66.     });
  67.   }
  68.  
  69.   void dispose() {
  70.     _favoriteAddController.close();
  71.     _favoriteRemoveController.close();
  72.     _favoriteTotalController.close();
  73.     _favoritesController.close();
  74.     super.dispose();
  75.   }
  76.  
  77.   // ############# HANDLING  #####################
  78.  
  79.   void _handleAddFavorite(ProductsModel product) {
  80.     // Add the product to the list of favorite ones
  81.     _favorites.add(product);
  82.     _saveFav();
  83.  
  84.     _notify();
  85.   }
  86.  
  87.   void _handleRemoveFavorite(ProductsModel product) {
  88.     _favorites.remove(product);
  89.     _saveFav();
  90.  
  91.     _notify();
  92.   }
  93.  
  94.   void _notify() {
  95.     // Send to whomever is interested...
  96.     // The total number of favorites
  97.     _inTotalFavorites.add(_favorites.length);
  98.  
  99.     // The new list of all favorite products
  100.     _inFavorites.add(UnmodifiableListView(_favorites));
  101.   }
  102. }
  103.  
  104. // Buttons products_page
  105. StreamBuilder<bool>(
  106.  stream: favoritesBloc.outIsFavorite,
  107.  initialData: false,
  108.  builder: (context, snapshot) {
  109.   return IconButton(
  110.    icon: Icon(
  111.     snapshot.data
  112.     ? Icons.favorite
  113.     : Icons.favorite_border),
  114.    color: Colors.amber[500],
  115.    onPressed: () {
  116.     snapshot.data
  117.     ? bloc.inRemoveFavorite.add(item)
  118.     : bloc.inAddFavorite.add(item);
  119.   },
  120.  );
  121. }),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement