RileyLinkSetupView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // RileyLinkSelectionView.swift
  3. // OmniKitUI
  4. //
  5. // Created by Pete Schwamb on 6/7/22.
  6. // Copyright © 2022 Pete Schwamb. All rights reserved.
  7. //
  8. import SwiftUI
  9. import LoopKitUI
  10. import RileyLinkKitUI
  11. struct RileyLinkSetupView: View {
  12. public var cancelButtonTapped: (() -> Void)?
  13. @Environment(\.dismissAction) private var dismiss
  14. let nextAction: () -> Void
  15. @ObservedObject private var dataSource: RileyLinkListDataSource
  16. init(dataSource: RileyLinkListDataSource, nextAction: @escaping () -> Void) {
  17. self.dataSource = dataSource
  18. self.nextAction = nextAction
  19. }
  20. @State private var isOn: Bool = false
  21. var body: some View {
  22. VStack {
  23. List {
  24. VStack {
  25. Image("RileyLink", bundle: Bundle(for: RileyLinkCell.self))
  26. .resizable()
  27. .foregroundColor(Color(imageTint))
  28. .aspectRatio(contentMode: ContentMode.fit)
  29. bodyText
  30. .foregroundColor(.secondary)
  31. }
  32. Section(header: HStack {
  33. FrameworkLocalText("Devices", comment: "Header for devices section of RileyLinkSetupView")
  34. Spacer()
  35. ProgressView()
  36. }) {
  37. ForEach(dataSource.devices, id: \.peripheralIdentifier) { device in
  38. Toggle(isOn: dataSource.autoconnectBinding(for: device)) {
  39. HStack {
  40. Text(device.name ?? "Unknown")
  41. Spacer()
  42. Text(formatRSSI(rssi:device.rssi)).foregroundColor(.secondary)
  43. }
  44. }
  45. }
  46. }
  47. }
  48. Spacer()
  49. continueButton
  50. .padding([.bottom, .horizontal])
  51. }
  52. .navigationTitle(LocalizedString("RileyLink Setup", comment: "Navigation title for RileyLinkSetupView"))
  53. .toolbar {
  54. ToolbarItem(placement: .navigationBarTrailing) {
  55. Button(LocalizedString("Cancel", comment: "Cancel button title"), action: {
  56. cancelButtonTapped?()
  57. })
  58. }
  59. }
  60. .navigationBarHidden(false)
  61. .onAppear { dataSource.isScanningEnabled = true }
  62. .onDisappear { dataSource.isScanningEnabled = false }
  63. }
  64. var decimalFormatter: NumberFormatter = {
  65. let formatter = NumberFormatter()
  66. formatter.numberStyle = .decimal
  67. formatter.minimumFractionDigits = 0
  68. formatter.maximumFractionDigits = 2
  69. return formatter
  70. }()
  71. private func formatRSSI(rssi: Int?) -> String {
  72. if let rssi = rssi, let rssiStr = decimalFormatter.decibleString(from: rssi) {
  73. return rssiStr
  74. } else {
  75. return ""
  76. }
  77. }
  78. var imageTint: UIColor {
  79. return UIColor(named: "RileyLink Tint", in: Bundle(for: RileyLinkCell.self), compatibleWith: nil) ?? .gray
  80. }
  81. @ViewBuilder
  82. private var bodyText: some View {
  83. Text(LocalizedString("RileyLink allows for communication with the pump over Bluetooth", comment: "bodyText for RileyLinkSetupView"))
  84. }
  85. private var continueButton: some View {
  86. Button(LocalizedString("Continue", comment: "Text for continue button on PodSetupView"), action: nextAction)
  87. .buttonStyle(ActionButtonStyle())
  88. .disabled(!dataSource.connecting)
  89. }
  90. }