RemoteControlConfig.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. @Environment(AppState.self) var appState
  17. var body: some View {
  18. List {
  19. SettingInputSection(
  20. decimalValue: $decimalPlaceholder,
  21. booleanValue: $state.isTrioRemoteControlEnabled,
  22. shouldDisplayHint: $shouldDisplayHint,
  23. selectedVerboseHint: Binding(
  24. get: { selectedVerboseHint },
  25. set: {
  26. selectedVerboseHint = $0.map { AnyView($0) }
  27. hintLabel = "Enable Remote Command"
  28. }
  29. ),
  30. units: state.units,
  31. type: .boolean,
  32. label: "Enable Remote Control",
  33. miniHint: "Allow Trio to receive commands from Loop Follow remotely.",
  34. verboseHint: VStack(alignment: .leading, spacing: 10) {
  35. Text("Default: OFF").bold()
  36. Text(
  37. "When Remote Control is enabled, you can send boluses, overrides, temporary targets, carbs, and other commands to Trio via push notifications."
  38. )
  39. Text(
  40. "To ensure security, these commands are protected by a shared secret, which must be entered in the Loop Follow app."
  41. )
  42. },
  43. headerText: "Trio Remote Control"
  44. )
  45. Section(
  46. header: Text("Shared Secret"),
  47. content: {
  48. TextField("Enter Shared Secret", text: $state.sharedSecret)
  49. .disableAutocorrection(true)
  50. .autocapitalization(.none)
  51. .padding(8)
  52. .background(Color(UIColor.systemGray6))
  53. .cornerRadius(8)
  54. Button(action: {
  55. UIPasteboard.general.string = state.sharedSecret
  56. isCopied = true
  57. }) {
  58. Label("Copy Secret", systemImage: "doc.on.doc")
  59. .frame(maxWidth: .infinity)
  60. }
  61. .buttonStyle(.bordered)
  62. .frame(maxWidth: .infinity)
  63. .alert(isPresented: $isCopied) {
  64. Alert(
  65. title: Text("Copied"),
  66. message: Text("Shared Secret copied to clipboard"),
  67. dismissButton: .default(Text("OK"))
  68. )
  69. }
  70. Button(action: {
  71. state.generateNewSharedSecret()
  72. }) {
  73. Label("Generate Secret", systemImage: "arrow.clockwise")
  74. .frame(maxWidth: .infinity)
  75. }
  76. .buttonStyle(.borderedProminent)
  77. .foregroundColor(.white)
  78. .frame(maxWidth: .infinity)
  79. }
  80. ).listRowBackground(Color.chart)
  81. }
  82. .listSectionSpacing(sectionSpacing)
  83. .sheet(isPresented: $shouldDisplayHint) {
  84. SettingInputHintView(
  85. hintDetent: $hintDetent,
  86. shouldDisplayHint: $shouldDisplayHint,
  87. hintLabel: hintLabel ?? "",
  88. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  89. sheetTitle: "Help"
  90. )
  91. }
  92. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  93. .onAppear(perform: configureView)
  94. .navigationTitle("Remote Control")
  95. .navigationBarTitleDisplayMode(.automatic)
  96. }
  97. }
  98. }