Skip to content

Commit

Permalink
Include ids when making apple health data points
Browse files Browse the repository at this point in the history
  • Loading branch information
theospears committed Nov 20, 2023
1 parent 2e2045a commit b40a99f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
4 changes: 4 additions & 0 deletions BeeSwift/DataPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Foundation
import SwiftyJSON

protocol DataPoint {
var requestid: String { get }
var daystamp: String { get }
var value: NSNumber { get }
var comment: String { get }
Expand All @@ -12,6 +13,7 @@ protocol DataPoint {
/// A data point received from the server. This will have had an ID allocated
struct ExistingDataPoint : DataPoint {
let id: String
let requestid: String
let daystamp: String
let value: NSNumber
let comment: String
Expand All @@ -23,6 +25,7 @@ struct ExistingDataPoint : DataPoint {
daystamp = json["daystamp"].stringValue
value = json["value"].numberValue
comment = json["comment"].stringValue
requestid = json["requestid"].stringValue
}

static func fromJSONArray(array: [JSON]) -> [ExistingDataPoint] {
Expand All @@ -32,6 +35,7 @@ struct ExistingDataPoint : DataPoint {

/// A data point we have created locally (e.g. from user input, or HealthKit)
struct NewDataPoint : DataPoint {
let requestid: String
let daystamp: String
let value: NSNumber
let comment: String
Expand Down
11 changes: 1 addition & 10 deletions BeeSwift/Goal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,6 @@ class Goal {
var isLinkedToHealthKit: Bool {
return self.autodata == "apple"
}

func minuteStamp() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "YYYYMMddHHmm"
return formatter.string(from: Date())
}

/// The daystamp corresponding to the day of the goal's creation, thus the first day we should add data points for.
var initDaystamp: String {
Expand Down Expand Up @@ -364,11 +358,9 @@ class Goal {
return
}
let daystamp = datapoint.daystamp
let requestId = "\(daystamp)-\(self.minuteStamp())"
let params = [
"value": "\(datapointValue)",
"comment": "Auto-updated via Apple Health",
"requestid": requestId
]
do {
let _ = try await ServiceLocator.requestManager.put(url: "api/v1/users/\(ServiceLocator.currentUserManager.username!)/goals/\(self.slug)/datapoints/\(datapoint.id).json", parameters: params)
Expand Down Expand Up @@ -474,8 +466,7 @@ class Goal {
return
}

let requestId = "\(newDataPoint.daystamp)-\(minuteStamp())"
let params = ["urtext": "\(newDataPoint.daystamp.suffix(2)) \(newDataPoint.value) \"\(newDataPoint.comment)\"", "requestid": requestId]
let params = ["urtext": "\(newDataPoint.daystamp.suffix(2)) \(newDataPoint.value) \"\(newDataPoint.comment)\"", "requestid": newDataPoint.requestid]

logger.notice("Creating new datapoint for \(self.id, privacy: .public) on \(newDataPoint.daystamp, privacy: .public): \(newDataPoint.value, privacy: .private)")

Expand Down
3 changes: 2 additions & 1 deletion BeeSwift/HeathKit/CategoryHealthKitMetric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ class CategoryHealthKitMetric : HealthKitMetric {
healthStore.execute(query)
})

let id = "apple-heath-" + daystamp
let datapointValue = self.hkDatapointValueForSamples(samples: samples, startOfDate: bounds.start)
return NewDataPoint(daystamp: daystamp, value: NSNumber(value: datapointValue), comment: "Auto-entered via Apple Health")
return NewDataPoint(requestid: id, daystamp: daystamp, value: NSNumber(value: datapointValue), comment: "Auto-entered via Apple Health")
}

internal func dayStampFromDayOffset(dayOffset : Int, deadline : Int) throws -> String {
Expand Down
3 changes: 2 additions & 1 deletion BeeSwift/HeathKit/QuantityHealthKitMetric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ class QuantityHealthKitMetric : HealthKitMetric {
formatter.dateFormat = "yyyyMMdd"
let daystamp = formatter.string(from: datapointDate)

results.append(NewDataPoint(daystamp: daystamp, value: NSNumber(value: datapointValue), comment: "Auto-entered via Apple Health"))
let id = "apple-health-" + daystamp
results.append(NewDataPoint(requestid: id, daystamp: daystamp, value: NSNumber(value: datapointValue), comment: "Auto-entered via Apple Health"))
}

return results
Expand Down

0 comments on commit b40a99f

Please sign in to comment.