Kaynağa Gözat

Use bundle name for contact

Jonas Björkert 1 yıl önce
ebeveyn
işleme
2e3c07746a

+ 1 - 1
LoopFollow.xcodeproj/project.pbxproj

@@ -612,6 +612,7 @@
 			isa = PBXGroup;
 			children = (
 				DD50C7512D0828B40057AE6F /* Settings */,
+				DD50C7542D0862770057AE6F /* ContactImageUpdater.swift */,
 			);
 			path = Contact;
 			sourceTree = "<group>";
@@ -716,7 +717,6 @@
 				FCA2DDE52501095000254A8C /* Timers.swift */,
 				DD608A0B2C27415C00F91132 /* BackgroundAlertManager.swift */,
 				DDF699932C555B310058A8D9 /* ViewControllerManager.swift */,
-				DD50C7542D0862770057AE6F /* ContactImageUpdater.swift */,
 			);
 			path = Controllers;
 			sourceTree = "<group>";

+ 4 - 2
LoopFollow/Controllers/ContactImageUpdater.swift

@@ -26,7 +26,9 @@ class ContactImageUpdater {
                 return
             }
 
-            let predicate = CNContact.predicateForContacts(matchingName: "LoopFollowBG")
+            let bundleDisplayName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? "LoopFollow"
+            let contactName = "\(bundleDisplayName) - BG"
+            let predicate = CNContact.predicateForContacts(matchingName: contactName)
             let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactImageDataKey] as [CNKeyDescriptor]
 
             do {
@@ -40,7 +42,7 @@ class ContactImageUpdater {
                     print("Contact image updated successfully.")
                 } else {
                     let newContact = CNMutableContact()
-                    newContact.givenName = "LoopFollowBG"
+                    newContact.givenName = contactName
                     newContact.imageData = imageData
                     let saveRequest = CNSaveRequest()
                     saveRequest.add(newContact, toContainerWithIdentifier: nil)

+ 1 - 1
LoopFollow/Contact/Settings/ContactSettingsView.swift

@@ -21,7 +21,7 @@ struct ContactSettingsView: View {
         NavigationView {
             Form {
                 Section(header: Text("Contact Integration")) {
-                    Text("Add the contact named LoopFollowBG to your watch face to show the current BG value in real time.")
+                    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.")
                         .font(.footnote)
                         .foregroundColor(.secondary)
                         .padding(.vertical, 4)

+ 12 - 0
LoopFollow/Contact/Settings/ContactSettingsViewModel.swift

@@ -9,7 +9,19 @@
 import Foundation
 import Combine
 
+import Foundation
+
+extension Bundle {
+    var displayName: String {
+        return object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? "LoopFollow"
+    }
+}
+
 class ContactSettingsViewModel: ObservableObject {
+    var contactName: String {
+        "'\(Bundle.main.displayName) - BG'"
+    }
+
     @Published var contactEnabled: Bool {
         didSet {
             storage.contactEnabled.value = contactEnabled