Skip to content

Commit ba81c85

Browse files
committed
Fixed the duplicated contacts issue
1 parent f0f61ea commit ba81c85

3 files changed

Lines changed: 30 additions & 18 deletions

File tree

app/lib/screens/wallets/contacts.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@ class _ContactsScreenState extends State<ContactsScreen> {
4343
}
4444

4545
_onAddContact(PkidContact contact) async {
46-
final exists = myPkidContacts.any((c) => c.address == contact.address);
47-
if (!exists) {
48-
myPkidContacts.add(contact);
49-
setState(() {});
50-
}
46+
myPkidContacts.add(contact);
47+
setState(() {});
5148
}
5249

5350
_openAddContactOverlay() {
@@ -61,6 +58,7 @@ class _ContactsScreenState extends State<ContactsScreen> {
6158
onAddContact: _onAddContact,
6259
chainType: widget.chainType,
6360
contacts: [...myPkidContacts, ...myWalletContacts],
61+
getAllContacts: () => [...myPkidContacts, ...myWalletContacts],
6462
));
6563
}
6664

@@ -146,14 +144,14 @@ class _ContactsScreenState extends State<ContactsScreen> {
146144
c.address != widget.currentWalletAddress &&
147145
c.type == widget.chainType)
148146
.toList(),
147+
chainType: widget.chainType,
149148
onSelectToAddress: widget.onSelectToAddress),
150149
ContactsWidget(
151-
contacts: myPkidContacts
152-
.where((c) => c.type == widget.chainType)
153-
.toList(),
150+
contacts: myPkidContacts,
154151
onSelectToAddress: widget.onSelectToAddress,
155152
onDeleteContact: _onDeleteContact,
156153
onEditContact: _openEditContactOverlay,
154+
chainType: widget.chainType,
157155
canEditAndDelete: true,
158156
),
159157
],

app/lib/widgets/wallets/add_edit_contact.dart

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AddEditContact extends StatefulWidget {
1717
this.name = '',
1818
this.address = '',
1919
this.onEditContact,
20+
this.getAllContacts,
2021
});
2122

2223
final void Function(PkidContact addedContact)? onAddContact;
@@ -27,6 +28,7 @@ class AddEditContact extends StatefulWidget {
2728
final String address;
2829
final void Function(String oldName, String newName, String newAddress)?
2930
onEditContact;
31+
final List<PkidContact> Function()? getAllContacts;
3032

3133
@override
3234
State<StatefulWidget> createState() {
@@ -81,27 +83,34 @@ class _AddEditContactState extends State<AddEditContact> {
8183

8284
bool _validateAddress(String contactAddress, {bool edit = false}) {
8385
addressError = null;
86+
final allContacts = widget.getAllContacts != null
87+
? widget.getAllContacts!()
88+
: widget.contacts;
8489

8590
if (contactAddress.isEmpty) {
8691
addressError = "Address can't be empty";
8792
return false;
8893
}
89-
// Check for duplicates across all chain types
90-
final contacts = widget.contacts.where((c) => c.address == contactAddress);
91-
if (contacts.isNotEmpty) {
94+
95+
final isDuplicate = allContacts.any((c) => c.address == contactAddress);
96+
97+
if (isDuplicate) {
9298
addressError = 'Address is used in another contact';
9399
return false;
94100
}
101+
95102
if (_selectedChainType == ChainType.TFChain &&
96103
contactAddress.length != 48) {
97104
addressError = 'Address length should be 48 characters';
98105
return false;
99106
}
107+
100108
if (_selectedChainType == ChainType.Stellar &&
101109
!isValidStellarAddress(contactAddress)) {
102110
addressError = 'Invaild Stellar address';
103111
return false;
104112
}
113+
105114
return true;
106115
}
107116

@@ -122,10 +131,12 @@ class _AddEditContactState extends State<AddEditContact> {
122131
Icons.error, DialogType.Error);
123132
return;
124133
}
125-
if (chainType == widget.chainType) {
126-
widget.onAddContact!(PkidContact(
127-
name: contactName, address: contactAddress, type: chainType));
128-
}
134+
widget.onAddContact?.call(PkidContact(
135+
name: contactName,
136+
address: contactAddress,
137+
type: chainType,
138+
));
139+
129140
if (!context.mounted) return;
130141
Navigator.pop(context);
131142
}

app/lib/widgets/wallets/contacts_widget.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:threebotlogin/models/contact.dart';
3+
import 'package:threebotlogin/models/wallet.dart';
34
import 'package:threebotlogin/widgets/wallets/contact_card.dart';
45

56
class ContactsWidget extends StatelessWidget {
@@ -9,19 +10,21 @@ class ContactsWidget extends StatelessWidget {
910
required this.onSelectToAddress,
1011
this.onDeleteContact,
1112
this.onEditContact,
13+
required this.chainType,
1214
this.canEditAndDelete = false,
1315
});
14-
1516
final List<PkidContact> contacts;
1617
final void Function(String address) onSelectToAddress;
1718
final bool canEditAndDelete;
1819
final void Function(String name)? onDeleteContact;
1920
final void Function(String oldName, String oldAddress)? onEditContact;
21+
final ChainType chainType;
2022

2123
@override
2224
Widget build(BuildContext context) {
25+
final filteredContacts = contacts.where((c) => c.type == chainType).toList();
2326
Widget content;
24-
if (contacts.isEmpty) {
27+
if (filteredContacts.isEmpty) {
2528
content = Center(
2629
child: Text(
2730
'No contacts yet.',
@@ -33,7 +36,7 @@ class ContactsWidget extends StatelessWidget {
3336
);
3437
} else {
3538
content = ListView(children: [
36-
for (final contact in contacts)
39+
for (final contact in filteredContacts)
3740
InkWell(
3841
onTap: () {
3942
onSelectToAddress(contact.address);

0 commit comments

Comments
 (0)