Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions app/lib/screens/wallets/contacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class _ContactsScreenState extends State<ContactsScreen> {

_loadFavouriteContacts() async {
myPkidContacts = await getPkidContacts();
myPkidContacts =
myPkidContacts.where((c) => c.type == widget.chainType).toList();
setState(() {});
}

Expand All @@ -60,6 +58,7 @@ class _ContactsScreenState extends State<ContactsScreen> {
onAddContact: _onAddContact,
chainType: widget.chainType,
contacts: [...myPkidContacts, ...myWalletContacts],
getAllContacts: () => [...myPkidContacts, ...myWalletContacts],
));
}

Expand Down Expand Up @@ -141,15 +140,18 @@ class _ContactsScreenState extends State<ContactsScreen> {
children: [
ContactsWidget(
contacts: myWalletContacts
.where(
(c) => c.address != widget.currentWalletAddress && c.type == widget.chainType)
.where((c) =>
c.address != widget.currentWalletAddress &&
c.type == widget.chainType)
.toList(),
chainType: widget.chainType,
onSelectToAddress: widget.onSelectToAddress),
ContactsWidget(
contacts: myPkidContacts,
onSelectToAddress: widget.onSelectToAddress,
onDeleteContact: _onDeleteContact,
onEditContact: _openEditContactOverlay,
chainType: widget.chainType,
canEditAndDelete: true,
),
],
Expand Down
29 changes: 18 additions & 11 deletions app/lib/widgets/wallets/add_edit_contact.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AddEditContact extends StatefulWidget {
this.name = '',
this.address = '',
this.onEditContact,
this.getAllContacts,
});

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

@override
State<StatefulWidget> createState() {
Expand Down Expand Up @@ -81,31 +83,34 @@ class _AddEditContactState extends State<AddEditContact> {

bool _validateAddress(String contactAddress, {bool edit = false}) {
addressError = null;
final allContacts = widget.getAllContacts != null
? widget.getAllContacts!()
: widget.contacts;

if (contactAddress.isEmpty) {
addressError = "Address can't be empty";
return false;
}
final contacts = widget.contacts.where((c) => c.address == contactAddress);
if (edit && contactAddress != widget.address && contacts.isNotEmpty) {
addressError = 'Address is used in another contact';
return false;
} else if (!edit && contacts.isNotEmpty) {
addressError =
'Address exists in another ${contacts.first.type.name} contact';

final isDuplicate = allContacts.any((c) => c.address == contactAddress && (!edit || (edit && widget.address != contactAddress)));

if (isDuplicate) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we are in the edit flow, and the address is not changed? it will be misleading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be fixed.

addressError = 'Address is used in another contact';
return false;
}

if (_selectedChainType == ChainType.TFChain &&
contactAddress.length != 48) {
addressError = 'Address length should be 48 characters';
return false;
}

if (_selectedChainType == ChainType.Stellar &&
!isValidStellarAddress(contactAddress)) {
addressError = 'Invaild Stellar address';
return false;
}

return true;
}

Expand All @@ -126,10 +131,12 @@ class _AddEditContactState extends State<AddEditContact> {
Icons.error, DialogType.Error);
return;
}
if (chainType == widget.chainType) {
widget.onAddContact!(PkidContact(
name: contactName, address: contactAddress, type: chainType));
}
widget.onAddContact?.call(PkidContact(
name: contactName,
address: contactAddress,
type: chainType,
));

if (!context.mounted) return;
Navigator.pop(context);
}
Expand Down
9 changes: 6 additions & 3 deletions app/lib/widgets/wallets/contacts_widget.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:threebotlogin/models/contact.dart';
import 'package:threebotlogin/models/wallet.dart';
import 'package:threebotlogin/widgets/wallets/contact_card.dart';

class ContactsWidget extends StatelessWidget {
Expand All @@ -9,19 +10,21 @@ class ContactsWidget extends StatelessWidget {
required this.onSelectToAddress,
this.onDeleteContact,
this.onEditContact,
required this.chainType,
this.canEditAndDelete = false,
});

final List<PkidContact> contacts;
final void Function(String address) onSelectToAddress;
final bool canEditAndDelete;
final void Function(String name)? onDeleteContact;
final void Function(String oldName, String oldAddress)? onEditContact;
final ChainType chainType;

@override
Widget build(BuildContext context) {
final filteredContacts = contacts.where((c) => c.type == chainType).toList();
Widget content;
if (contacts.isEmpty) {
if (filteredContacts.isEmpty) {
content = Center(
child: Text(
'No contacts yet.',
Expand All @@ -33,7 +36,7 @@ class ContactsWidget extends StatelessWidget {
);
} else {
content = ListView(children: [
for (final contact in contacts)
for (final contact in filteredContacts)
InkWell(
onTap: () {
onSelectToAddress(contact.address);
Expand Down