RemoteControlConfig.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. List {
  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: "Allow Trio to receive instructions, such as boluses and temp targets, from Loop Follow remotely.",
  49. verboseHint: VStack(spacing: 10) {
  50. Text("Default: OFF").bold()
  51. Text(
  52. "When Remote Control is enabled, you can send boluses, overrides, temporary targets, carbs, and other commands to Trio via push notifications."
  53. )
  54. Text(
  55. "To ensure security, these commands are protected by a shared secret, which must be entered in the Loop Follow app."
  56. )
  57. },
  58. headerText: "Trio Remote Control"
  59. )
  60. Section(
  61. header: Text("Shared Secret"),
  62. content: {
  63. TextField("Enter Shared Secret", text: $state.sharedSecret)
  64. .disableAutocorrection(true)
  65. .autocapitalization(.none)
  66. .padding(8)
  67. .background(Color(UIColor.systemGray6))
  68. .cornerRadius(8)
  69. Button(action: {
  70. UIPasteboard.general.string = state.sharedSecret
  71. isCopied = true
  72. }) {
  73. Label("Copy Secret", systemImage: "doc.on.doc")
  74. .frame(maxWidth: .infinity)
  75. }
  76. .buttonStyle(.bordered)
  77. .frame(maxWidth: .infinity)
  78. .alert(isPresented: $isCopied) {
  79. Alert(
  80. title: Text("Copied"),
  81. message: Text("Shared Secret copied to clipboard"),
  82. dismissButton: .default(Text("OK"))
  83. )
  84. }
  85. Button(action: {
  86. state.generateNewSharedSecret()
  87. }) {
  88. Label("Generate Secret", systemImage: "arrow.clockwise")
  89. .frame(maxWidth: .infinity)
  90. }
  91. .buttonStyle(.borderedProminent)
  92. .foregroundColor(.white)
  93. .frame(maxWidth: .infinity)
  94. }
  95. ).listRowBackground(Color.chart)
  96. }
  97. .listSectionSpacing(sectionSpacing)
  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. }