RemoteControlConfig.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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: String?
  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. Section(header: Text("Trio Remote Control")) {
  35. SettingInputSection(
  36. decimalValue: $decimalPlaceholder,
  37. booleanValue: $state.isTRCEnabled,
  38. shouldDisplayHint: $shouldDisplayHint,
  39. selectedVerboseHint: Binding(
  40. get: { selectedVerboseHint },
  41. set: {
  42. selectedVerboseHint = $0
  43. hintLabel = "Enable Remote Command"
  44. }
  45. ),
  46. units: state.units,
  47. type: .boolean,
  48. label: "Enable Remote Control",
  49. miniHint: "Remote Control allow Trio to receive instructions, such as boluses and temp targets, from LoopFollow.",
  50. verboseHint: "When Remote Control is enabled, you can send boluses, overrides, temporary targets, carbs, and other commands to Trio via push notifications. To ensure security, these commands are protected by a shared secret, which must be entered in LoopFollow."
  51. )
  52. }
  53. Section(
  54. header: Text("Shared Secret"),
  55. content: {
  56. TextField("Enter Shared Secret", text: $state.sharedSecret)
  57. .disableAutocorrection(true)
  58. .autocapitalization(.none)
  59. .padding(8)
  60. .background(Color(UIColor.systemGray6))
  61. .cornerRadius(8)
  62. Button(action: {
  63. UIPasteboard.general.string = state.sharedSecret
  64. isCopied = true
  65. }) {
  66. Label("Copy Secret", systemImage: "doc.on.doc")
  67. .frame(maxWidth: .infinity)
  68. }
  69. .buttonStyle(.bordered)
  70. .frame(maxWidth: .infinity)
  71. .alert(isPresented: $isCopied) {
  72. Alert(
  73. title: Text("Copied"),
  74. message: Text("Shared Secret copied to clipboard"),
  75. dismissButton: .default(Text("OK"))
  76. )
  77. }
  78. Button(action: {
  79. state.generateNewSharedSecret()
  80. }) {
  81. Label("Generate Secret", systemImage: "arrow.clockwise")
  82. .frame(maxWidth: .infinity)
  83. }
  84. .buttonStyle(.borderedProminent)
  85. .foregroundColor(.white)
  86. .frame(maxWidth: .infinity)
  87. }
  88. ).listRowBackground(Color.chart)
  89. }
  90. .sheet(isPresented: $shouldDisplayHint) {
  91. SettingInputHintView(
  92. hintDetent: $hintDetent,
  93. shouldDisplayHint: $shouldDisplayHint,
  94. hintLabel: hintLabel ?? "",
  95. hintText: selectedVerboseHint ?? "",
  96. sheetTitle: "Help"
  97. )
  98. }
  99. .scrollContentBackground(.hidden).background(color)
  100. .onAppear(perform: configureView)
  101. .navigationTitle("Remote Control")
  102. .navigationBarTitleDisplayMode(.automatic)
  103. }
  104. }
  105. }