ContactSettingsView.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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("Select 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("Select Text Color", selection: $viewModel.contactTextColor) {
  38. ForEach(ContactColorOption.allCases, id: \.rawValue) { option in
  39. Text(option.rawValue.capitalized).tag(option.rawValue)
  40. }
  41. }
  42. }
  43. Section(header: Text("Additional Information")) {
  44. Text("To see your trend or delta, include one in the original '\(viewModel.contactName)' contact, or create separate contacts ending in '- Trend' and '- Delta' for up to three contacts on your watch face.")
  45. .font(.footnote)
  46. .foregroundColor(.secondary)
  47. .padding(.vertical, 4)
  48. Text("Show Trend")
  49. .font(.subheadline)
  50. Picker("Show Trend", selection: $viewModel.contactTrend) {
  51. ForEach(ContactIncludeOption.allCases, id: \.self) { option in
  52. Text(option.rawValue).tag(option)
  53. }
  54. }
  55. .pickerStyle(SegmentedPickerStyle())
  56. Text("Show Delta")
  57. .font(.subheadline)
  58. Picker("Show Delta", selection: $viewModel.contactDelta) {
  59. ForEach(ContactIncludeOption.allCases, id: \.self) { option in
  60. Text(option.rawValue).tag(option)
  61. }
  62. }
  63. .pickerStyle(SegmentedPickerStyle())
  64. }
  65. }
  66. }
  67. .alert(isPresented: $showAlert) {
  68. Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("OK")))
  69. }
  70. }
  71. .preferredColorScheme(Storage.shared.forceDarkMode.value ? .dark : nil)
  72. .navigationBarTitle("Contact", displayMode: .inline)
  73. }
  74. private func requestContactAccess() {
  75. let contactStore = CNContactStore()
  76. let status = CNContactStore.authorizationStatus(for: .contacts)
  77. if status == .authorized {
  78. // Already authorized, do nothing
  79. } else if status == .notDetermined {
  80. contactStore.requestAccess(for: .contacts) { granted, _ in
  81. DispatchQueue.main.async {
  82. if !granted {
  83. viewModel.contactEnabled = false
  84. showAlert(title: "Access Denied", message: "Please allow access to Contacts in Settings to enable this feature.")
  85. }
  86. }
  87. }
  88. } else if status == .denied {
  89. viewModel.contactEnabled = false
  90. showAlert(title: "Access Denied", message: "Access to Contacts is denied. Please go to Settings and enable Contacts access.")
  91. } else if status == .restricted {
  92. viewModel.contactEnabled = false
  93. showAlert(title: "Access Restricted", message: "Access to Contacts is restricted.")
  94. } else {
  95. viewModel.contactEnabled = false
  96. showAlert(title: "Error", message: "An unknown error occurred while checking Contacts access.")
  97. }
  98. }
  99. private func showAlert(title: String, message: String) {
  100. alertTitle = title
  101. alertMessage = message
  102. showAlert = true
  103. }
  104. }