Advertisement
rodrigofbm

Untitled

Aug 11th, 2019
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.09 KB | None | 0 0
  1. import 'package:rxdart/rxdart.dart';
  2. import '../resources/contacts_db_provider.dart';
  3. import '../models/contact_model.dart';
  4.  
  5. class ContactsBloc {
  6.   final _addContactsObject = BehaviorSubject<ContactModel>();
  7.   final _fetchAllContactObject = BehaviorSubject<List<ContactModel>>();
  8.   Observable<ContactModel> save;
  9.  
  10.   ContactsBloc() {
  11.     save = _addContactsObject.stream;
  12.     save.listen(contactsDbProvider.saveContact);
  13.   }
  14.  
  15.   // Add Contacts
  16.   addContact(Map<String, dynamic> map)  {
  17.     ContactModel contact = ContactModel.fromMap(map);
  18.  
  19.     //print(contact);
  20.     _addContactsObject.sink.add(contact);
  21.   }
  22.  
  23.   // Fetch All Contacts
  24.  
  25.   getContacts() {
  26.     contactsDbProvider.initDb();
  27.  
  28.     Future.delayed(Duration(seconds: 1)).then((res) {
  29.       contactsDbProvider.fetchAll().then((contacts) {
  30.         _fetchAllContactObject.sink.add(contacts);
  31.       });
  32.     });
  33.   }
  34.  
  35.   Observable<List<ContactModel>> get contacts => _fetchAllContactObject.map((contacts){
  36.     return contacts;
  37.   });
  38.  
  39.   dispose() {
  40.     _addContactsObject.close();
  41.     _fetchAllContactObject.close();
  42.   }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement