Skip to content

Commit 3d6a31a

Browse files
authored
Read and write requestids for datapoints with HealthKit (#419)
This prepares for supporting point-by-point syncing of data from HealthKit. In particular 1) HealthKit data sources now provide a requestid for the datapoints they generate 2) We always read requestid from the server (now they are always sent) This will start writing new IDs immediately, and in the future means we can use the IDs to sync when we have multiple datapoints per day. Testing: * [ ] Deleted some existing data points, synced with apple heath, and checked they got requestids as expected * [ ] Checked the app can still update existing points via apple health * [ ] Confirmed data points still load without crashing
1 parent aa9950f commit 3d6a31a

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

BeeSwift/DataPoint.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Foundation
44
import SwiftyJSON
55

66
protocol DataPoint {
7+
var requestid: String { get }
78
var daystamp: String { get }
89
var value: NSNumber { get }
910
var comment: String { get }
@@ -12,6 +13,7 @@ protocol DataPoint {
1213
/// A data point received from the server. This will have had an ID allocated
1314
struct ExistingDataPoint : DataPoint {
1415
let id: String
16+
let requestid: String
1517
let daystamp: String
1618
let value: NSNumber
1719
let comment: String
@@ -23,6 +25,7 @@ struct ExistingDataPoint : DataPoint {
2325
daystamp = json["daystamp"].stringValue
2426
value = json["value"].numberValue
2527
comment = json["comment"].stringValue
28+
requestid = json["requestid"].stringValue
2629
}
2730

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

3336
/// A data point we have created locally (e.g. from user input, or HealthKit)
3437
struct NewDataPoint : DataPoint {
38+
let requestid: String
3539
let daystamp: String
3640
let value: NSNumber
3741
let comment: String

BeeSwift/Goal.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,6 @@ class Goal {
316316
var isLinkedToHealthKit: Bool {
317317
return self.autodata == "apple"
318318
}
319-
320-
func minuteStamp() -> String {
321-
let formatter = DateFormatter()
322-
formatter.dateFormat = "YYYYMMddHHmm"
323-
return formatter.string(from: Date())
324-
}
325319

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

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

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

BeeSwift/HeathKit/CategoryHealthKitMetric.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ class CategoryHealthKitMetric : HealthKitMetric {
6565
healthStore.execute(query)
6666
})
6767

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

7273
internal func dayStampFromDayOffset(dayOffset : Int, deadline : Int) throws -> String {

BeeSwift/HeathKit/QuantityHealthKitMetric.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ class QuantityHealthKitMetric : HealthKitMetric {
148148
formatter.dateFormat = "yyyyMMdd"
149149
let daystamp = formatter.string(from: datapointDate)
150150

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

154155
return results

0 commit comments

Comments
 (0)