DeliveryUncertaintyRecoveryView.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // Wrapper to provide a CompletionNotifying ViewController
  55. class DeliveryUncertaintyRecoveryViewController: UIHostingController<AnyView>, CompletionNotifying {
  56. public weak var completionDelegate: CompletionDelegate?
  57. init(appName: String, uncertaintyStartedAt: Date, recoverCommsTapped: @escaping () -> Void) {
  58. var dismiss = {}
  59. let view = DeliveryUncertaintyRecoveryView(appName: appName, uncertaintyStartedAt: uncertaintyStartedAt) {
  60. recoverCommsTapped()
  61. dismiss()
  62. }
  63. .environment(\.dismissAction, { dismiss() })
  64. super.init(rootView: AnyView(view))
  65. dismiss = {
  66. self.completionDelegate?.completionNotifyingDidComplete(self)
  67. }
  68. }
  69. @objc required dynamic init?(coder aDecoder: NSCoder) {
  70. fatalError("init(coder:) has not been implemented")
  71. }
  72. }