HealthKitStateModel.swift 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import Combine
  2. import SwiftUI
  3. extension AppleHealthKit {
  4. final class StateModel: BaseStateModel<Provider> {
  5. @Injected() var healthKitManager: HealthKitManager!
  6. @Published var useAppleHealth = false
  7. @Published var needShowInformationTextForSetPermissions = false
  8. override func subscribe() {
  9. useAppleHealth = settingsManager.settings.useAppleHealth
  10. needShowInformationTextForSetPermissions = healthKitManager.areAllowAllPermissions
  11. subscribeSetting(\.useAppleHealth, on: $useAppleHealth) {
  12. useAppleHealth = $0
  13. } didSet: { [weak self] value in
  14. guard let self = self else { return }
  15. guard value else {
  16. self.needShowInformationTextForSetPermissions = false
  17. return
  18. }
  19. self.healthKitManager.requestPermission { _, error in
  20. guard error == nil else {
  21. return
  22. }
  23. self.healthKitManager.enableBackgroundDelivery()
  24. self.healthKitManager.createObserver()
  25. DispatchQueue.main.async {
  26. self.needShowInformationTextForSetPermissions = !self.healthKitManager.areAllowAllPermissions
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }