Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate the implementation for clinical records #104

Merged
merged 17 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
.DS_Store
node_modules
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
* `queryCorrelationType`
* `deleteSamples`

Read below about `CLINICAL_READ_PERMISSION` to use these
* `queryClinicalSampleType`
* `queryForClinicalRecordsFromSource`
* `queryForClinicalRecordsWithFHIRResourceType`

### Resources

* The official Apple documentation for [HealthKit can be found here](https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HealthKit_Framework/index.html#//apple_ref/doc/uid/TP40014707).
Expand All @@ -53,6 +58,16 @@ cordova plugin add com.telerik.plugins.healthkit --variable HEALTH_READ_PERMISSI
```
`HEALTH_READ_PERMISSION` and `HEALTH_WRITE_PERMISSION` are shown when your app asks for access to data in HealthKit.

If you would like to read clinical record data from the HealthKit store you will need to provide an extra variable during the plugin install. The `CLINICAL_READ_PERMISSION` can be set to include the ability to read FHIR resources. The value that is set here will be used in the `NSHealthClinicalHealthRecordsShareUsageDescription` key of your app's `info.plist` file. It will be shown when your app asks for clinical record data from HealthKit. Do not include the `CLINICAL_READ_PERMISSION` variable unless you really need access to the clinical record data otherwise Apple may reject your app.

The `Health Records` capability will be enabled if the `CLINICAL_READ_PERMISSION` is provided.

Here is an install example with `CLINICAL_READ_PERMISSION` -
```bash
cordova plugin add com.telerik.plugins.healthkit --variable HEALTH_READ_PERMISSION='App needs read access' --variable HEALTH_WRITE_PERMISSION='App needs write access' --variable CLINICAL_READ_PERMISSION='App needs read access' --save
```


#### Using PhoneGap Build?

```xml
Expand All @@ -76,7 +91,7 @@ PhoneGap Build has [recently migrated](https://blog.phonegap.com/phonegap-7-0-1-
<plugin name="com.telerik.plugins.healthkit" spec="^0.5.5" >
<variable name="HEALTH_READ_PERMISSION" value="App needs read access" />
<variable name="HEALTH_WRITE_PERMISSION" value="App needs write access" />
</plugin>
</plugin>
</platform>
```

81 changes: 81 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ <h1>Apache Cordova</h1>
'HKQuantityTypeIdentifierDietaryFatTotal'
];

// or any of these HKClinicalType for readTypes
// use these to access health records (read only)
// HKClinicalTypeIdentifierAllergyRecord
// HKClinicalTypeIdentifierConditionRecord
// HKClinicalTypeIdentifierImmunizationRecord
// HKClinicalTypeIdentifierLabResultRecord
// HKClinicalTypeIdentifierMedicationRecord
// HKClinicalTypeIdentifierProcedureRecord
// HKClinicalTypeIdentifierVitalSignRecord

window.plugins.healthkit.requestAuthorization(
{
readTypes: supportedTypes,
Expand Down Expand Up @@ -283,6 +293,77 @@ <h1>Apache Cordova</h1>
);
}

// used to query a specified clinical sample type
function queryClinicalSampleType() {
window.plugins.healthkit.queryClinicalSampleType(
{
'startDate': new Date(new Date().getTime() - 365 * 24 * 60 * 60 * 1000), // 365 days ago
'endDate': new Date(), // now
'sampleType': 'HKClinicalTypeIdentifierAllergyRecord',
// or any of these other HKClinicalType
// HKClinicalTypeIdentifierConditionRecord
// HKClinicalTypeIdentifierImmunizationRecord
// HKClinicalTypeIdentifierLabResultRecord
// HKClinicalTypeIdentifierMedicationRecord
// HKClinicalTypeIdentifierProcedureRecord
// HKClinicalTypeIdentifierVitalSignRecord
},
callback,
callback
);
}

// this is used to search for a specific FHIR resource type
// it uses predicateForClinicalRecordsWithFHIRResourceType (https://developer.apple.com/documentation/healthkit/hkquery/2999414-predicateforclinicalrecordswithf?language=objc)
// In most cases, there’s a one-to-one correspondance between the clinical record types and the FHIR resource types;
// therefore, most queries already return samples from a single FHIR resource type.
// However, queries for the HKClinicalTypeIdentifierMedicationRecord type can return records from the
// HKFHIRResourceTypeMedicationOrder, HKFHIRResourceTypeMedicationDispense, and HKFHIRResourceTypeMedicationStatement FHIR resource types.
// You can use this predicate to limit your query to one of these FHIR types.
function queryForClinicalRecordsWithFHIRResourceType() {
window.plugins.healthkit.queryForClinicalRecordsWithFHIRResourceType(
{
fhirResourceType: 'HKFHIRResourceTypeCondition',
sampleType: 'HKClinicalTypeIdentifierConditionRecord'
// or any of these other HKFHIRResourceType
// HKFHIRResourceTypeAllergyIntolerance',
// HKFHIRResourceTypeImmunization
// HKFHIRResourceTypeMedicationDispense
// HKFHIRResourceTypeMedicationOrder
// HKFHIRResourceTypeMedicationStatement
// HKFHIRResourceTypeObservation
// HKFHIRResourceTypeProcedure

// or any of these other HKClinicalType
// HKClinicalTypeIdentifierImmunizationRecord
// HKClinicalTypeIdentifierLabResultRecord
// HKClinicalTypeIdentifierMedicationRecord
// HKClinicalTypeIdentifierProcedureRecord
// HKClinicalTypeIdentifierVitalSignRecord
},
callback,
callback
);
}

// this is used to find a particular FHIR record
// it uses predicateForClinicalRecordsFromSource (https://developer.apple.com/documentation/healthkit/hkquery/2999413-predicateforclinicalrecordsfroms?language=objc)
function queryForClinicalRecordsFromSource() {
window.plugins.healthkit.queryForClinicalRecordsFromSource(
{
fhirResourceType: 'HKFHIRResourceTypeCondition',
sampleType: 'HKClinicalTypeIdentifierConditionRecord',
identifier: '42467', // the identifier of the FHIR resource
source: { // the provenance of this FHIR record
name: 'Sample Location C',
bundleIdentifier: 'com.apple.public.health.clinical.7D9AED77-AD3F-7801-5A65-59F537187C7D'
}
},
callback,
callback
);
}


// this is work in progress
function monitorSampleType() {
Expand Down
89 changes: 89 additions & 0 deletions package-lock.json

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

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@
"name": "cordova",
"version": ">=3.0.0"
}
]
}
],
"dependencies": {
"elementtree": "0.1.7",
"xcode": "^1.0.0"
}
}
10 changes: 10 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,18 @@
<true/>
</config-file>

<config-file target="*/Entitlements-Debug.plist" parent="com.apple.developer.healthkit.access">
<array/>
</config-file>

<config-file target="*/Entitlements-Release.plist" parent="com.apple.developer.healthkit">
<true/>
</config-file>

<config-file target="*/Entitlements-Release.plist" parent="com.apple.developer.healthkit.access">
<array/>
</config-file>

<header-file src="src/ios/WorkoutActivityConversion.h"/>
<source-file src="src/ios/WorkoutActivityConversion.m"/>
<header-file src="src/ios/HKHealthStore+AAPLExtensions.h"/>
Expand All @@ -69,6 +77,8 @@
<source-file src="src/ios/HealthKit.m"/>

<framework src="HealthKit.framework" weak="true" />

<hook type="after_plugin_install" src="scripts/after-plugin-install.js" />
</platform>

</plugin>
78 changes: 78 additions & 0 deletions scripts/after-plugin-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
module.exports = function (ctx) {
var isMeteorProject = ctx.opts.projectRoot.indexOf('/.meteor/') > -1;

if (ctx.cmdLine.indexOf('CLINICAL_READ_PERMISSION') < 0 && !isMeteorProject) {
console.log('CLINICAL_READ_PERMISSION was not provided');
return;
}

try {
var fs = require('fs'),
path = require('path'),
configXMLPath = path.join(ctx.opts.projectRoot, 'config.xml'),
et = require('elementtree'),
xcode = require('xcode'),
usageDescription;

if (isMeteorProject) {
var meteorProjectPath = ctx.opts.projectRoot.split('/.meteor/')[0];
var mobileConfigPath = path.join(meteorProjectPath, 'mobile-config.js');
var mobileConfigData = fs.readFileSync(mobileConfigPath, 'utf8');
var re = /CLINICAL_READ_PERMISSION?:\s*["|'](.*)['|"]/g;
var matches = re.exec(mobileConfigData);
if (matches && matches.length > 1) {
usageDescription = matches[1];
} else {
console.log('CLINICAL_READ_PERMISSION was not provided');
return;
}
} else {
usageDescription = ctx.cmdLine.split('CLINICAL_READ_PERMISSION=')[1].split('--')[0].trim();
}

console.log('*** Installing HealthKitClinicalRecords ***');
console.log('CLINICAL_READ_PERMISSION = ', usageDescription);

var configData = fs.readFileSync(configXMLPath).toString();
var etree = et.parse(configData);
var appName = etree.findtext('./name');
var srcPath = path.join(ctx.opts.projectRoot, 'plugins/com.telerik.plugins.healthkit/src/ios');
var projPath = path.join(ctx.opts.projectRoot, 'platforms/ios', appName + '.xcodeproj/project.pbxproj');
var xcodeProj = xcode.project(projPath);
xcodeProj.parseSync();

xcodeProj.addHeaderFile(path.join(srcPath, 'HealthKitClinicalRecords.h'));
xcodeProj.addSourceFile(path.join(srcPath, 'HealthKitClinicalRecords.m'));

fs.writeFileSync(projPath, xcodeProj.writeSync());

// enable health records
var tagPlatform = etree.findall('./platform[@name="ios"]');
if (tagPlatform.length > 0) {
// add CLINICAL_READ_PERMISSION text to config.xml
var tagEditConfig = et.Element('config-file', { target: '*-Info.plist', parent: 'NSHealthClinicalHealthRecordsShareUsageDescription' });
var tagString = et.Element('string');
tagString.text = usageDescription;
tagEditConfig.append(tagString);
tagPlatform[0].append(tagEditConfig);

// add Health Records to entitlements
['*Entitlements-Debug.plist', '*Entitlements-Release.plist'].forEach(function(fileName){
var healthRecordCapabilityConfig = et.Element('config-file', { target: fileName, parent: 'com.apple.developer.healthkit.access'});
var healthRecordArray = et.Element('array');
var healthRecordString = et.Element('string');
healthRecordString.text = 'health-records';
healthRecordArray.append(healthRecordString);
healthRecordCapabilityConfig.append(healthRecordArray);
tagPlatform[0].append(healthRecordCapabilityConfig);
});

configData = etree.write({ 'indent': 4 });
fs.writeFileSync(configXMLPath, configData);
}

console.log('*** DONE Installing HealthKitClinicalRecords ***');
} catch(e) {
console.log('healthkit after-plugin-install error, e: ', JSON.stringify(e, null, 2));
}
};
Loading