Binding.swift 766 B

123456789101112131415161718192021222324252627282930
  1. //
  2. // Binding.swift
  3. // LoopKitUI
  4. //
  5. // Created by Michael Pangburn on 5/13/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. import HealthKit
  10. extension Binding where Value == Double {
  11. func withUnit(_ unit: HKUnit) -> Binding<HKQuantity> {
  12. Binding<HKQuantity>(
  13. get: { HKQuantity(unit: unit, doubleValue: self.wrappedValue) },
  14. set: { self.wrappedValue = $0.doubleValue(for: unit) }
  15. )
  16. }
  17. }
  18. extension Binding where Value == HKQuantity {
  19. func doubleValue(for unit: HKUnit) -> Binding<Double> {
  20. Binding<Double>(
  21. get: { self.wrappedValue.doubleValue(for: unit) },
  22. set: { self.wrappedValue = HKQuantity(unit: unit, doubleValue: $0) }
  23. )
  24. }
  25. }