TempTargetPresetsView.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import SwiftUI
  2. struct TempTargetPresetsView: View {
  3. let state: WatchState
  4. let tempTargetPresets: [TempTargetPresetWatch]
  5. var onPresetAction: () -> Void // Callback to handle selection of preset, or cancellation, and dismiss the sheet
  6. private let activePresetGradient = LinearGradient(
  7. colors: [
  8. Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902), // #43BBE9
  9. Color(red: 0.3411764706, green: 0.6666666667, blue: 0.9254901961) // #57AAEC
  10. ],
  11. startPoint: .leading,
  12. endPoint: .trailing
  13. )
  14. private var sortedPresets: [TempTargetPresetWatch] {
  15. tempTargetPresets.sorted { $0.isEnabled && !$1.isEnabled }
  16. }
  17. private var activePreset: TempTargetPresetWatch? {
  18. sortedPresets.first { $0.isEnabled }
  19. }
  20. var body: some View {
  21. NavigationView {
  22. List {
  23. if let active = activePreset {
  24. Button("Stop \(active.name)") {
  25. state.sendCancelTempTargetRequest()
  26. onPresetAction()
  27. }
  28. .foregroundColor(.white)
  29. .listRowBackground(
  30. Color.loopRed
  31. .clipShape(RoundedRectangle(cornerRadius: 8))
  32. )
  33. }
  34. if sortedPresets.isEmpty {
  35. Text("No Temp Target Presets")
  36. .font(.caption)
  37. .foregroundColor(.secondary)
  38. } else {
  39. ForEach(sortedPresets, id: \.name) { preset in
  40. Button(action: {
  41. if !preset.isEnabled {
  42. state.sendActivateTempTargetRequest(presetName: preset.name)
  43. }
  44. onPresetAction()
  45. }) {
  46. HStack {
  47. Text(preset.name)
  48. .font(.caption)
  49. if preset.isEnabled {
  50. Spacer()
  51. Text("is running")
  52. .font(.caption2)
  53. .foregroundStyle(.white)
  54. }
  55. }
  56. }
  57. .listRowBackground(
  58. preset.isEnabled ?
  59. activePresetGradient
  60. .clipShape(RoundedRectangle(cornerRadius: 8))
  61. : nil
  62. )
  63. .foregroundColor(preset.isEnabled ? .white : .primary)
  64. }
  65. }
  66. }
  67. .navigationTitle("Temp Target Presets")
  68. }
  69. }
  70. }