Skip to content

Commit

Permalink
Support extra permissions in Ask
Browse files Browse the repository at this point in the history
  • Loading branch information
andersio committed Sep 16, 2024
1 parent ff7913c commit 2d1a7c4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
20 changes: 12 additions & 8 deletions Sources/VitalHealthKit/HealthKit/Abstractions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ struct Predicates: @unchecked Sendable {
struct VitalHealthKitStore {
var isHealthDataAvailable: () -> Bool

var requestReadWriteAuthorization: ([VitalResource], [WritableVitalResource]) async throws -> Void
var requestReadWriteAuthorization: ([VitalResource], [WritableVitalResource], [HKObjectType], [HKSampleType]) async throws -> Void

var authorizationState: (VitalResource) -> AuthorizationState

var writeInput: (DataInput, Date, Date) async throws -> Void
Expand Down Expand Up @@ -243,21 +243,25 @@ extension VitalHealthKitStore {

return .init {
HKHealthStore.isHealthDataAvailable()
} requestReadWriteAuthorization: { readResources, writeResources in
let readTypes: [HKObjectType] = readResources
} requestReadWriteAuthorization: { readResources, writeResources, extraReadTypes, extraWriteTypes in
let readTypes = Set(
readResources
.map(toHealthKitTypes)
.flatMap(\.allObjectTypes)
).union(extraReadTypes)

let writeTypes: [HKSampleType] = writeResources
let writeTypes = Set(
writeResources
.map(\.toResource)
.map(toHealthKitTypes)
.flatMap(\.allObjectTypes)
.compactMap { $0 as? HKSampleType }
).union(extraWriteTypes)

if #available(iOS 15.0, *) {
try await store.requestAuthorization(toShare: Set(writeTypes), read: Set(readTypes))
try await store.requestAuthorization(toShare: writeTypes, read: readTypes)
} else {
try await store.__requestAuthorization(toShare: Set(writeTypes), read: Set(readTypes))
try await store.__requestAuthorization(toShare: writeTypes, read: readTypes)
}

} authorizationState: { resource in
Expand Down Expand Up @@ -291,7 +295,7 @@ extension VitalHealthKitStore {
static var debug: VitalHealthKitStore {
return .init {
return true
} requestReadWriteAuthorization: { _, _ in
} requestReadWriteAuthorization: { _, _, _, _ in
return
} authorizationState: { _ in
(true, [])
Expand Down
20 changes: 18 additions & 2 deletions Sources/VitalHealthKit/HealthKit/VitalHealthKitClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1092,17 +1092,33 @@ extension VitalHealthKitClient {
}
}

/// Ask for health data read and write permission from the user.
///
/// The interactive permission prompt is managed by the operating system (Apple iOS). Vital SDK cannot customize
/// its appearance or behaviour.
///
/// The prompt only appears for resources being asked for the first time. iOS typically ignores any subsequent attempts,
/// regardless of whether permission has been granted or denied during that first time. You will have to direct your users to
/// either the Settings ap or the Apple Health app for reviewing and updating the permissions.
///
/// - Parameter readPermissions: `VitalResource`s to request read permission for.
/// - Parameter writePermissions: `VitalResource`s to request write permission for.
/// - Parameter extraReadPermissions: Extra HealthKit object types whose read permissions should be requested in addition to the needs of Vital SDK.
/// - Parameter extraWritePermissions: Extra HealthKit sample types whose write permissions should be requested in addition to the needs of Vital SDK.
///
public func ask(
readPermissions readResources: [VitalResource],
writePermissions writeResource: [WritableVitalResource]
writePermissions writeResource: [WritableVitalResource],
extraReadPermissions: [HKObjectType] = [],
extraWritePermissions: [HKSampleType] = []
) async -> PermissionOutcome {

guard store.isHealthDataAvailable() else {
return .healthKitNotAvailable
}

do {
try await store.requestReadWriteAuthorization(readResources, writeResource)
try await store.requestReadWriteAuthorization(readResources, writeResource, extraReadPermissions, extraWritePermissions)

let state = authorizationState(store: store)
SyncProgressStore.shared.recordAsk(state.activeResources)
Expand Down

0 comments on commit 2d1a7c4

Please sign in to comment.