DeliveryUncertaintyRecoveryView.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // DeliveryUncertaintyRecoveryView.swift
  3. // MockKitUI
  4. //
  5. // Created by Pete Schwamb on 8/17/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. import LoopKitUI
  10. struct DeliveryUncertaintyRecoveryView: View, HorizontalSizeClassOverride {
  11. @Environment(\.dismissAction) private var dismiss
  12. let appName: String
  13. let uncertaintyStartedAt: Date
  14. let recoverCommsTapped: () -> Void
  15. var body: some View {
  16. NavigationView {
  17. GuidePage(content: {
  18. Text("\(self.appName) has been unable to communicate with the Simulator Pump since \(self.uncertaintyDateLocalizedString).\n\nWithout communication, the app cannot continue to send commands for insulin delivery or display accurate, recent information about your active insulin or the insulin being delivered.")
  19. }) {
  20. Button(action: {
  21. self.recoverCommsTapped()
  22. self.dismiss()
  23. }) {
  24. Text(LocalizedString("Recover Simulator", comment: "Button title recovering comms"))
  25. .actionButtonStyle()
  26. .padding()
  27. }
  28. }
  29. .environment(\.horizontalSizeClass, horizontalOverride)
  30. .navigationBarTitle(Text("Comms Recovery"), displayMode: .large)
  31. .toolbar {
  32. ToolbarItem(placement: .navigationBarLeading) {
  33. backButton
  34. }
  35. }
  36. }
  37. }
  38. private var uncertaintyDateLocalizedString: String {
  39. DateFormatter.localizedString(from: uncertaintyStartedAt, dateStyle: .none, timeStyle: .short)
  40. }
  41. private var backButton: some View {
  42. Button(LocalizedString("Back", comment: "Back button text on DeliveryUncertaintyRecoveryView"), action: {
  43. self.dismiss()
  44. })
  45. }
  46. }
  47. struct DeliveryUncertaintyRecoveryView_Previews: PreviewProvider {
  48. static var previews: some View {
  49. DeliveryUncertaintyRecoveryView(appName: "Test App", uncertaintyStartedAt: Date()) {
  50. print("Recover Comms")
  51. }
  52. }
  53. }
  54. struct _DeliveryUncertaintyRecoveryView: View {
  55. let appName: String
  56. let uncertaintyStartedAt: Date
  57. let recoverCommsTapped: () -> Void
  58. var dismiss: () -> Void = {}
  59. var body: some View {
  60. DeliveryUncertaintyRecoveryView(
  61. appName: appName,
  62. uncertaintyStartedAt: uncertaintyStartedAt
  63. ) {
  64. recoverCommsTapped()
  65. dismiss()
  66. }
  67. .environment(\.dismissAction, { dismiss() })
  68. }
  69. }
  70. // Wrapper to provide a CompletionNotifying ViewController
  71. class DeliveryUncertaintyRecoveryViewController: UIHostingController<_DeliveryUncertaintyRecoveryView>, CompletionNotifying {
  72. public weak var completionDelegate: CompletionDelegate?
  73. init(appName: String, uncertaintyStartedAt: Date, recoverCommsTapped: @escaping () -> Void) {
  74. var view = _DeliveryUncertaintyRecoveryView(
  75. appName: appName,
  76. uncertaintyStartedAt: uncertaintyStartedAt,
  77. recoverCommsTapped: recoverCommsTapped
  78. )
  79. super.init(rootView: view)
  80. view.dismiss = {
  81. self.completionDelegate?.completionNotifyingDidComplete(self)
  82. }
  83. }
  84. @objc required dynamic init?(coder aDecoder: NSCoder) {
  85. fatalError("init(coder:) has not been implemented")
  86. }
  87. }