RemoteControlConfig.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import Combine
  2. import SwiftUI
  3. import Swinject
  4. import UIKit
  5. extension RemoteControlConfig {
  6. struct RootView: BaseView {
  7. let resolver: Resolver
  8. @StateObject var state = StateModel()
  9. @State private var shouldDisplayHint: Bool = false
  10. @State var hintDetent = PresentationDetent.large
  11. @State var selectedVerboseHint: AnyView?
  12. @State var hintLabel: String?
  13. @State private var decimalPlaceholder: Decimal = 0.0
  14. @State private var isCopied: Bool = false
  15. @Environment(\.colorScheme) var colorScheme
  16. private var color: LinearGradient {
  17. colorScheme == .dark ? LinearGradient(
  18. gradient: Gradient(colors: [
  19. Color.bgDarkBlue,
  20. Color.bgDarkerDarkBlue
  21. ]),
  22. startPoint: .top,
  23. endPoint: .bottom
  24. )
  25. :
  26. LinearGradient(
  27. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  28. startPoint: .top,
  29. endPoint: .bottom
  30. )
  31. }
  32. var body: some View {
  33. Form {
  34. SettingInputSection(
  35. decimalValue: $decimalPlaceholder,
  36. booleanValue: $state.isTrioRemoteControlEnabled,
  37. shouldDisplayHint: $shouldDisplayHint,
  38. selectedVerboseHint: Binding(
  39. get: { selectedVerboseHint },
  40. set: {
  41. selectedVerboseHint = $0.map { AnyView($0) }
  42. hintLabel = "Enable Remote Command"
  43. }
  44. ),
  45. units: state.units,
  46. type: .boolean,
  47. label: "Enable Remote Control",
  48. miniHint: """
  49. Allow Trio to receive instructions, such as boluses and temp targets, from LoopFollow remotely.
  50. Default: OFF
  51. """,
  52. verboseHint: VStack {
  53. Text("Default: OFF").bold()
  54. Text("""
  55. When Remote Control is enabled, you can send boluses, overrides, temporary targets, carbs, and other commands to Trio via push notifications.
  56. To ensure security, these commands are protected by a shared secret, which must be entered in LoopFollow.
  57. """)
  58. },
  59. headerText: "Trio Remote Control"
  60. )
  61. Section(
  62. header: Text("Shared Secret"),
  63. content: {
  64. TextField("Enter Shared Secret", text: $state.sharedSecret)
  65. .disableAutocorrection(true)
  66. .autocapitalization(.none)
  67. .padding(8)
  68. .background(Color(UIColor.systemGray6))
  69. .cornerRadius(8)
  70. Button(action: {
  71. UIPasteboard.general.string = state.sharedSecret
  72. isCopied = true
  73. }) {
  74. Label("Copy Secret", systemImage: "doc.on.doc")
  75. .frame(maxWidth: .infinity)
  76. }
  77. .buttonStyle(.bordered)
  78. .frame(maxWidth: .infinity)
  79. .alert(isPresented: $isCopied) {
  80. Alert(
  81. title: Text("Copied"),
  82. message: Text("Shared Secret copied to clipboard"),
  83. dismissButton: .default(Text("OK"))
  84. )
  85. }
  86. Button(action: {
  87. state.generateNewSharedSecret()
  88. }) {
  89. Label("Generate Secret", systemImage: "arrow.clockwise")
  90. .frame(maxWidth: .infinity)
  91. }
  92. .buttonStyle(.borderedProminent)
  93. .foregroundColor(.white)
  94. .frame(maxWidth: .infinity)
  95. }
  96. ).listRowBackground(Color.chart)
  97. }
  98. .sheet(isPresented: $shouldDisplayHint) {
  99. SettingInputHintView(
  100. hintDetent: $hintDetent,
  101. shouldDisplayHint: $shouldDisplayHint,
  102. hintLabel: hintLabel ?? "",
  103. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  104. sheetTitle: "Help"
  105. )
  106. }
  107. .scrollContentBackground(.hidden).background(color)
  108. .onAppear(perform: configureView)
  109. .navigationTitle("Remote Control")
  110. .navigationBarTitleDisplayMode(.automatic)
  111. }
  112. }
  113. }