Skip to content

Commit ea60bd1

Browse files
authored
chore: cleanup contact workers (#226)
1 parent 7c66082 commit ea60bd1

File tree

6 files changed

+234
-155
lines changed

6 files changed

+234
-155
lines changed

packages/contacts/models/contact.android.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class Contact extends ContactCommon {
250250

251251
// Add/Update Names
252252
ops.add(
253-
helper
253+
ContactHelper.android
254254
.getContactBuilder(id, android.provider.ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
255255
.withValue(android.provider.ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, this.name.displayname)
256256
.withValue(android.provider.ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, this.name.given)
@@ -272,7 +272,7 @@ export class Contact extends ContactCommon {
272272
var nativePhoneType = ContactHelper.android.getNativePhoneType(item.label);
273273

274274
ops.add(
275-
helper
275+
ContactHelper.android
276276
.getRawContactBuilder(rawId, android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
277277
.withValue(TYPE, new java.lang.Integer(nativePhoneType))
278278
.withValue(LABEL, nativePhoneType ? '' : item.label)
@@ -286,7 +286,7 @@ export class Contact extends ContactCommon {
286286
var nativeEmailType = ContactHelper.android.getNativeEmailType(item.label);
287287

288288
ops.add(
289-
helper
289+
ContactHelper.android
290290
.getRawContactBuilder(rawId, android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
291291
.withValue(TYPE, new java.lang.Integer(nativeEmailType))
292292
.withValue(LABEL, nativeEmailType ? '' : item.label)
@@ -300,7 +300,7 @@ export class Contact extends ContactCommon {
300300
var nativeAddressType = ContactHelper.android.getNativeAddressType(item.label);
301301

302302
ops.add(
303-
helper
303+
ContactHelper.android
304304
.getRawContactBuilder(rawId, android.provider.ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
305305
.withValue(TYPE, new java.lang.Integer(nativeAddressType))
306306
.withValue(LABEL, nativeAddressType ? '' : item.label)
@@ -322,7 +322,7 @@ export class Contact extends ContactCommon {
322322
var nativeWebsiteType = ContactHelper.android.getNativeWebsiteType(item.label);
323323

324324
ops.add(
325-
helper
325+
ContactHelper.android
326326
.getRawContactBuilder(rawId, android.provider.ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
327327
.withValue(TYPE, new java.lang.Integer(nativeWebsiteType))
328328
.withValue(LABEL, nativeWebsiteType ? '' : item.label)
@@ -334,7 +334,7 @@ export class Contact extends ContactCommon {
334334
// Add Organization
335335
var nativeOrgType = ContactHelper.android.getNativeOrgType(this.organization.type);
336336
ops.add(
337-
helper
337+
ContactHelper.android
338338
.getRawContactBuilder(rawId, android.provider.ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
339339
.withValue(TYPE, new java.lang.Integer(nativeOrgType))
340340
.withValue(LABEL, nativeOrgType ? '' : this.organization.type)
Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
require('@nativescript/core/globals')
2-
var Contact = require("./models/contact").Contact;
3-
var helper = require("./helper").ContactHelper;
1+
import '@nativescript/core/globals';
2+
3+
import { Contact } from './models/contact';
4+
import { ContactHelper } from './helper';
45

56
/* pass debug messages to main thread since web workers do not have console access */
67
// function console_log(msg) { postMessage({ type: 'debug', message: msg }); }
78
// function console_dump(msg) { postMessage({ type: 'dump', message: msg }); }
89

910
self.onmessage = function (event) {
10-
try {
11-
var c = helper.android.getContext().getContentResolver().query(android.provider.ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
11+
try {
12+
const c = ContactHelper.android.getContext().getContentResolver().query(android.provider.ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
1213

13-
if(c.getCount() > 0){
14-
var cts = [];
15-
while(c.moveToNext()){
16-
try {
17-
var contactModel = new Contact();
18-
contactModel.initializeFromNative(c,event.data.contactFields);
19-
cts.push(contactModel);
20-
} catch(e) {
21-
// console_log("Error processing contact");
22-
}
23-
}
24-
c.close();
25-
postMessage({ type: 'result', message: { data: cts, response: "fetch" }});
26-
} else {
27-
c.close();
28-
postMessage({ type: 'result', message: { data: null, response: "fetch" }})
29-
}
30-
} catch (e) {
31-
// console.log('error', e)
32-
postMessage({ type: 'result', message: e });
33-
}
34-
}
14+
if (c.getCount() > 0) {
15+
let cts = [];
16+
while (c.moveToNext()) {
17+
try {
18+
const contactModel = new Contact();
19+
contactModel.initializeFromNative(c, event.data.contactFields);
20+
cts.push(contactModel);
21+
} catch (e) {
22+
// console_log("Error processing contact");
23+
}
24+
}
25+
c.close();
26+
postMessage({ type: 'result', message: { data: cts, response: 'fetch' } });
27+
} else {
28+
c.close();
29+
postMessage({ type: 'result', message: { data: null, response: 'fetch' } });
30+
}
31+
} catch (e) {
32+
// console.log('error', e)
33+
postMessage({ type: 'result', message: e });
34+
}
35+
};
Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,87 @@
1-
require('@nativescript/core/globals')
2-
var Contact = require("./models/contact").Contact;
1+
import '@nativescript/core/globals';
2+
3+
import { Contact } from './models/contact';
34

45
/* pass debug messages to main thread since web workers do not have console access */
56
// function console_log(msg) { postMessage({ type: 'debug', message: msg }); }
67
// function console_dump(msg) { postMessage({ type: 'dump', message: msg }); }
78

89
self.onmessage = function (event) {
9-
contactFields = event.data.contactFields || [
10-
'name',
11-
'organization',
12-
'nickname',
13-
'notes',
14-
'photo',
15-
'urls',
16-
'phoneNumbers',
17-
'emailAddresses',
18-
'postalAddresses',
19-
];
20-
21-
var keysToFetch = []; // All Properties that we are using in the Model
22-
if (contactFields.indexOf('name') > -1) {
23-
keysToFetch.push(
24-
"givenName", "familyName", "middleName", "namePrefix", "nameSuffix",
25-
"phoneticGivenName", "phoneticMiddleName", "phoneticFamilyName"
26-
);
27-
}
28-
29-
if (contactFields.indexOf('organization') > -1) { keysToFetch.push("jobTitle", "departmentName", "organizationName"); }
30-
if (contactFields.indexOf('nickname') > -1) { keysToFetch.push("nickname"); }
31-
if (contactFields.indexOf('notes') > -1) { keysToFetch.push("note"); }
32-
if (contactFields.indexOf('photo') > -1) { keysToFetch.push("imageData", "imageDataAvailable"); }
33-
if (contactFields.indexOf('phoneNumbers') > -1) { keysToFetch.push("phoneNumbers"); }
34-
if (contactFields.indexOf('emailAddresses') > -1) { keysToFetch.push("emailAddresses"); }
35-
if (contactFields.indexOf('postalAddresses') > -1) { keysToFetch.push("postalAddresses"); }
36-
if (contactFields.indexOf('urlAddresses') > -1) { keysToFetch.push("urlAddresses"); }
37-
38-
var store = new CNContactStore(),
39-
error,
40-
fetch = CNContactFetchRequest.alloc().initWithKeysToFetch(keysToFetch),
41-
cts = [],
42-
nativeMutableArray = NSMutableArray.alloc().init();
43-
44-
fetch.unifyResults = true;
45-
fetch.predicate = null;
46-
47-
store.enumerateContactsWithFetchRequestErrorUsingBlock(fetch, error, function(c,s){
48-
nativeMutableArray.addObject(c);
49-
var contactModel = new Contact();
50-
contactModel.initializeFromNative(c,contactFields);
51-
cts.push(contactModel);
52-
});
53-
54-
if(error) { postMessage({ type: 'error', message: error }); }
55-
if(cts.length > 0) { postMessage({ type: 'result', message: { data: cts, response: "fetch" }}); }
56-
else { postMessage({ type: 'result', message: { data: null, response: "fetch" }}); }
57-
}
10+
// prettier-ignore
11+
const contactFields = event.data.contactFields || [
12+
'name',
13+
'organization',
14+
'nickname',
15+
'notes',
16+
'photo',
17+
'urls',
18+
'phoneNumbers',
19+
'emailAddresses',
20+
'postalAddresses'
21+
];
22+
23+
const keysToFetch = []; // All Properties that we are using in the Model
24+
if (contactFields.indexOf('name') > -1) {
25+
// prettier-ignore
26+
keysToFetch.push(
27+
'givenName',
28+
'familyName',
29+
'middleName',
30+
'namePrefix',
31+
'nameSuffix',
32+
'phoneticGivenName',
33+
'phoneticMiddleName',
34+
'phoneticFamilyName'
35+
);
36+
}
37+
38+
if (contactFields.indexOf('organization') > -1) {
39+
keysToFetch.push('jobTitle', 'departmentName', 'organizationName');
40+
}
41+
if (contactFields.indexOf('nickname') > -1) {
42+
keysToFetch.push('nickname');
43+
}
44+
if (contactFields.indexOf('notes') > -1) {
45+
keysToFetch.push('note');
46+
}
47+
if (contactFields.indexOf('photo') > -1) {
48+
keysToFetch.push('imageData', 'imageDataAvailable');
49+
}
50+
if (contactFields.indexOf('phoneNumbers') > -1) {
51+
keysToFetch.push('phoneNumbers');
52+
}
53+
if (contactFields.indexOf('emailAddresses') > -1) {
54+
keysToFetch.push('emailAddresses');
55+
}
56+
if (contactFields.indexOf('postalAddresses') > -1) {
57+
keysToFetch.push('postalAddresses');
58+
}
59+
if (contactFields.indexOf('urlAddresses') > -1) {
60+
keysToFetch.push('urlAddresses');
61+
}
62+
63+
const store = new CNContactStore();
64+
let error: any;
65+
let fetch = CNContactFetchRequest.alloc().initWithKeysToFetch(keysToFetch);
66+
let cts = [];
67+
let nativeMutableArray = NSMutableArray.alloc().init();
68+
69+
fetch.unifyResults = true;
70+
fetch.predicate = null;
71+
72+
store.enumerateContactsWithFetchRequestErrorUsingBlock(fetch, error, function (c, s) {
73+
nativeMutableArray.addObject(c);
74+
const contactModel = new Contact();
75+
contactModel.initializeFromNative(c, contactFields);
76+
cts.push(contactModel);
77+
});
78+
79+
if (error) {
80+
postMessage({ type: 'error', message: error });
81+
}
82+
if (cts.length > 0) {
83+
postMessage({ type: 'result', message: { data: cts, response: 'fetch' } });
84+
} else {
85+
postMessage({ type: 'result', message: { data: null, response: 'fetch' } });
86+
}
87+
};
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
require('@nativescript/core/globals')
2-
var Contact = require("./models/contact").Contact;
3-
var helper = require("./helper").ContactHelper;
1+
import '@nativescript/core/globals';
2+
3+
import { Contact } from './models/contact';
4+
import { ContactHelper } from './helper';
45

56
/* pass debug messages to main thread since web workers do not have console access */
67
// function console_log(msg) { postMessage({ type: 'debug', message: msg }); }
78
// function console_dump(msg) { postMessage({ type: 'dump', message: msg }); }
89

910
self.onmessage = (event) => {
10-
try {
11-
var Contacts = android.provider.ContactsContract.Contacts,
12-
SELECTION = android.provider.ContactsContract.ContactNameColumns.DISPLAY_NAME_PRIMARY;
13-
var c = helper.android.getContext().getContentResolver().query(
14-
Contacts.CONTENT_URI,
15-
null,
16-
SELECTION + " like ?",
17-
["%" + event.data.searchPredicate + "%"],
18-
null
19-
);
11+
try {
12+
const Contacts = android.provider.ContactsContract.Contacts;
13+
const SELECTION = android.provider.ContactsContract.ContactNameColumns.DISPLAY_NAME_PRIMARY;
14+
15+
const c = ContactHelper.android
16+
.getContext()
17+
.getContentResolver()
18+
.query(Contacts.CONTENT_URI, null, SELECTION + ' like ?', ['%' + event.data.searchPredicate + '%'], null);
2019

21-
if(c.getCount() > 0){
22-
var cts = [];
23-
while(c.moveToNext()){
24-
var contactModel = new Contact();
25-
contactModel.initializeFromNative(c,event.data.contactFields);
26-
cts.push(contactModel);
27-
}
28-
c.close();
29-
postMessage({ type: 'result', message: { data: cts, response: "fetch" }});
30-
}
31-
else{
32-
c.close();
33-
postMessage({ type: 'result', message: { data: null, response: "fetch" }});
34-
}
35-
} catch (e) { postMessage({ type: 'result', message: e }); }
36-
}
20+
if (c.getCount() > 0) {
21+
const cts = [];
22+
while (c.moveToNext()) {
23+
const contactModel = new Contact();
24+
contactModel.initializeFromNative(c, event.data.contactFields);
25+
cts.push(contactModel);
26+
}
27+
c.close();
28+
postMessage({ type: 'result', message: { data: cts, response: 'fetch' } });
29+
} else {
30+
c.close();
31+
postMessage({ type: 'result', message: { data: null, response: 'fetch' } });
32+
}
33+
} catch (e) {
34+
postMessage({ type: 'result', message: e });
35+
}
36+
};

0 commit comments

Comments
 (0)