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

Add unified way to get workouts + convert Activity Types to name + isTracked flag #25

Merged
merged 26 commits into from
Feb 26, 2019

Conversation

EJohnF
Copy link
Contributor

@EJohnF EJohnF commented Mar 14, 2018

No description provided.

@EJohnF EJohnF mentioned this pull request Mar 26, 2018
@0livare
Copy link

0livare commented Aug 5, 2018

Hi @EJohnF, I'm relatively unfamiliar with how integrations work with react native, but I would love to test what you've put together here! Would it be possible for you to provide a little documentation around what I would need to do to successfully get the workout information in JS?

@EJohnF
Copy link
Contributor Author

EJohnF commented Nov 15, 2018

Sorry, I just missed that there was a comment. I'm planning to add documentation in near future as well as sync with master branch here

@iMaciah
Copy link

iMaciah commented Dec 10, 2018

Any news of committing to the master the reading of workouts? Anyway we could help?

@mazenchami
Copy link

@EJohnF any update on the documentation and merging with master?

@EJohnF
Copy link
Contributor Author

EJohnF commented Dec 23, 2018

Mostly sync now. @terrillo what do you think about changing default units to the metric system?

@mazenchami
Copy link

@terrillo if it helps, i went ahead and copied this PR and tested it on my device and it worked as desired! i still think @EJohnF needs to update docs for what the options and return values are for getSamples()...

@joaovpmamede
Copy link

Any plans on adding this to master?

@jplandry908
Copy link

jplandry908 commented Feb 3, 2019

@EJohnF I have been trying to get Activity data and came across this today. I pulled it down to my device (npm install rn-apple-healthkit@EJohnF/rn-apple-healthkit#observer) and am able to successfully pull Activity data using 'getSamples'. Great Job! Your efforts are VERY much appreciated.

However, I do have a question you may be able to assist with. The Activity data is returning the ID number in the 'activityName' field (maybe it should be called activityNameId instead?). Is there an easy way to convert this ID to the Activity Name? I personally like that it provides the ID (since that is what I will store in my database), but I'm trying to figure out the name/id mappings to display the Name itself to the user.

I appreciate any assistance you can provide. And thanks again for your efforts on this! 👍

@EJohnF
Copy link
Contributor Author

EJohnF commented Feb 5, 2019

@jplandry908
there is a simple way to do so, but it's inside native code
NSString *type = [RCTAppleHealthKit stringForHKWorkoutActivityType:[sample workoutActivityType]];

we did it like a name firslty, but then our customer required to send as Id. That's why this confusion in the code, sorry :/

rename it to activityNameId

@EJohnF
Copy link
Contributor Author

EJohnF commented Feb 5, 2019

@joaovpmamede not so far, it has some breaking changes - like using metric system as default - so, I don't want to merge without approve from owner

@mazenchami added docs

@mazenchami
Copy link

@EJohnF thanks for adding the docs. I feel like to get a quicker approval from the owner, if its possible to add a 'switch' to metric? i.e. unless a flag is sent to the getSamples() function it'll always return imperial units...does that make sense?

@EJohnF
Copy link
Contributor Author

EJohnF commented Feb 5, 2019

it isn't only about getSamples, but also for get/set Height/Weight. So, not that straight forward

@JulianKingman
Copy link

JulianKingman commented Feb 8, 2019

@EJohnF this is brilliant, it's exactly what I was hoping for for the next major version release. I just wish I had noticed this PR before I started working on #65

I've got some overlap on the observer queries (the main thing I'm working on); it looks like you haven't implemented the background observer yet, did you have a plan for that? My plan, as of now, is to write information received to AsyncStorage, so it can be retrieved when the app comes back to the foreground (AsyncStorage can also be accessed and used by react-native-background-fetch, to allow operation completely in the background).

Edit: for units, I think we should allow setting them in the init function, something like:

AppleHealthKit.initHealthKit({
  options: {
    // use imperial; keep as default for backward compatibility?
    units: "imperial",
    // use metric
    units: "metric",
    // default to metric, but use distance mile
    units: {default: "metric", distance: "mile"},
  }
});

It's a bit out of scope for this particular PR, though.

@EJohnF
Copy link
Contributor Author

EJohnF commented Feb 8, 2019

@JulianKingman No, didn't plan to work with async storage now. For background - setObserver function allow to listen changes in a given type and execute JS code when new data comes. What to do with it - on user.

@JulianKingman
Copy link

@EJohnF I tested it just to make sure, the code in the listener won't run in the background at all, so there's no way to get data from the background observer query. This is why I suggest using AsyncStorage. Another way would be to use a native storage that can be exposed to the app, so there's no risk of namespace conflict.

As far as I know, the only way for JS code to run in the background, is to use something like react-native-background-fetch. For that to work, you need to make the data accessible. As you pointed out, you can't access healthkit data through background-fetch, so you need to store it somewhere that can be accessed by background-fetch. There may be a way to pass a callback function into the setObserver() function, which could then be run, but I'm not sure how that would be done...

You can test if for yourself here: https://github.com/JulianKingman/rn-healthkit-observer-test
I test with HeartRate, but you could change it to steps. In theory, the event listener should create a local notification, but it won't because it doesn't run in the background.

Make sense?

@jplandry908
Copy link

@jplandry908
there is a simple way to do so, but it's inside native code
NSString *type = [RCTAppleHealthKit stringForHKWorkoutActivityType:[sample workoutActivityType]];

we did it like a name firslty, but then our customer required to send as Id. That's why this confusion in the code, sorry :/

rename it to activityNameId

Thank you! I was able to successfully get the name using your sample code. It worked perfectly!

Any objection to just returning both (the name and ID) just in case anyone else needs the name too? I would prefer to use your code 'as-is' and not make any customizations to avoid update issues.

I appreciate your efforts! 👍

@JulianKingman
Copy link

@EJohnF I’m testing writing to an encrypted local file from the background observer, it works quite well, and the data can be accessed while in the background by background fetch

@EJohnF
Copy link
Contributor Author

EJohnF commented Feb 26, 2019

@jplandry908 it would be nice if you can test the new version. I made as you suggested - keep id and name

@EJohnF EJohnF requested a review from terrillo February 26, 2019 19:15
@EJohnF EJohnF merged commit 715a0eb into lucaspbordignon:master Feb 26, 2019
@jplandry908
Copy link

@jplandry908 it would be nice if you can test the new version. I made as you suggested - keep id and name

Hi @EJohnF. I tested your code and it appears to be working great. I can now see the ID and NAME returned and no longer need to use my customization. Thank you!

@jplandry908
Copy link

Related to a unified way to get workout data, has anyone been able to figure out how to return GPS data for workouts that support them (i.e. RUNS)?

@jplandry908
Copy link

Hi @EJohnF (or anyone). I'm having difficulty pulling in route/GPS data for workouts that have this information (runs, walks, etc) and was wondering if you are interested in a small side project. I'm willing to pay you for your troubles.

https://developer.apple.com/documentation/healthkit/workouts_and_activity_rings/reading_route_data?language=objc

It doesn't look too difficult (probably only a few hours of work), but I just don't know how ObjC. :)

@EJohnF
Copy link
Contributor Author

EJohnF commented Apr 29, 2019

@jplandry908 sorry for the late response, I'm quite busy right now so that not sure that can help you in near future

@jplandry908
Copy link

@EJohnF No worries. We were able to find someone to assist. I appreciate the follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants