PairPodView.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // PairPodView.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 2/5/20.
  6. // Copyright © 2021 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. import LoopKitUI
  10. struct PairPodView: View {
  11. @ObservedObject var viewModel: PairPodViewModel
  12. @State private var cancelModalIsPresented: Bool = false
  13. var body: some View {
  14. GuidePage(content: {
  15. VStack {
  16. LeadingImage("PodBottom")
  17. HStack {
  18. InstructionList(instructions: [
  19. LocalizedString("Fill a new pod with U-100 Insulin (leave clear Pod needle cap on). Listen for 2 beeps.", comment: "Label text for step 1 of pair pod instructions"),
  20. LocalizedString("Keep the RileyLink about 6 inches from the pod during pairing.", comment: "Label text for step 2 of pair pod instructions")
  21. ])
  22. .disabled(viewModel.state.instructionsDisabled)
  23. }
  24. .padding(.bottom, 8)
  25. }
  26. .accessibility(sortPriority: 1)
  27. }) {
  28. VStack {
  29. if self.viewModel.state.showProgressDetail {
  30. self.viewModel.error.map {
  31. ErrorView($0, errorClass: $0.recoverable ? .normal : .critical)
  32. .accessibility(sortPriority: 0)
  33. }
  34. if self.viewModel.error == nil {
  35. VStack {
  36. ProgressIndicatorView(state: self.viewModel.state.progressState)
  37. if self.viewModel.state.isFinished {
  38. FrameworkLocalText("Paired", comment: "Label text indicating pairing finished.")
  39. .bold()
  40. .padding(.top)
  41. }
  42. }
  43. .padding(.bottom, 8)
  44. }
  45. }
  46. if self.viewModel.error != nil && self.viewModel.podIsActivated {
  47. Button(action: {
  48. self.viewModel.didRequestDeactivation?()
  49. }) {
  50. Text(LocalizedString("Deactivate Pod", comment: "Button text for deactivate pod button"))
  51. .accessibility(identifier: "button_deactivate_pod")
  52. .actionButtonStyle(.destructive)
  53. }
  54. .disabled(self.viewModel.state.isProcessing)
  55. }
  56. if (self.viewModel.error == nil || self.viewModel.error?.recoverable == true) {
  57. Button(action: {
  58. self.viewModel.continueButtonTapped()
  59. }) {
  60. Text(self.viewModel.state.nextActionButtonDescription)
  61. .accessibility(identifier: "button_next_action")
  62. .accessibility(label: Text(self.viewModel.state.actionButtonAccessibilityLabel))
  63. .actionButtonStyle(.primary)
  64. }
  65. .disabled(self.viewModel.state.isProcessing)
  66. .animation(nil)
  67. .zIndex(1)
  68. }
  69. }
  70. .transition(AnyTransition.opacity.combined(with: .move(edge: .bottom)))
  71. .padding()
  72. }
  73. .animation(.default)
  74. .alert(isPresented: $cancelModalIsPresented) { cancelPairingModal }
  75. .navigationBarTitle(LocalizedString("Pair Pod", comment: "Pair Pod navigationBarTitle"), displayMode: .automatic)
  76. .navigationBarBackButtonHidden(self.viewModel.backButtonHidden)
  77. .navigationBarItems(trailing: self.viewModel.state.navBarVisible ? cancelButton : nil)
  78. }
  79. var cancelButton: some View {
  80. Button(LocalizedString("Cancel", comment: "Cancel button text in navigation bar on pair pod UI")) {
  81. if viewModel.podIsActivated {
  82. cancelModalIsPresented = true
  83. } else {
  84. viewModel.didCancelSetup?()
  85. }
  86. }
  87. .accessibility(identifier: "button_cancel")
  88. .disabled(self.viewModel.state.isProcessing)
  89. }
  90. var cancelPairingModal: Alert {
  91. return Alert(
  92. title: FrameworkLocalText("Are you sure you want to cancel Pod setup?", comment: "Alert title for cancel pairing modal"),
  93. message: FrameworkLocalText("If you cancel Pod setup, the current Pod will be deactivated and will be unusable.", comment: "Alert message body for confirm pod attachment"),
  94. primaryButton: .destructive(FrameworkLocalText("Yes, Deactivate Pod", comment: "Button title for confirm deactivation option"), action: { viewModel.didRequestDeactivation?() }),
  95. secondaryButton: .default(FrameworkLocalText("No, Continue With Pod", comment: "Continue pairing button title of in pairing cancel modal"))
  96. )
  97. }
  98. }