So I realize that this idea has been done before, but I want to make an app that displays my weight based on the information shared by the Health app on my iPhone.
I already use an app called LoseIt! that does this, however I don't love how the app tracks your weight. It shows the average of the past ten days and it sets all the milestones based on your last weigh-in. LoseIt! does a lot of other stuff as well and I generally like the app, however, it's deeply flawed in my opinion.
Anyone who has carefully weighed themselves knows that weight changes throughout the day and the week and this doesn't reflect the true weight that you want to measure. My weight can vary as much as 5 pounds in either direction throughout the week.
So when I get a notification from LoseIt that I hit my five-pound weight loss goal, I know that's total BS. That irks me. I would rather they used the average real weight to display these motivational rewards.
When I look at my real weight based on LoseIt! I see an average that spans two weekends (10 days). I know that my weight measurements are generally higher on the weekends so this measurement is artificially elevated. Using seven days would be better since that accurately catches the entire week to grab each significant change without biasing things toward the weekend or showing averages that seem to change based on how much of the weekends are captured in those ten days.
This is a real quibble LOL.
It's an opportunity to build some time for myself that is customized exactly the way I want it to be. This is a great reason to learn how to build iOS apps. Why not create the exact app that you want?
Maybe this app will appear on the App Store one day, but that's far from necessary.
I'm envisioning a sort of LoseIt! clone that behaves exactly the way I want it to behave. It will calculate weight correctly and perhaps provide a simple goal achievement or calorie counter interface. We'll see.
To give you an idea of what I do now to solve this problem, I'll show you my current process to get an idea of where I am coming from.
Today, I'm using the data analysis programming language R to process the XML file that you can export from your Health App on iOS. I simply export the XML file from the Health App to Dropbox and then download that file to my R project. Then I can read that in and extract my weigh-ins.
library(tidyverse)
library(xml2)
xml_data <- read_xml("export.xml")
The file that Apple provides gives you a schema that you can use to decode the XML file. I'm familiar enough with XML, but I'm certainly no expert when it comes to navigating XML in R. I enlisted the help of AI to pull together this basic script to read my measurements.
records <- xml_find_all(
xml_data
, ".//Record[
@type='HKQuantityTypeIdentifierBodyMass'
]"
)
This extracts the important XML nodes and puts them into that records
object. All I needed to do next was use xml_attr
to extract the attribute values I was interested in.
record_dates <- xml_attr(records, "startDate")
record_values <- xml_attr(records, "value")
Now that I have two vectors with the data I need, I can use these to create a dataset.
d <- data.frame(
date = as_date(ymd_hms(record_dates))
, weight = as.numeric(record_values)
, stringsAsFactors = FALSE
)
Next, I count how many days have passed for each weight measurement so I can figure out how I'm doing this week vs last week.
d <- d %>%
mutate(
days_past = as.numeric(
today() - date
)
, week = days_past < 7
, past_week = days_past >= 7
& days_past < 14
) %>%
arrange(desc(date))
Now, I can find out what my average weight was in the past seven days and compare that to what my average weight was in the last seven-day period. For example, I can use this to figure out my current seven-day weight:
d %>%
filter(week) %>%
summarise(avg = mean(weight)) %>%
as.numeric() %>%
round(1)
To see the previous week, I can filter on past_week
.
This solves my problem, more or less. But, it's a cumbersome process and requires me to manually move files around and open a code editor to get what is ultimately a simple piece of information.
What I want to do with the app is follow the same process as above with the HealthKit framework. The idea is that I can open the app and immediately know how I'm doing compared to the week before without all this manual work.
I realize this is a simple thing, but it's great to know that you can have a way of creating a completely tailored experience for yourself with iOS.