SetupCompleteView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // SetupCompleteView.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 3/2/21.
  6. // Copyright © 2021 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. import LoopKitUI
  10. struct SetupCompleteView: View {
  11. @Environment(\.verticalSizeClass) var verticalSizeClass
  12. @Environment(\.appName) private var appName
  13. private var onSaveScheduledExpirationReminder: ((_ selectedDate: Date?, _ completion: @escaping (_ error: Error?) -> Void) -> Void)?
  14. private var didFinish: () -> Void
  15. private var didRequestDeactivation: () -> Void
  16. private var dateFormatter: DateFormatter
  17. @State private var scheduledReminderDate: Date?
  18. @State private var scheduleReminderDateEditViewIsShown: Bool = false
  19. var allowedDates: [Date]
  20. init(scheduledReminderDate: Date?, dateFormatter: DateFormatter, allowedDates: [Date], onSaveScheduledExpirationReminder: ((_ selectedDate: Date?, _ completion: @escaping (_ error: Error?) -> Void) -> Void)?, didFinish: @escaping () -> Void, didRequestDeactivation: @escaping () -> Void)
  21. {
  22. self._scheduledReminderDate = State(initialValue: scheduledReminderDate)
  23. self.dateFormatter = dateFormatter
  24. self.allowedDates = allowedDates
  25. self.onSaveScheduledExpirationReminder = onSaveScheduledExpirationReminder
  26. self.didFinish = didFinish
  27. self.didRequestDeactivation = didRequestDeactivation
  28. }
  29. var body: some View {
  30. GuidePage(content: {
  31. VStack {
  32. LeadingImage("Pod")
  33. Text(String(format: LocalizedString("Your Pod is ready for use.\n\n%1$@ will remind you to change your pod before it expires. You can change this to a time convenient for you.", comment: "Format string for instructions for setup complete view. (1: app name)"), appName))
  34. .fixedSize(horizontal: false, vertical: true)
  35. Divider()
  36. VStack(alignment: .leading) {
  37. Text(LocalizedString("Scheduled Reminder", comment: "Scheduled reminder card title on SetupCompleteView"))
  38. Divider()
  39. NavigationLink(
  40. destination: ScheduledExpirationReminderEditView(
  41. scheduledExpirationReminderDate: scheduledReminderDate,
  42. allowedDates: allowedDates,
  43. dateFormatter: dateFormatter,
  44. onSave: { (newDate, completion) in
  45. onSaveScheduledExpirationReminder?(newDate) { (error) in
  46. if error == nil {
  47. scheduledReminderDate = newDate
  48. }
  49. completion(error)
  50. }
  51. },
  52. onFinish: { scheduleReminderDateEditViewIsShown = false }),
  53. isActive: $scheduleReminderDateEditViewIsShown)
  54. {
  55. RoundedCardValueRow(
  56. label: LocalizedString("Time", comment: "Label for expiration reminder row"),
  57. value: scheduledReminderDateString(scheduledReminderDate),
  58. highlightValue: false
  59. )
  60. }
  61. }
  62. }
  63. .padding(.bottom, 8)
  64. .accessibility(sortPriority: 1)
  65. }) {
  66. Button(action: {
  67. didFinish()
  68. }) {
  69. Text(LocalizedString("Finish Setup", comment: "Action button title to continue at Setup Complete"))
  70. .actionButtonStyle(.primary)
  71. }
  72. .padding()
  73. .background(Color(UIColor.systemBackground))
  74. .zIndex(1)
  75. }
  76. .animation(.default)
  77. .navigationBarTitle(LocalizedString("Setup Complete", comment: "Title of SetupCompleteView"), displayMode: .automatic)
  78. }
  79. private func scheduledReminderDateString(_ scheduledDate: Date?) -> String {
  80. if let scheduledDate = scheduledDate {
  81. return dateFormatter.string(from: scheduledDate)
  82. } else {
  83. return LocalizedString("No Reminder", comment: "Value text for no expiration reminder")
  84. }
  85. }
  86. }
  87. struct SetupCompleteView_Previews: PreviewProvider {
  88. static var previews: some View {
  89. SetupCompleteView(
  90. scheduledReminderDate: Date(),
  91. dateFormatter: DateFormatter(),
  92. allowedDates: [Date()],
  93. onSaveScheduledExpirationReminder: { (date, completion) in
  94. },
  95. didFinish: {
  96. },
  97. didRequestDeactivation: {
  98. }
  99. )
  100. }
  101. }