Skip to content

Commit 18c7b00

Browse files
committed
handle duplicated contacts
1 parent c866d40 commit 18c7b00

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

app/lib/screens/wallets/contacts.dart

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,21 @@ class _ContactsScreenState extends State<ContactsScreen> {
3838
}
3939

4040
_loadFavouriteContacts() async {
41-
myPkidContacts = await getPkidContacts();
42-
myPkidContacts =
43-
myPkidContacts.where((c) => c.type == widget.chainType).toList();
41+
final allContacts = await getPkidContacts();
42+
43+
final seen = <String>{};
44+
final filtered = <PkidContact>[];
45+
46+
for (final c in allContacts) {
47+
if (c.type != widget.chainType) continue;
48+
49+
final key = '${c.address}_${c.type}';
50+
if (seen.add(key)) {
51+
filtered.add(c);
52+
}
53+
}
54+
55+
myPkidContacts = filtered;
4456
setState(() {});
4557
}
4658

@@ -141,8 +153,9 @@ class _ContactsScreenState extends State<ContactsScreen> {
141153
children: [
142154
ContactsWidget(
143155
contacts: myWalletContacts
144-
.where(
145-
(c) => c.address != widget.currentWalletAddress && c.type == widget.chainType)
156+
.where((c) =>
157+
c.address != widget.currentWalletAddress &&
158+
c.type == widget.chainType)
146159
.toList(),
147160
onSelectToAddress: widget.onSelectToAddress),
148161
ContactsWidget(

app/lib/widgets/wallets/add_edit_contact.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,16 @@ class _AddEditContactState extends State<AddEditContact> {
127127
return;
128128
}
129129
if (chainType == widget.chainType) {
130-
widget.onAddContact!(PkidContact(
131-
name: contactName, address: contactAddress, type: chainType));
130+
final alreadyExists = widget.contacts
131+
.any((c) => c.address == contactAddress && c.type == chainType);
132+
133+
if (!alreadyExists) {
134+
widget.onAddContact!(PkidContact(
135+
name: contactName,
136+
address: contactAddress,
137+
type: chainType,
138+
));
139+
}
132140
}
133141
if (!context.mounted) return;
134142
Navigator.pop(context);

0 commit comments

Comments
 (0)