From 49a7747ae61b50998513c11e1e9794362cc39137 Mon Sep 17 00:00:00 2001 From: LEO Yoon-Tsaw Date: Wed, 24 May 2023 21:37:41 -0400 Subject: [PATCH] Use group container --- Chinese Time.xcodeproj/project.pbxproj | 8 ++++---- Shared/Delegates.swift | 23 ++++++++++++++++++----- Watch/ContentView.swift | 7 +++++-- iOS/ViewController.swift | 7 +++++-- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Chinese Time.xcodeproj/project.pbxproj b/Chinese Time.xcodeproj/project.pbxproj index 2b76ed9..da401a4 100644 --- a/Chinese Time.xcodeproj/project.pbxproj +++ b/Chinese Time.xcodeproj/project.pbxproj @@ -1535,8 +1535,8 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; - APP_BUILD = 78; - APP_VERSION = 4.2.3; + APP_BUILD = 79; + APP_VERSION = 4.2.4; CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_ANALYZER_NONNULL = YES; CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; @@ -1603,8 +1603,8 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; - APP_BUILD = 78; - APP_VERSION = 4.2.3; + APP_BUILD = 79; + APP_VERSION = 4.2.4; CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_ANALYZER_NONNULL = YES; CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; diff --git a/Shared/Delegates.swift b/Shared/Delegates.swift index f675af4..ffbdfb2 100644 --- a/Shared/Delegates.swift +++ b/Shared/Delegates.swift @@ -108,6 +108,14 @@ final class LocationManager: NSObject, ObservableObject, CLLocationManagerDelega final class DataContainer: ObservableObject { static let shared = DataContainer() +#if os(macOS) + static let groupId = Bundle.main.object(forInfoDictionaryKey: "GroupID") as! String +#elseif os(iOS) + static let groupId = "group.ChineseTime" +#elseif os(watchOS) + static let groupId = "group.ChineseTime.Watch" +#endif + lazy var persistentContainer: NSPersistentCloudKitContainer = { /* The persistent container for the application. This implementation @@ -124,15 +132,12 @@ final class DataContainer: ObservableObject { print(error.localizedDescription) } #endif + let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: DataContainer.groupId)! #if os(macOS) - let prefix = Bundle.main.object(forInfoDictionaryKey: "GroupID") as! String - let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: prefix)! let description = NSPersistentStoreDescription(url: url.appendingPathComponent("ChineseTime")) #elseif os(iOS) - let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.ChineseTime")! let description = NSPersistentStoreDescription(url: url.appendingPathComponent("ChineseTime.sqlite")) #elseif os(watchOS) - let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.ChineseTime.Watch")! let description = NSPersistentStoreDescription(url: url.appendingPathComponent("ChineseTime.sqlite")) #endif description.configuration = "Cloud" @@ -203,7 +208,11 @@ final class DataContainer: ObservableObject { try? persistentContainer.viewContext.setQueryGenerationFrom(.current) let managedContext = persistentContainer.viewContext let fetchRequest = NSFetchRequest(entityName: "Layout") - fetchRequest.predicate = NSPredicate(format: "(name == %@) AND (deviceName == %@)", argumentArray: [name ?? NSLocalizedString("Default", comment: "Default save file name"), deviceName ?? self.deviceName]) + var appDeviceName: String? = nil + if let userDefault = UserDefaults(suiteName: DataContainer.groupId) { + appDeviceName = userDefault.string(forKey: "deviceName") + } + fetchRequest.predicate = NSPredicate(format: "(name == %@) AND (deviceName == %@)", argumentArray: [name ?? NSLocalizedString("Default", comment: "Default save file name"), (deviceName ?? appDeviceName) ?? self.deviceName]) if let fetchedEntities = try? managedContext.fetch(fetchRequest), let savedLayout = fetchedEntities.last?.value(forKey: "code") as? String { @@ -275,6 +284,10 @@ final class DataContainer: ObservableObject { } func saveLayout(_ layout: String, name: String? = nil) { + if let userDefaults = UserDefaults(suiteName: DataContainer.groupId) { + userDefaults.set(deviceName, forKey: "deviceName") + userDefaults.synchronize() + } let managedContext = persistentContainer.viewContext managedContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy let fetchRequest = NSFetchRequest(entityName: "Layout") diff --git a/Watch/ContentView.swift b/Watch/ContentView.swift index 61891b6..e93e46b 100644 --- a/Watch/ContentView.swift +++ b/Watch/ContentView.swift @@ -194,7 +194,10 @@ struct ContentView: View { .tint(Color.pink) Toggle(NSLocalizedString("分列日時", comment: "Split Date and Time"), isOn: $dual) .onChange(of: dual) { newValue in - UserDefaults.standard.set(newValue, forKey: "ChinsesTime.DualWatchDisplay") + if let userDefaults = UserDefaults(suiteName: DataContainer.groupId) { + userDefaults.set(newValue, forKey: "ChinsesTime.DualWatchDisplay") + userDefaults.synchronize() + } } .toggleStyle(.button) .tint(.pink) @@ -206,7 +209,7 @@ struct ContentView: View { } } .onAppear { - dual = UserDefaults.standard.bool(forKey: "ChinsesTime.DualWatchDisplay") + dual = UserDefaults(suiteName: DataContainer.groupId)?.bool(forKey: "ChinsesTime.DualWatchDisplay") ?? dual size = proxy.size } } diff --git a/iOS/ViewController.swift b/iOS/ViewController.swift index a1b24cb..c5154ad 100644 --- a/iOS/ViewController.swift +++ b/iOS/ViewController.swift @@ -74,7 +74,7 @@ final class ViewController: UIViewController, UIGestureRecognizerDelegate { } override func viewDidAppear(_ animated: Bool) { - let launchedBefore = UserDefaults.standard.bool(forKey: "ChineseTimeLaunchedBefore") + let launchedBefore = UserDefaults(suiteName: DataContainer.groupId)?.bool(forKey: "ChineseTimeLaunchedBefore") ?? false if !launchedBefore { let storyBoard = UIStoryboard(name: "Main", bundle: nil) let welcome = storyBoard.instantiateViewController(withIdentifier: "WelcomeView") as! WelcomeViewController @@ -136,7 +136,10 @@ final class WelcomeViewController: UIViewController { @IBOutlet var button: UIButton! @IBAction func close(_ sender: UIButton) { - UserDefaults.standard.set(true, forKey: "ChineseTimeLaunchedBefore") + if let userDefaults = UserDefaults(suiteName: DataContainer.groupId) { + userDefaults.set(true, forKey: "ChineseTimeLaunchedBefore") + userDefaults.synchronize() + } dismiss(animated: true) }