From e5ecda2aea59a0eaff513823f1f227bc6bca7cd3 Mon Sep 17 00:00:00 2001
From: vjrj <vjrj@comunes.org>
Date: Sat, 30 Dec 2023 12:50:44 +0100
Subject: [PATCH] Add new Contacts to ContactsCache. Comparison of contacts

---
 lib/data/models/contact_cubit.dart | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/lib/data/models/contact_cubit.dart b/lib/data/models/contact_cubit.dart
index bdd203ec..b9b0d72f 100644
--- a/lib/data/models/contact_cubit.dart
+++ b/lib/data/models/contact_cubit.dart
@@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart';
 import 'package:hydrated_bloc/hydrated_bloc.dart';
 
 import '../../g1/g1_helper.dart';
+import '../../ui/contacts_cache.dart';
 import '../../ui/ui_helpers.dart';
 import 'contact.dart';
 import 'contact_sort_type.dart';
@@ -42,6 +43,7 @@ class ContactsCubit extends HydratedCubit<ContactsState> {
         filteredContacts: updatedFilteredContacts,
       ));
     }
+    ContactsCache().addContact(contact);
   }
 
   void removeContact(Contact contact) {
@@ -94,6 +96,7 @@ class ContactsCubit extends HydratedCubit<ContactsState> {
       }
       return c;
     }).toList();
+    ContactsCache().addContact(contact);
     emit(state.copyWith(contacts: contacts, filteredContacts: fcontacts));
   }
 
@@ -197,6 +200,11 @@ class ContactsCubit extends HydratedCubit<ContactsState> {
   @override
   String get id => 'contacts';
 
-  bool isContact(String pubKey) =>
-      state.contacts.any((Contact c) => c.pubKey == pubKey);
+  bool isContact(String pubKey) => state.contacts.any(
+      (Contact c) => extractPublicKey(c.pubKey) == extractPublicKey(pubKey));
+
+  Contact? getContact(String pubKey) => isContact(pubKey)
+      ? state.contacts.firstWhere(
+          (Contact c) => extractPublicKey(c.pubKey) == extractPublicKey(pubKey))
+      : null;
 }
-- 
GitLab