ContactSettingsView.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // LoopFollow
  2. // ContactSettingsView.swift
  3. import Contacts
  4. import SwiftUI
  5. struct ContactSettingsView: View {
  6. @ObservedObject var viewModel: ContactSettingsViewModel
  7. @State private var showAlert: Bool = false
  8. @State private var alertTitle: String = ""
  9. @State private var alertMessage: String = ""
  10. var body: some View {
  11. NavigationView {
  12. Form {
  13. Section(header: Text("Contact Integration")) {
  14. Text("Add the contact named '\(viewModel.contactName)' to your watch face to show the current BG value in real time. Make sure to give the app full access to Contacts when prompted.")
  15. .font(.footnote)
  16. .foregroundColor(.secondary)
  17. .padding(.vertical, 4)
  18. Toggle("Enable Contact BG Updates", isOn: $viewModel.contactEnabled)
  19. .toggleStyle(SwitchToggleStyle())
  20. .onChange(of: viewModel.contactEnabled) { isEnabled in
  21. if isEnabled {
  22. requestContactAccess()
  23. }
  24. }
  25. }
  26. if viewModel.contactEnabled {
  27. Section(header: Text("Color Options")) {
  28. Text("Select the colors for your BG values. Note: not all watch faces allow control over colors. Recommend options like Activity or Modular Duo if you want to customize colors.")
  29. .font(.footnote)
  30. .foregroundColor(.secondary)
  31. .padding(.vertical, 4)
  32. Picker("Background Color", selection: $viewModel.contactBackgroundColor) {
  33. ForEach(ContactColorOption.allCases, id: \.rawValue) { option in
  34. Text(option.rawValue.capitalized).tag(option.rawValue)
  35. }
  36. }
  37. Picker("Color Mode", selection: $viewModel.contactColorMode) {
  38. ForEach(ContactColorMode.allCases, id: \.self) { mode in
  39. Text(mode.displayName).tag(mode)
  40. }
  41. }
  42. if viewModel.contactColorMode == .staticColor {
  43. Picker("Text Color", selection: $viewModel.contactTextColor) {
  44. ForEach(ContactColorOption.allCases, id: \.rawValue) { option in
  45. Text(option.rawValue.capitalized).tag(option.rawValue)
  46. }
  47. }
  48. } else {
  49. Text("Dynamic mode colors text based on BG range: Green (in range), Yellow (high), Red (low)")
  50. .font(.footnote)
  51. .foregroundColor(.secondary)
  52. }
  53. }
  54. Section(header: Text("Additional Information")) {
  55. Text("To see your trend, delta, or IOB, include them in another contact or create separate contacts. When using 'Include', select which contact to add the value to.")
  56. .font(.footnote)
  57. .foregroundColor(.secondary)
  58. .padding(.vertical, 4)
  59. Text("Trend")
  60. .font(.subheadline)
  61. Picker("Show Trend", selection: $viewModel.contactTrend) {
  62. ForEach(ContactIncludeOption.allCases, id: \.self) { option in
  63. Text(option.rawValue).tag(option)
  64. }
  65. }
  66. .pickerStyle(SegmentedPickerStyle())
  67. if viewModel.contactTrend == .include {
  68. Picker("Include Trend in", selection: $viewModel.contactTrendTarget) {
  69. ForEach(viewModel.availableTargets(for: .Trend), id: \.self) { target in
  70. Text(target.rawValue).tag(target)
  71. }
  72. }
  73. }
  74. Text("Delta")
  75. .font(.subheadline)
  76. Picker("Show Delta", selection: $viewModel.contactDelta) {
  77. ForEach(ContactIncludeOption.allCases, id: \.self) { option in
  78. Text(option.rawValue).tag(option)
  79. }
  80. }
  81. .pickerStyle(SegmentedPickerStyle())
  82. if viewModel.contactDelta == .include {
  83. Picker("Include Delta in", selection: $viewModel.contactDeltaTarget) {
  84. ForEach(viewModel.availableTargets(for: .Delta), id: \.self) { target in
  85. Text(target.rawValue).tag(target)
  86. }
  87. }
  88. }
  89. Text("IOB")
  90. .font(.subheadline)
  91. Picker("Show IOB", selection: $viewModel.contactIOB) {
  92. ForEach(ContactIncludeOption.allCases, id: \.self) { option in
  93. Text(option.rawValue).tag(option)
  94. }
  95. }
  96. .pickerStyle(SegmentedPickerStyle())
  97. if viewModel.contactIOB == .include {
  98. Picker("Include IOB in", selection: $viewModel.contactIOBTarget) {
  99. ForEach(viewModel.availableTargets(for: .IOB), id: \.self) { target in
  100. Text(target.rawValue).tag(target)
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. .alert(isPresented: $showAlert) {
  108. Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("OK")))
  109. }
  110. }
  111. .preferredColorScheme(Storage.shared.appearanceMode.value.colorScheme)
  112. .navigationBarTitle("Contact", displayMode: .inline)
  113. }
  114. private func requestContactAccess() {
  115. let contactStore = CNContactStore()
  116. let status = CNContactStore.authorizationStatus(for: .contacts)
  117. if status == .authorized {
  118. // Already authorized, do nothing
  119. } else if status == .notDetermined {
  120. contactStore.requestAccess(for: .contacts) { granted, _ in
  121. DispatchQueue.main.async {
  122. if !granted {
  123. viewModel.contactEnabled = false
  124. showAlert(title: "Access Denied", message: "Please allow access to Contacts in Settings to enable this feature.")
  125. }
  126. }
  127. }
  128. } else if status == .denied {
  129. viewModel.contactEnabled = false
  130. showAlert(title: "Access Denied", message: "Access to Contacts is denied. Please go to Settings and enable Contacts access.")
  131. } else if status == .restricted {
  132. viewModel.contactEnabled = false
  133. showAlert(title: "Access Restricted", message: "Access to Contacts is restricted.")
  134. } else {
  135. viewModel.contactEnabled = false
  136. showAlert(title: "Error", message: "An unknown error occurred while checking Contacts access.")
  137. }
  138. }
  139. private func showAlert(title: String, message: String) {
  140. alertTitle = title
  141. alertMessage = message
  142. showAlert = true
  143. }
  144. }