PlayTestBeepsView.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // PlayTestBeepsView.swift
  3. // OmniKit
  4. //
  5. // Created by Joe Moran on 9/1/23.
  6. // Copyright © 2023 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. import LoopKit
  10. struct PlayTestBeepsView: View {
  11. @Environment(\.horizontalSizeClass) var horizontalSizeClass
  12. @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
  13. var toRun: ((_ completion: @escaping (_ result: Error?) -> Void) -> Void)?
  14. private let title = LocalizedString("Play Test Beeps", comment: "navigation title for play test beeps")
  15. private let actionString = LocalizedString("Playing Test Beeps...", comment: "button title when executing play test beeps")
  16. private let failedString: String = LocalizedString("Failed to play test beeps.", comment: "Alert title for error when playing test beeps")
  17. @State private var alertIsPresented: Bool = false
  18. @State private var displayString: String = ""
  19. @State private var successMessage = LocalizedString("Play test beeps command sent successfully.\n\nIf you did not hear any beeps from your Pod, the piezo speaker in your Pod may be broken or disabled.", comment: "Success message for play test beeps")
  20. @State private var error: Error? = nil
  21. @State private var executing: Bool = false
  22. @State private var showActivityView = false
  23. var body: some View {
  24. VStack {
  25. List {
  26. Section {
  27. Text(self.displayString).fixedSize(horizontal: false, vertical: true)
  28. }
  29. }
  30. VStack {
  31. Button(action: {
  32. asyncAction()
  33. }) {
  34. Text(buttonText)
  35. .actionButtonStyle(.primary)
  36. }
  37. .padding()
  38. .disabled(executing)
  39. }
  40. .padding(self.horizontalSizeClass == .regular ? .bottom : [])
  41. .background(Color(UIColor.secondarySystemGroupedBackground).shadow(radius: 5))
  42. }
  43. .insetGroupedListStyle()
  44. .navigationTitle(title)
  45. .navigationBarTitleDisplayMode(.automatic)
  46. .alert(isPresented: $alertIsPresented, content: { alert(error: error) })
  47. .onFirstAppear {
  48. asyncAction()
  49. }
  50. }
  51. private func asyncAction () {
  52. DispatchQueue.global(qos: .utility).async {
  53. executing = true
  54. self.displayString = ""
  55. toRun?() { (error) in
  56. executing = false
  57. if let error = error {
  58. self.displayString = ""
  59. self.error = error
  60. self.alertIsPresented = true
  61. } else {
  62. self.displayString = successMessage
  63. }
  64. }
  65. }
  66. }
  67. private var buttonText: String {
  68. if executing {
  69. return actionString
  70. } else {
  71. return title
  72. }
  73. }
  74. private func alert(error: Error?) -> SwiftUI.Alert {
  75. return SwiftUI.Alert(
  76. title: Text(failedString),
  77. message: Text(error?.localizedDescription ?? "No Error")
  78. )
  79. }
  80. }
  81. struct PlayTestBeepsView_Previews: PreviewProvider {
  82. static var previews: some View {
  83. NavigationView {
  84. PlayTestBeepsView() { completion in
  85. completion(nil)
  86. }
  87. }
  88. }
  89. }