Skip to content

Commit

Permalink
Revert "Remove MockWebService dependent code"
Browse files Browse the repository at this point in the history
This reverts commit e13e849.
  • Loading branch information
nriedman committed Apr 4, 2024
1 parent e13e849 commit 364a777
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ENGAGEHF/ENGAGEHFStandard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import SpeziAccount
import SpeziFirebaseAccountStorage
import SpeziFirestore
import SpeziHealthKit
import SpeziMockWebService
import SpeziOnboarding
import SpeziQuestionnaire
import SwiftUI
Expand All @@ -30,6 +31,7 @@ actor ENGAGEHFStandard: Standard, EnvironmentAccessible, HealthKitConstraint, On
Firestore.firestore().collection("users")
}

@Dependency var mockWebService: MockWebService?
@Dependency var accountStorage: FirestoreAccountStorage?

@AccountReference var account: Account
Expand Down Expand Up @@ -66,6 +68,14 @@ actor ENGAGEHFStandard: Standard, EnvironmentAccessible, HealthKitConstraint, On


func add(sample: HKSample) async {
if let mockWebService {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
let jsonRepresentation = (try? String(data: encoder.encode(sample.resource), encoding: .utf8)) ?? ""
try? await mockWebService.upload(path: "healthkit/\(sample.uuid.uuidString)", body: jsonRepresentation)
return
}

do {
try await healthKitDocument(id: sample.id).setData(from: sample.resource)
} catch {
Expand All @@ -74,6 +84,11 @@ actor ENGAGEHFStandard: Standard, EnvironmentAccessible, HealthKitConstraint, On
}

func remove(sample: HKDeletedObject) async {
if let mockWebService {
try? await mockWebService.remove(path: "healthkit/\(sample.uuid.uuidString)")
return
}

do {
try await healthKitDocument(id: sample.uuid).delete()
} catch {
Expand All @@ -84,6 +99,12 @@ actor ENGAGEHFStandard: Standard, EnvironmentAccessible, HealthKitConstraint, On
func add(response: ModelsR4.QuestionnaireResponse) async {
let id = response.identifier?.value?.value?.string ?? UUID().uuidString

if let mockWebService {
let jsonRepresentation = (try? String(data: JSONEncoder().encode(response), encoding: .utf8)) ?? ""
try? await mockWebService.upload(path: "questionnaireResponse/\(id)", body: jsonRepresentation)
return
}

do {
try await userDocumentReference
.collection("QuestionnaireResponse") // Add all HealthKit sources in a /QuestionnaireResponse collection.
Expand Down

0 comments on commit 364a777

Please sign in to comment.