Skip to content

Commit

Permalink
CS1-119: iOS: Ability to Pause HealthKit Synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
andersio committed Feb 6, 2024
1 parent f99cacc commit faa7a4e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Examples/iOS/HealthKit Tab/HealthKitExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import VitalCore

struct HealthKitExample: View {
@State var permissions: [VitalResource: Bool] = [:]
@State var pauseSync = VitalHealthKitClient.shared.pauseSynchronization

var body: some View {
NavigationView {
Expand Down Expand Up @@ -36,6 +37,8 @@ struct HealthKitExample: View {
}
.buttonStyle(PlainButtonStyle())
}

Toggle(isOn: $pauseSync) { Text("Pause Synchronization") }

Button("Add water 1L") {
Task {
Expand Down Expand Up @@ -66,6 +69,9 @@ struct HealthKitExample: View {
}
)
}
.onChange(of: self.pauseSync) { pauseSync in
VitalHealthKitClient.shared.pauseSynchronization = pauseSync
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class VitalHealthKitStorage {
private let flag = "vital_anchor_"

private let initialSyncDone = "initial_sync_done"
private let pauseSync = "pause_sync"

private let storage: VitalBackStorage

Expand Down Expand Up @@ -43,6 +44,15 @@ class VitalHealthKitStorage {
let anchor = self.read(key: key)
return anchor?.vitalAnchors == nil && anchor?.date == nil && anchor?.anchor == nil
}

func shouldPauseSynchronization() -> Bool {
// Default is nil; so this evaluate to false
return storage.read(pauseSync) == Data([0x01])
}

func setPauseSynchronization(_ newValue: Bool) {
storage.store(Data([newValue ? 0x01 : 0x0]), pauseSync)
}

func store(entity: StoredAnchor) {
let anchorPrefix = anchorPrefix + entity.key
Expand Down
17 changes: 17 additions & 0 deletions Sources/VitalHealthKit/HealthKit/VitalHealthKitClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ extension VitalHealthKitClient {
private func sync(
payload: SyncPayload
) async {
guard self.pauseSynchronization == false else { return }

let configuration = await configuration.get()
let startDate: Date = .dateAgo(days: configuration.numberOfDaysToBackFill)
Expand Down Expand Up @@ -713,6 +714,22 @@ extension VitalHealthKitClient {
}
}

extension VitalHealthKitClient {
/// Pause all synchronization, both automatic syncs and any manual `syncData(_:)` calls.
public var pauseSynchronization: Bool {
get { self.storage.shouldPauseSynchronization() }
set {
let oldValue = self.storage.shouldPauseSynchronization()
self.storage.setPauseSynchronization(newValue)

// Auto-trigger an asynchronous sync when we un-pause.
if oldValue && !newValue {
self.syncData()
}
}
}
}

func transform(data: ProcessedResourceData, calendar: Calendar) -> ProcessedResourceData {
switch data {
case .summary(.activity):
Expand Down

0 comments on commit faa7a4e

Please sign in to comment.