Skip to content

Commit

Permalink
Merge pull request #251 from Abion47/master
Browse files Browse the repository at this point in the history
Refactors all usages of Iterable to use List
  • Loading branch information
lukasgit authored Sep 21, 2021
2 parents 87acd94 + 49cc9f8 commit 5babc50
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 80 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ If you do not request user permission or have it granted, the application will f
import 'package:contacts_service/contacts_service.dart';
// Get all contacts on device
Iterable<Contact> contacts = await ContactsService.getContacts();
List<Contact> contacts = await ContactsService.getContacts();
// Get all contacts without thumbnail (faster)
Iterable<Contact> contacts = await ContactsService.getContacts(withThumbnails: false);
List<Contact> contacts = await ContactsService.getContacts(withThumbnails: false);
// Android only: Get thumbnail for an avatar afterwards (only necessary if `withThumbnails: false` is used)
Uint8List avatar = await ContactsService.getAvatar(contact);
// Get contacts matching a string
Iterable<Contact> johns = await ContactsService.getContacts(query : "john");
List<Contact> johns = await ContactsService.getContacts(query : "john");
// Add a contact
// The contact must have a firstName / lastName to be successfully added
Expand Down Expand Up @@ -86,13 +86,13 @@ String displayName, givenName, middleName, prefix, suffix, familyName;
String company, jobTitle;
// Email addresses
Iterable<Item> emails = [];
List<Item> emails = [];
// Phone numbers
Iterable<Item> phones = [];
List<Item> phones = [];
// Post addresses
Iterable<PostalAddress> postalAddresses = [];
List<PostalAddress> postalAddresses = [];
// Contact avatar/thumbnail
Uint8List avatar;
Expand Down
90 changes: 44 additions & 46 deletions example/lib/contacts_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ class _ContactListPageState extends State<ContactListPage> {
Future<void> refreshContacts() async {
// Load without thumbnails initially.
var contacts = (await ContactsService.getContacts(
withThumbnails: false, iOSLocalizedLabels: iOSLocalizedLabels))
.toList();
withThumbnails: false, iOSLocalizedLabels: iOSLocalizedLabels));
// var contacts = (await ContactsService.getContactsForPhone("8554964652"))
// .toList();
// ;
setState(() {
_contacts = contacts;
});
Expand All @@ -41,7 +40,6 @@ class _ContactListPageState extends State<ContactListPage> {

void updateContact() async {
Contact ninja = _contacts
.toList()
.firstWhere((contact) => contact.familyName.startsWith("Ninja"));
ninja.avatar = null;
await ContactsService.updateContact(ninja);
Expand All @@ -51,7 +49,7 @@ class _ContactListPageState extends State<ContactListPage> {

_openContactForm() async {
try {
var contact = await ContactsService.openContactForm(
var _ = await ContactsService.openContactForm(
iOSLocalizedLabels: iOSLocalizedLabels);
refreshContacts();
} on FormOperationException catch (e) {
Expand Down Expand Up @@ -234,43 +232,44 @@ class ContactDetailsPage extends StatelessWidget {
class AddressesTile extends StatelessWidget {
AddressesTile(this._addresses);

final Iterable<PostalAddress> _addresses;
final List<PostalAddress> _addresses;

Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ListTile(title: Text("Addresses")),
Column(
children: _addresses
.map((a) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
children: <Widget>[
ListTile(
title: Text("Street"),
trailing: Text(a.street ?? ""),
),
ListTile(
title: Text("Postcode"),
trailing: Text(a.postcode ?? ""),
),
ListTile(
title: Text("City"),
trailing: Text(a.city ?? ""),
),
ListTile(
title: Text("Region"),
trailing: Text(a.region ?? ""),
),
ListTile(
title: Text("Country"),
trailing: Text(a.country ?? ""),
),
],
children: [
for (var a in _addresses)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
children: <Widget>[
ListTile(
title: Text("Street"),
trailing: Text(a.street ?? ""),
),
))
.toList(),
ListTile(
title: Text("Postcode"),
trailing: Text(a.postcode ?? ""),
),
ListTile(
title: Text("City"),
trailing: Text(a.city ?? ""),
),
ListTile(
title: Text("Region"),
trailing: Text(a.region ?? ""),
),
ListTile(
title: Text("Country"),
trailing: Text(a.country ?? ""),
),
],
),
),
],
),
],
);
Expand All @@ -280,7 +279,7 @@ class AddressesTile extends StatelessWidget {
class ItemsTile extends StatelessWidget {
ItemsTile(this._title, this._items);

final Iterable<Item> _items;
final List<Item> _items;
final String _title;

@override
Expand All @@ -290,17 +289,16 @@ class ItemsTile extends StatelessWidget {
children: <Widget>[
ListTile(title: Text(_title)),
Column(
children: _items
.map(
(i) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ListTile(
title: Text(i.label ?? ""),
trailing: Text(i.value ?? ""),
),
children: [
for (var i in _items)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ListTile(
title: Text(i.label ?? ""),
trailing: Text(i.value ?? ""),
),
)
.toList(),
),
],
),
],
);
Expand All @@ -323,7 +321,7 @@ class _AddContactPageState extends State<AddContactPage> {
appBar: AppBar(
title: Text("Add a contact"),
actions: <Widget>[
FlatButton(
TextButton(
onPressed: () {
_formKey.currentState.save();
contact.postalAddresses = [address];
Expand Down
2 changes: 1 addition & 1 deletion example/lib/contacts_picker_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class _ContactPickerPageState extends State<ContactPickerPage> {
body: SafeArea(
child: Column(
children: <Widget>[
RaisedButton(
ElevatedButton(
child: const Text('Pick a contact'),
onPressed: _pickContact,
),
Expand Down
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ class _HomePageState extends State<HomePage> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
RaisedButton(
ElevatedButton(
child: const Text('Contacts list'),
onPressed: () => _askPermissions('/contactsList'),
),
RaisedButton(
ElevatedButton(
child: const Text('Native Contacts picker'),
onPressed: () => _askPermissions('/nativeContactPicker'),
),
Expand Down
35 changes: 18 additions & 17 deletions lib/contacts_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ContactsService {

/// Fetches all contacts, or when specified, the contacts with a name
/// matching [query]
static Future<Iterable<Contact>> getContacts(
static Future<List<Contact>> getContacts(
{String? query,
bool withThumbnails = true,
bool photoHighResolution = true,
Expand All @@ -29,18 +29,18 @@ class ContactsService {
'iOSLocalizedLabels': iOSLocalizedLabels,
'androidLocalizedLabels': androidLocalizedLabels,
});
return contacts.map((m) => Contact.fromMap(m));
return contacts.map((m) => Contact.fromMap(m)).toList();
}

/// Fetches all contacts, or when specified, the contacts with the phone
/// matching [phone]
static Future<Iterable<Contact>> getContactsForPhone(String? phone,
static Future<List<Contact>> getContactsForPhone(String? phone,
{bool withThumbnails = true,
bool photoHighResolution = true,
bool orderByGivenName = true,
bool iOSLocalizedLabels = true,
bool androidLocalizedLabels = true}) async {
if (phone == null || phone.isEmpty) return Iterable.empty();
if (phone == null || phone.isEmpty) return List.empty();

Iterable contacts =
await _channel.invokeMethod('getContactsForPhone', <String, dynamic>{
Expand All @@ -51,19 +51,19 @@ class ContactsService {
'iOSLocalizedLabels': iOSLocalizedLabels,
'androidLocalizedLabels': androidLocalizedLabels,
});
return contacts.map((m) => Contact.fromMap(m));
return contacts.map((m) => Contact.fromMap(m)).toList();
}

/// Fetches all contacts, or when specified, the contacts with the email
/// matching [email]
/// Works only on iOS
static Future<Iterable<Contact>> getContactsForEmail(String email,
static Future<List<Contact>> getContactsForEmail(String email,
{bool withThumbnails = true,
bool photoHighResolution = true,
bool orderByGivenName = true,
bool iOSLocalizedLabels = true,
bool androidLocalizedLabels = true}) async {
Iterable contacts =
List contacts =
await _channel.invokeMethod('getContactsForEmail', <String, dynamic>{
'email': email,
'withThumbnails': withThumbnails,
Expand All @@ -72,7 +72,7 @@ class ContactsService {
'iOSLocalizedLabels': iOSLocalizedLabels,
'androidLocalizedLabels': androidLocalizedLabels,
});
return contacts.map((m) => Contact.fromMap(m));
return contacts.map((m) => Contact.fromMap(m)).toList();
}

/// Loads the avatar for the given contact and returns it. If the user does
Expand Down Expand Up @@ -132,9 +132,9 @@ class ContactsService {
'androidLocalizedLabels': androidLocalizedLabels,
});
// result contains either :
// - an Iterable of contacts containing 0 or 1 contact
// - an List of contacts containing 0 or 1 contact
// - a FormOperationErrorCode value
if (result is Iterable) {
if (result is List) {
if (result.isEmpty) {
return null;
}
Expand Down Expand Up @@ -209,9 +209,9 @@ class Contact {
jobTitle;
String? androidAccountTypeRaw, androidAccountName;
AndroidAccountType? androidAccountType;
Iterable<Item>? emails = [];
Iterable<Item>? phones = [];
Iterable<PostalAddress>? postalAddresses = [];
List<Item>? emails = [];
List<Item>? phones = [];
List<PostalAddress>? postalAddresses = [];
Uint8List? avatar;
DateTime? birthday;

Expand All @@ -234,10 +234,11 @@ class Contact {
androidAccountTypeRaw = m["androidAccountType"];
androidAccountType = accountTypeFromString(androidAccountTypeRaw);
androidAccountName = m["androidAccountName"];
emails = (m["emails"] as Iterable?)?.map((m) => Item.fromMap(m));
phones = (m["phones"] as Iterable?)?.map((m) => Item.fromMap(m));
postalAddresses = (m["postalAddresses"] as Iterable?)
?.map((m) => PostalAddress.fromMap(m));
emails = (m["emails"] as List?)?.map((m) => Item.fromMap(m)).toList();
phones = (m["phones"] as List?)?.map((m) => Item.fromMap(m)).toList();
postalAddresses = (m["postalAddresses"] as List?)
?.map((m) => PostalAddress.fromMap(m))
.toList();
avatar = m["avatar"];
try {
birthday = m["birthday"] != null ? DateTime.parse(m["birthday"]) : null;
Expand Down
16 changes: 8 additions & 8 deletions test/contacts_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ void main() {
final contacts = await ContactsService.getContacts();
expect(contacts.length, 2);
expect(contacts, everyElement(isInstanceOf<Contact>()));
expect(contacts.toList()[0].givenName, 'givenName1');
expect(contacts.toList()[1].postalAddresses!.toList()[0].label, 'label');
expect(contacts.toList()[1].emails!.toList()[0].label, 'label');
expect(contacts.toList()[1].birthday, DateTime(1994, 2, 1));
expect(contacts[0].givenName, 'givenName1');
expect(contacts[1].postalAddresses![0].label, 'label');
expect(contacts[1].emails![0].label, 'label');
expect(contacts[1].birthday, DateTime(1994, 2, 1));
});

test('should get avatar for contact identifiers', () async {
Expand Down Expand Up @@ -79,8 +79,8 @@ void main() {
test('returns contacts if phone number supplied', () async {
final contacts = await ContactsService.getContactsForPhone('1234567890');
expect(contacts.length, equals(2));
expect(contacts.toList()[0].givenName, 'givenName1');
expect(contacts.toList()[1].givenName, 'givenName2');
expect(contacts[0].givenName, 'givenName1');
expect(contacts[1].givenName, 'givenName2');
});
});

Expand All @@ -91,8 +91,8 @@ void main() {
'abc@example.net',
);
expect(contacts.length, equals(2));
expect(contacts.toList()[0].givenName, 'givenName1');
expect(contacts.toList()[1].givenName, 'givenName2');
expect(contacts[0].givenName, 'givenName1');
expect(contacts[1].givenName, 'givenName2');
});
});

Expand Down

0 comments on commit 5babc50

Please sign in to comment.