2020-07-06 20:09:03 +00:00
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:cake_wallet/store/contact_list_store.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/contact.dart';
|
2020-07-06 20:09:03 +00:00
|
|
|
|
|
|
|
class ContactService {
|
|
|
|
ContactService(this.contactSource, this.contactListStore) {
|
|
|
|
_forceUpdateContactListStore();
|
|
|
|
}
|
|
|
|
|
|
|
|
final Box<Contact> contactSource;
|
|
|
|
final ContactListStore contactListStore;
|
|
|
|
|
|
|
|
Future add(Contact contact) async {
|
|
|
|
await contactSource.add(contact);
|
2020-09-07 15:13:39 +00:00
|
|
|
// contactListStore.contacts.add(contact);
|
2020-07-06 20:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future update(Contact contact) async {
|
|
|
|
await contact.save();
|
|
|
|
final index = contactListStore.contacts.indexOf(contact) ?? -1;
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
_forceUpdateContactListStore();
|
|
|
|
} else {
|
|
|
|
contactListStore.contacts.add(contact);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future delete(Contact contact) async {
|
|
|
|
await contact.delete();
|
|
|
|
contactListStore.contacts.remove(contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _forceUpdateContactListStore() {
|
|
|
|
contactListStore.contacts.clear();
|
|
|
|
contactListStore.contacts.addAll(contactSource.values);
|
|
|
|
}
|
|
|
|
}
|