stack_wallet/lib/services/address_book_service.dart

88 lines
2.7 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
2022-08-26 08:11:35 +00:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
2023-05-14 15:06:47 +00:00
import 'package:stackwallet/db/isar/main_db.dart';
import 'package:stackwallet/models/isar/models/contact_entry.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/utilities/enums/coin_enum.dart';
class AddressBookService extends ChangeNotifier {
ContactEntry getContactById(String id) {
2023-05-14 15:06:47 +00:00
ContactEntry? contactEntry = MainDB.instance.getContactEntry(id: id);
if (contactEntry == null) {
throw Exception('Contact ID "$id" not found!');
2023-05-14 15:06:47 +00:00
} else {
return contactEntry;
2022-08-26 08:11:35 +00:00
}
}
List<ContactEntry> get contacts => MainDB.instance.getContactEntries();
2022-08-26 08:11:35 +00:00
/// search address book entries
//TODO search using isar queries
Future<List<ContactEntry>> search(String text) async {
if (text.isEmpty) return contacts;
var results = contacts.toList();
2022-08-26 08:11:35 +00:00
results.retainWhere((contact) => matches(text, contact));
return results;
}
bool matches(String term, ContactEntry contact) {
if (term.isEmpty) {
return true;
}
2022-08-26 08:11:35 +00:00
final text = term.toLowerCase();
if (contact.name.toLowerCase().contains(text)) {
return true;
}
for (int i = 0; i < contact.addresses.length; i++) {
if (contact.addresses[i].label.toLowerCase().contains(text) ||
contact.addresses[i].coin.name.toLowerCase().contains(text) ||
contact.addresses[i].coin.prettyName.toLowerCase().contains(text) ||
contact.addresses[i].coin.ticker.toLowerCase().contains(text) ||
contact.addresses[i].address.toLowerCase().contains(text)) {
return true;
}
}
return false;
}
/// add contact
///
/// returns false if it provided [contact]'s id already exists in the database
/// other true if the [contact] was saved
Future<bool> addContact(ContactEntry contact) async {
if (await MainDB.instance.isContactEntryExists(id: contact.customId)) {
2022-08-26 08:11:35 +00:00
return false;
2023-05-14 15:06:47 +00:00
} else {
await MainDB.instance.putContactEntry(contactEntry: contact);
notifyListeners();
2023-05-14 15:06:47 +00:00
return true;
2022-08-26 08:11:35 +00:00
}
}
/// Edit contact
Future<bool> editContact(ContactEntry editedContact) async {
2022-08-26 08:11:35 +00:00
// over write the contact with edited version
await MainDB.instance.putContactEntry(contactEntry: editedContact);
notifyListeners();
2022-08-26 08:11:35 +00:00
return true;
}
/// Remove address book contact entry from db if it exists
Future<void> removeContact(String id) async {
2023-05-14 15:06:47 +00:00
await MainDB.instance.deleteContactEntry(id: id);
2022-08-26 08:11:35 +00:00
notifyListeners();
}
}