GuardrailConstrainedQuantityRangeView.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // GuardrailConstrainedQuantityRangeView.swift
  3. // LoopKitUI
  4. //
  5. // Created by Michael Pangburn on 5/14/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. import HealthKit
  10. import LoopKit
  11. public struct GuardrailConstrainedQuantityRangeView: View {
  12. var range: ClosedRange<HKQuantity>?
  13. var unit: HKUnit
  14. var guardrail: Guardrail<HKQuantity>
  15. var isEditing: Bool
  16. var formatter: NumberFormatter
  17. var forceDisableAnimations: Bool
  18. @State var hasAppeared = false
  19. public init(
  20. range: ClosedRange<HKQuantity>?,
  21. unit: HKUnit,
  22. guardrail: Guardrail<HKQuantity>,
  23. isEditing: Bool,
  24. forceDisableAnimations: Bool = false
  25. ) {
  26. self.range = range
  27. self.unit = unit
  28. self.guardrail = guardrail
  29. self.isEditing = isEditing
  30. self.formatter = {
  31. let quantityFormatter = QuantityFormatter()
  32. quantityFormatter.setPreferredNumberFormatter(for: unit)
  33. return quantityFormatter.numberFormatter
  34. }()
  35. self.forceDisableAnimations = forceDisableAnimations
  36. }
  37. public var body: some View {
  38. HStack {
  39. lowerBoundView
  40. Text("–")
  41. .foregroundColor(Color(.secondaryLabel))
  42. upperBoundView
  43. }
  44. .animation(forceDisableAnimations || isEditing || !hasAppeared ? nil : .default)
  45. .onAppear { self.hasAppeared = true }
  46. }
  47. var lowerBoundView: some View {
  48. Group {
  49. if range != nil {
  50. GuardrailConstrainedQuantityView(
  51. value: range!.lowerBound,
  52. unit: unit,
  53. guardrail: guardrail,
  54. isEditing: isEditing,
  55. iconSpacing: 4,
  56. isUnitLabelVisible: false,
  57. forceDisableAnimations: forceDisableAnimations
  58. )
  59. } else {
  60. Text(LocalizedString("min", comment: "Placeholder for quantity range lower bound"))
  61. .foregroundColor(Color(.tertiaryLabel))
  62. }
  63. }
  64. }
  65. var upperBoundView: some View {
  66. Group {
  67. if range != nil {
  68. GuardrailConstrainedQuantityView(
  69. value: range!.upperBound,
  70. unit: unit,
  71. guardrail: guardrail,
  72. isEditing: isEditing,
  73. iconSpacing: 4,
  74. forceDisableAnimations: forceDisableAnimations
  75. )
  76. } else {
  77. HStack {
  78. Text(LocalizedString("max", comment: "Placeholder for quantity range upper bound"))
  79. .foregroundColor(Color(.tertiaryLabel))
  80. Text(unit.shortLocalizedUnitString())
  81. .foregroundColor(Color(.secondaryLabel))
  82. }
  83. }
  84. }
  85. }
  86. }