GuardrailConstrainedQuantityRangeView.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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(for: unit)
  32. return quantityFormatter.numberFormatter
  33. }()
  34. self.forceDisableAnimations = forceDisableAnimations
  35. }
  36. public var body: some View {
  37. HStack {
  38. lowerBoundView
  39. Text("–")
  40. .foregroundColor(Color(.secondaryLabel))
  41. upperBoundView
  42. }
  43. .animation(forceDisableAnimations || isEditing || !hasAppeared ? nil : .default)
  44. .onAppear { self.hasAppeared = true }
  45. }
  46. var lowerBoundView: some View {
  47. Group {
  48. if range != nil {
  49. GuardrailConstrainedQuantityView(
  50. value: range!.lowerBound,
  51. unit: unit,
  52. guardrail: guardrail,
  53. isEditing: isEditing,
  54. iconSpacing: 4,
  55. isUnitLabelVisible: false,
  56. forceDisableAnimations: forceDisableAnimations
  57. )
  58. } else {
  59. Text(LocalizedString("min", comment: "Placeholder for quantity range lower bound"))
  60. .foregroundColor(Color(.tertiaryLabel))
  61. }
  62. }
  63. }
  64. var upperBoundView: some View {
  65. Group {
  66. if range != nil {
  67. GuardrailConstrainedQuantityView(
  68. value: range!.upperBound,
  69. unit: unit,
  70. guardrail: guardrail,
  71. isEditing: isEditing,
  72. iconSpacing: 4,
  73. forceDisableAnimations: forceDisableAnimations
  74. )
  75. } else {
  76. HStack {
  77. Text(LocalizedString("max", comment: "Placeholder for quantity range upper bound"))
  78. .foregroundColor(Color(.tertiaryLabel))
  79. Text(unit.shortLocalizedUnitString())
  80. .foregroundColor(Color(.secondaryLabel))
  81. }
  82. }
  83. }
  84. }
  85. }