NotificationSettingsView.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // NotificationSettingsView.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 2/3/21.
  6. // Copyright © 2021 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. import LoopKit
  10. import LoopKitUI
  11. import HealthKit
  12. struct NotificationSettingsView: View {
  13. var dateFormatter: DateFormatter
  14. @Binding var expirationReminderDefault: Int
  15. @State private var showingHourPicker: Bool = false
  16. var scheduledReminderDate: Date?
  17. var allowedScheduledReminderDates: [Date]?
  18. var lowReservoirReminderValue: Int
  19. var onSaveScheduledExpirationReminder: ((_ selectedDate: Date?, _ completion: @escaping (_ error: Error?) -> Void) -> Void)?
  20. var onSaveLowReservoirReminder: ((_ selectedValue: Int, _ completion: @escaping (_ error: Error?) -> Void) -> Void)?
  21. var insulinQuantityFormatter = QuantityFormatter(for: .internationalUnit())
  22. var body: some View {
  23. RoundedCardScrollView {
  24. RoundedCard(
  25. title: LocalizedString("Omnipod Reminders", comment: "Title for omnipod reminders section"),
  26. footer: LocalizedString("The app configures a reminder on the pod to notify you in advance of Pod expiration. Set the number of hours advance notice you would like to configure when pairing a new Pod.", comment: "Footer text for omnipod reminders section")
  27. ) {
  28. ExpirationReminderPickerView(expirationReminderDefault: $expirationReminderDefault)
  29. }
  30. if let allowedDates = allowedScheduledReminderDates {
  31. RoundedCard(
  32. footer: LocalizedString("This is a reminder that you scheduled when you paired your current Pod.", comment: "Footer text for scheduled reminder area"))
  33. {
  34. Text("Scheduled Reminder")
  35. Divider()
  36. scheduledReminderRow(scheduledDate: scheduledReminderDate, allowedDates: allowedDates)
  37. }
  38. }
  39. RoundedCard(footer: LocalizedString("The App notifies you when the amount of insulin in the Pod reaches this level.", comment: "Footer text for low reservoir value row")) {
  40. lowReservoirValueRow
  41. }
  42. RoundedCard<EmptyView>(
  43. title: LocalizedString("Critical Alerts", comment: "Title for critical alerts description"),
  44. footer: LocalizedString("The reminders above will not sound if your device is in Silent or Do Not Disturb mode.\n\nThere are other critical Pod alerts and alarms that will sound even if you device is set to Silent or Do Not Disturb mode.", comment: "Description text for critical alerts")
  45. )
  46. }
  47. .navigationBarTitle(LocalizedString("Notification Settings", comment: "navigation title for notification settings"))
  48. }
  49. @State private var scheduleReminderDateEditViewIsShown: Bool = false
  50. private func scheduledReminderRow(scheduledDate: Date?, allowedDates: [Date]) -> some View {
  51. Group {
  52. if let scheduledDate = scheduledDate, scheduledDate <= Date() {
  53. scheduledReminderRowContents(disclosure: false)
  54. } else {
  55. NavigationLink(
  56. destination: ScheduledExpirationReminderEditView(
  57. scheduledExpirationReminderDate: scheduledDate,
  58. allowedDates: allowedDates,
  59. dateFormatter: dateFormatter,
  60. onSave: onSaveScheduledExpirationReminder,
  61. onFinish: { scheduleReminderDateEditViewIsShown = false }),
  62. isActive: $scheduleReminderDateEditViewIsShown)
  63. {
  64. scheduledReminderRowContents(disclosure: true)
  65. }
  66. }
  67. }
  68. }
  69. private func scheduledReminderRowContents(disclosure: Bool) -> some View {
  70. RoundedCardValueRow(
  71. label: LocalizedString("Time", comment: "Label for scheduled reminder value row"),
  72. value: scheduledReminderDateString(scheduledReminderDate),
  73. highlightValue: false,
  74. disclosure: disclosure
  75. )
  76. }
  77. private func scheduledReminderDateString(_ scheduledDate: Date?) -> String {
  78. if let scheduledDate = scheduledDate {
  79. return dateFormatter.string(from: scheduledDate)
  80. } else {
  81. return LocalizedString("No Reminder", comment: "Value text for no expiration reminder")
  82. }
  83. }
  84. @State private var lowReservoirReminderEditViewIsShown: Bool = false
  85. var lowReservoirValueRow: some View {
  86. NavigationLink(
  87. destination: LowReservoirReminderEditView(
  88. lowReservoirReminderValue: lowReservoirReminderValue,
  89. insulinQuantityFormatter: insulinQuantityFormatter,
  90. onSave: onSaveLowReservoirReminder,
  91. onFinish: { lowReservoirReminderEditViewIsShown = false }),
  92. isActive: $lowReservoirReminderEditViewIsShown)
  93. {
  94. RoundedCardValueRow(
  95. label: LocalizedString("Low Reservoir Reminder", comment: "Label for low reservoir reminder row"),
  96. value: insulinQuantityFormatter.string(from: HKQuantity(unit: .internationalUnit(), doubleValue: Double(lowReservoirReminderValue)), for: .internationalUnit()) ?? "",
  97. highlightValue: false,
  98. disclosure: true)
  99. }
  100. }
  101. }
  102. struct NotificationSettingsView_Previews: PreviewProvider {
  103. static var previews: some View {
  104. return Group {
  105. NavigationView {
  106. NotificationSettingsView(dateFormatter: DateFormatter(), expirationReminderDefault: .constant(2), scheduledReminderDate: Date(), allowedScheduledReminderDates: [Date()], lowReservoirReminderValue: 20)
  107. .previewDevice(PreviewDevice(rawValue:"iPod touch (7th generation)"))
  108. .previewDisplayName("iPod touch (7th generation)")
  109. }
  110. NavigationView {
  111. NotificationSettingsView(dateFormatter: DateFormatter(), expirationReminderDefault: .constant(2), scheduledReminderDate: Date(), allowedScheduledReminderDates: [Date()], lowReservoirReminderValue: 20)
  112. .colorScheme(.dark)
  113. .previewDevice(PreviewDevice(rawValue: "iPhone XS Max"))
  114. .previewDisplayName("iPhone XS Max - Dark")
  115. }
  116. }
  117. }
  118. }