Skip to content

Commit a7fe9fb

Browse files
authored
Handle duplicated contacts in favorite List (#983)
* handle duplicated contacts * undo latest solution * fix adding duplicated wallet in fav list * Fixed the duplicated contacts issue * update condition when editing address
1 parent 26c533d commit a7fe9fb

3 files changed

Lines changed: 30 additions & 18 deletions

File tree

app/lib/screens/wallets/contacts.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ class _ContactsScreenState extends State<ContactsScreen> {
3939

4040
_loadFavouriteContacts() async {
4141
myPkidContacts = await getPkidContacts();
42-
myPkidContacts =
43-
myPkidContacts.where((c) => c.type == widget.chainType).toList();
4442
setState(() {});
4543
}
4644

@@ -60,6 +58,7 @@ class _ContactsScreenState extends State<ContactsScreen> {
6058
onAddContact: _onAddContact,
6159
chainType: widget.chainType,
6260
contacts: [...myPkidContacts, ...myWalletContacts],
61+
getAllContacts: () => [...myPkidContacts, ...myWalletContacts],
6362
));
6463
}
6564

@@ -141,15 +140,18 @@ class _ContactsScreenState extends State<ContactsScreen> {
141140
children: [
142141
ContactsWidget(
143142
contacts: myWalletContacts
144-
.where(
145-
(c) => c.address != widget.currentWalletAddress && c.type == widget.chainType)
143+
.where((c) =>
144+
c.address != widget.currentWalletAddress &&
145+
c.type == widget.chainType)
146146
.toList(),
147+
chainType: widget.chainType,
147148
onSelectToAddress: widget.onSelectToAddress),
148149
ContactsWidget(
149150
contacts: myPkidContacts,
150151
onSelectToAddress: widget.onSelectToAddress,
151152
onDeleteContact: _onDeleteContact,
152153
onEditContact: _openEditContactOverlay,
154+
chainType: widget.chainType,
153155
canEditAndDelete: true,
154156
),
155157
],

app/lib/widgets/wallets/add_edit_contact.dart

Lines changed: 18 additions & 11 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,31 +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-
final contacts = widget.contacts.where((c) => c.address == contactAddress);
90-
if (edit && contactAddress != widget.address && contacts.isNotEmpty) {
91-
addressError = 'Address is used in another contact';
92-
return false;
93-
} else if (!edit && contacts.isNotEmpty) {
94-
addressError =
95-
'Address exists in another ${contacts.first.type.name} contact';
9694

95+
final isDuplicate = allContacts.any((c) => c.address == contactAddress && (!edit || (edit && widget.address != contactAddress)));
96+
97+
if (isDuplicate) {
98+
addressError = 'Address is used in another contact';
9799
return false;
98100
}
101+
99102
if (_selectedChainType == ChainType.TFChain &&
100103
contactAddress.length != 48) {
101104
addressError = 'Address length should be 48 characters';
102105
return false;
103106
}
107+
104108
if (_selectedChainType == ChainType.Stellar &&
105109
!isValidStellarAddress(contactAddress)) {
106110
addressError = 'Invaild Stellar address';
107111
return false;
108112
}
113+
109114
return true;
110115
}
111116

@@ -126,10 +131,12 @@ class _AddEditContactState extends State<AddEditContact> {
126131
Icons.error, DialogType.Error);
127132
return;
128133
}
129-
if (chainType == widget.chainType) {
130-
widget.onAddContact!(PkidContact(
131-
name: contactName, address: contactAddress, type: chainType));
132-
}
134+
widget.onAddContact?.call(PkidContact(
135+
name: contactName,
136+
address: contactAddress,
137+
type: chainType,
138+
));
139+
133140
if (!context.mounted) return;
134141
Navigator.pop(context);
135142
}

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)