Skip to content

Commit

Permalink
Merge pull request #5 from byrdsandbytes/develop
Browse files Browse the repository at this point in the history
Refactor and bump up to v.1.1.0
  • Loading branch information
Idrimi committed Jun 10, 2020
2 parents d291f41 + 57f2419 commit a62b76c
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 175 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ $RECYCLE.BIN/
log.txt
npm-debug.log*

android/build
android/.gradle
android/.settings
android/.classpath
android/.project

/.idea
/.ionic
/.sass-cache
/.sourcemaps
/.versions
/.vscode
/build/
/coverage
/dist
/node_modules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class CapContacts extends Plugin {
public static final String CONTACT_ID = "contactId";
public static final String EMAILS = "emails";
public static final String PHONE_NUMBERS = "phoneNumbers";
public static final String LOOKUP_KEY = "lookupKey";
public static final String DISPLAY_NAME = "displayName";
public static final String ORGANIZATION_NAME = "organizationName";
public static final String ORGANIZATION_ROLE = "organizationRole";
Expand All @@ -38,6 +37,7 @@ public void getPermissions(PluginCall call) {
requestPermissions(call);
} else {
JSObject result = new JSObject();
result.put("granted", true);
call.success(result);
}
}
Expand All @@ -47,10 +47,14 @@ protected void handleRequestPermissionsResult(int requestCode, String[] permissi
super.handleRequestPermissionsResult(requestCode, permissions, grantResults);

PluginCall savedCall = getSavedCall();
JSObject result = new JSObject();

if (!hasRequiredPermissions()) {
savedCall.error("Permissions not granted.");
result.put("granted", false);
savedCall.success(result);
} else {
savedCall.success();
result.put("granted", true);
savedCall.success(result);
}
}

Expand All @@ -67,7 +71,6 @@ public void getContacts(PluginCall call) {
JSObject jsContact = new JSObject();
String contactId = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Contacts._ID));
jsContact.put(CONTACT_ID, contactId);
jsContact.put(LOOKUP_KEY, dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)));
jsContact.put(DISPLAY_NAME, dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY)));

addOrganization(jsContact);
Expand Down Expand Up @@ -223,7 +226,7 @@ private void addPhoneNumbers(JSObject jsContact) {

@PluginMethod()
public void deleteContact(PluginCall call) {
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, call.getString(LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, call.getString(CONTACT_ID));
getContext().getContentResolver().delete(uri, null, null);

JSObject result = new JSObject();
Expand Down
16 changes: 0 additions & 16 deletions dist/esm/definitions.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion dist/esm/definitions.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/esm/definitions.js.map

This file was deleted.

2 changes: 0 additions & 2 deletions dist/esm/index.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions dist/esm/index.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/esm/index.js.map

This file was deleted.

16 changes: 0 additions & 16 deletions dist/esm/web.d.ts

This file was deleted.

41 changes: 0 additions & 41 deletions dist/esm/web.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/esm/web.js.map

This file was deleted.

18 changes: 5 additions & 13 deletions ios/Plugin/Contacts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import Contacts

class Contacts {
class func getContactFromCNContact() -> [CNContact] {
class func getContactFromCNContact() throws -> [CNContact] {

let contactStore = CNContactStore()
let keysToFetch = [
Expand All @@ -24,11 +24,8 @@ class Contacts {

//Get all the containers
var allContainers: [CNContainer] = []
do {
allContainers = try contactStore.containers(matching: nil)
} catch {
print("Error fetching containers")
}
allContainers = try contactStore.containers(matching: nil)


var results: [CNContact] = []

Expand All @@ -37,13 +34,8 @@ class Contacts {

let fetchPredicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier)

do {
let containerResults = try contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keysToFetch as! [CNKeyDescriptor])
results.append(contentsOf: containerResults)

} catch {
print("Error fetching results for container")
}
let containerResults = try contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keysToFetch as! [CNKeyDescriptor])
results.append(contentsOf: containerResults)
}

return results
Expand Down
5 changes: 1 addition & 4 deletions ios/Plugin/Permission.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import Foundation
import Contacts

class Permissions {
// MARK: Contacts


class func contactPermission(completionHandler: @escaping (_ accessGranted: Bool) -> Void) {
let contactStore = CNContactStore()
switch CNContactStore.authorizationStatus(for: .contacts) {
Expand All @@ -31,8 +30,6 @@ class Permissions {
}
}
}


}


3 changes: 1 addition & 2 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Define the plugin using the CAP_PLUGIN Macro, and
// each method the plugin supports using the CAP_PLUGIN_METHOD macro.
CAP_PLUGIN(CapContacts, "CapContacts",
CAP_PLUGIN_METHOD(echo, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(checkPermissions, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getPermissions, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getContacts, CAPPluginReturnPromise);
)
79 changes: 41 additions & 38 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Plugin.swift
//
//
//
// Created by Jonathan Gerber on 15.02.20.
// © Byrds & Bytes GmbH - Jonathan Gerber
Expand All @@ -11,58 +11,61 @@ import Capacitor

@objc(CapContacts)
public class CapContacts: CAPPlugin {

@objc func echo(_ call: CAPPluginCall) {
print("Echo was triggered in Swift")
let value = call.getString("value") ?? ""
call.success([
"allowed": value
])
}

@objc func getPermissions(_ call: CAPPluginCall) {
print("checkPermission was triggered in Swift")
Permissions.contactPermission { permission in
switch permission {
Permissions.contactPermission { granted in
switch granted {
case true:
call.success([
"allowed": true
"granted": true
])
default:
call.success([
"allowed": false
"granted": false
])
}
}
}

@objc func getContacts(_ call: CAPPluginCall) {
var contactsArray : [PluginResultData] = [];
let contacts = Contacts.getContactFromCNContact()
for contact in contacts {
var phoneNumbers: [String] = []
var emails: [String] = []
for number in contact.phoneNumbers {
let numberToAppend = number.value.stringValue
phoneNumbers.append(numberToAppend)
print(phoneNumbers)
}
for email in contact.emailAddresses {
let emailToAppend = email.value as String
emails.append(emailToAppend)
Permissions.contactPermission { granted in
if granted {
do {
let contacts = try Contacts.getContactFromCNContact()

for contact in contacts {
var phoneNumbers: [String] = []
var emails: [String] = []
for number in contact.phoneNumbers {
let numberToAppend = number.value.stringValue
phoneNumbers.append(numberToAppend)
print(phoneNumbers)
}
for email in contact.emailAddresses {
let emailToAppend = email.value as String
emails.append(emailToAppend)
}
let contactResult: PluginResultData = [
"contactId": contact.identifier,
"displayName": "\(contact.givenName) \(contact.familyName)",
"phoneNumbers": phoneNumbers,
"emails": emails
]
contactsArray.append(contactResult)
}
call.success([
"contacts": contactsArray
])
} catch let error as NSError {
call.error("Generic Error", error)
}
} else {
call.error("User denied access to contacts")
}
let contactResult: PluginResultData = [
"contactId": contact.identifier,
"lookupKey": contact.identifier,
"displayName": "\(contact.givenName) \(contact.familyName)",
"phoneNumbers": phoneNumbers,
"emails": emails
]
contactsArray.append(contactResult)
}
let data : PluginResultData = ["contacts": contactsArray]
call.success(data)

}

}

26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@byrds/capacitor-contacts",
"version": "1.0.2",
"version": "1.1.0",
"description": "A capacitor plugin to fetch contacts",
"main": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
Expand Down

0 comments on commit a62b76c

Please sign in to comment.