Skip to content

Commit

Permalink
Fix issue lukasgit#83: add parameter iOSLocalizedLabels to openContac…
Browse files Browse the repository at this point in the history
…tForm and openExistingContact
  • Loading branch information
sperochon committed Apr 23, 2020
1 parent 844399d commit 0afc663
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
35 changes: 22 additions & 13 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import 'package:permission_handler/permission_handler.dart';

void main() => runApp(ContactsExampleApp());

// iOS only: Localized labels language setting is equal to CFBundleDevelopmentRegion value (Info.plist) of the iOS project
// Set iOSLocalizedLabels=false if you always want english labels whatever is the CFBundleDevelopmentRegion value.
const iOSLocalizedLabels = false;

class ContactsExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -37,8 +41,9 @@ class _ContactListPageState extends State<ContactListPage> {
PermissionStatus permissionStatus = await _getContactPermission();
if (permissionStatus == PermissionStatus.granted) {
// Load without thumbnails initially.
var contacts =
(await ContactsService.getContacts(withThumbnails: false)).toList();
var contacts = (await ContactsService.getContacts(
withThumbnails: false, iOSLocalizedLabels: iOSLocalizedLabels))
.toList();
// var contacts = (await ContactsService.getContactsForPhone("8554964652"))
// .toList();
setState(() {
Expand Down Expand Up @@ -98,10 +103,11 @@ class _ContactListPageState extends State<ContactListPage> {

_openContactForm() async {
try {
var contact = await ContactsService.openContactForm();
var contact = await ContactsService.openContactForm(
iOSLocalizedLabels: iOSLocalizedLabels);
refreshContacts();
} on FormOperationException catch(e) {
switch(e.errorCode) {
} on FormOperationException catch (e) {
switch (e.errorCode) {
case FormOperationErrorCode.FORM_OPERATION_CANCELED:
case FormOperationErrorCode.FORM_COULD_NOT_BE_OPEN:
case FormOperationErrorCode.FORM_OPERATION_UNKNOWN_ERROR:
Expand Down Expand Up @@ -142,10 +148,10 @@ class _ContactListPageState extends State<ContactListPage> {
return ListTile(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) =>
ContactDetailsPage(
builder: (BuildContext context) => ContactDetailsPage(
c,
onContactDeviceSave: contactOnDeviceHasBeenUpdated,
onContactDeviceSave:
contactOnDeviceHasBeenUpdated,
)));
},
leading: (c.avatar != null && c.avatar.length > 0)
Expand All @@ -164,21 +170,23 @@ class _ContactListPageState extends State<ContactListPage> {

void contactOnDeviceHasBeenUpdated(Contact contact) {
this.setState(() {
var id = _contacts.indexWhere((c) => c.identifier == contact.identifier);
_contacts[id] = contact;
var id = _contacts.indexWhere((c) => c.identifier == contact.identifier);
_contacts[id] = contact;
});
}
}

class ContactDetailsPage extends StatelessWidget {
ContactDetailsPage(this._contact, { this.onContactDeviceSave });
ContactDetailsPage(this._contact, {this.onContactDeviceSave});

final Contact _contact;
final Function(Contact) onContactDeviceSave;

_openExistingContactOnDevice(BuildContext context) async {
try {
var contact = await ContactsService.openExistingContact(_contact);
var contact = await ContactsService.openExistingContact(_contact,
iOSLocalizedLabels: iOSLocalizedLabels);
print(contact.emails.first.label);
if (onContactDeviceSave != null) {
onContactDeviceSave(contact);
}
Expand Down Expand Up @@ -219,7 +227,8 @@ class ContactDetailsPage extends StatelessWidget {
),
),
IconButton(
icon: Icon(Icons.edit), onPressed: () => _openExistingContactOnDevice(context)),
icon: Icon(Icons.edit),
onPressed: () => _openExistingContactOnDevice(context)),
],
),
body: SafeArea(
Expand Down
11 changes: 8 additions & 3 deletions ios/Classes/SwiftContactsServicePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ContactsUI
@available(iOS 9.0, *)
public class SwiftContactsServicePlugin: NSObject, FlutterPlugin, CNContactViewControllerDelegate {
private var result: FlutterResult? = nil
private var localizedLabels: Bool = true
static let FORM_OPERATION_CANCELED: Int = 1
static let FORM_COULD_NOT_BE_OPEN: Int = 2

Expand Down Expand Up @@ -73,10 +74,14 @@ public class SwiftContactsServicePlugin: NSObject, FlutterPlugin, CNContactViewC
result(FlutterError(code: "", message: "Failed to update contact, make sure it has a valid identifier", details: nil))
}
case "openContactForm":
let arguments = call.arguments as! [String:Any]
localizedLabels = arguments["iOSLocalizedLabels"] as! Bool
self.result = result
_ = openContactForm()
case "openExistingContact":
let contact = call.arguments as! [String : Any]
let arguments = call.arguments as! [String : Any]
let contact = arguments["contact"] as! [String : Any]
localizedLabels = arguments["iOSLocalizedLabels"] as! Bool
self.result = result
_ = openExistingContact(contact: contact, result: result)
default:
Expand Down Expand Up @@ -230,7 +235,7 @@ public class SwiftContactsServicePlugin: NSObject, FlutterPlugin, CNContactViewC
viewController.dismiss(animated: true, completion: nil)
if let result = self.result {
if let contact = contact {
result(contactToDictionary(contact: contact))
result(contactToDictionary(contact: contact, localizedLabels: localizedLabels))
} else {
result(SwiftContactsServicePlugin.FORM_OPERATION_CANCELED)
}
Expand Down Expand Up @@ -444,7 +449,7 @@ public class SwiftContactsServicePlugin: NSObject, FlutterPlugin, CNContactViewC
return contact
}

func contactToDictionary(contact: CNContact, localizedLabels: Bool = true) -> [String:Any]{
func contactToDictionary(contact: CNContact, localizedLabels: Bool) -> [String:Any]{

var result = [String:Any]()

Expand Down
13 changes: 9 additions & 4 deletions lib/contacts_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,18 @@ class ContactsService {
static Future updateContact(Contact contact) =>
_channel.invokeMethod('updateContact', Contact._toMap(contact));

static Future<Contact> openContactForm() async {
dynamic result = await _channel.invokeMethod('openContactForm');
static Future<Contact> openContactForm({bool iOSLocalizedLabels = true}) async {
dynamic result = await _channel.invokeMethod('openContactForm',<String,dynamic>{
'iOSLocalizedLabels': iOSLocalizedLabels,
});
return _handleFormOperation(result);
}

static Future<Contact> openExistingContact(Contact contact) async {
dynamic result = await _channel.invokeMethod('openExistingContact', Contact._toMap(contact));
static Future<Contact> openExistingContact(Contact contact, {bool iOSLocalizedLabels = true}) async {
dynamic result = await _channel.invokeMethod('openExistingContact',<String,dynamic>{
'contact': Contact._toMap(contact),
'iOSLocalizedLabels': iOSLocalizedLabels,
}, );
return _handleFormOperation(result);
}

Expand Down

0 comments on commit 0afc663

Please sign in to comment.