Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:rxdart/rxdart.dart';
- import '../resources/contacts_db_provider.dart';
- import '../models/contact_model.dart';
- class ContactsBloc {
- final _addContactsObject = BehaviorSubject<ContactModel>();
- final _fetchAllContactObject = BehaviorSubject<List<ContactModel>>();
- Observable<ContactModel> save;
- ContactsBloc() {
- save = _addContactsObject.stream;
- save.listen(contactsDbProvider.saveContact);
- }
- // Add Contacts
- addContact(Map<String, dynamic> map) {
- ContactModel contact = ContactModel.fromMap(map);
- //print(contact);
- _addContactsObject.sink.add(contact);
- }
- // Fetch All Contacts
- getContacts() {
- contactsDbProvider.initDb();
- Future.delayed(Duration(seconds: 1)).then((res) {
- contactsDbProvider.fetchAll().then((contacts) {
- _fetchAllContactObject.sink.add(contacts);
- });
- });
- }
- Observable<List<ContactModel>> get contacts => _fetchAllContactObject.map((contacts){
- return contacts;
- });
- dispose() {
- _addContactsObject.close();
- _fetchAllContactObject.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement