ExpirationReminderPickerView.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // ExpirationReminderPickerView.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 5/17/21.
  6. // Copyright © 2021 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. import LoopKit
  10. import LoopKitUI
  11. import HealthKit
  12. struct ExpirationReminderPickerView: View {
  13. static let expirationReminderHoursAllowed = 0...24
  14. var expirationReminderDefault: Binding<Int>
  15. var collapsible: Bool = true
  16. @State var showingHourPicker: Bool = false
  17. var expirationDefaultFormatter = QuantityFormatter(for: .hour())
  18. var expirationDefaultString: String {
  19. return expirationReminderHourString(expirationReminderDefault.wrappedValue)
  20. }
  21. var body: some View {
  22. VStack {
  23. HStack {
  24. Text(LocalizedString("Expiration Reminder Default", comment: "Label text for expiration reminder default row"))
  25. Spacer()
  26. if collapsible {
  27. Button(expirationDefaultString) {
  28. withAnimation {
  29. showingHourPicker.toggle()
  30. }
  31. }
  32. } else {
  33. Text(expirationDefaultString)
  34. }
  35. }
  36. if showingHourPicker {
  37. ResizeablePicker(selection: expirationReminderDefault,
  38. data: Array(Self.expirationReminderHoursAllowed),
  39. formatter: { expirationReminderHourString($0) })
  40. }
  41. }
  42. }
  43. private func expirationReminderHourString(_ value: Int) -> String {
  44. if value > 0 {
  45. return expirationDefaultFormatter.string(from: HKQuantity(unit: .hour(), doubleValue: Double(value)), for: .hour())!
  46. } else {
  47. return LocalizedString("No Reminder", comment: "Value text for no expiration reminder")
  48. }
  49. }
  50. }
  51. struct ExpirationReminderPickerView_Previews: PreviewProvider {
  52. static var previews: some View {
  53. ExpirationReminderPickerView(expirationReminderDefault: .constant(2))
  54. }
  55. }