Skip to content

Commit

Permalink
Use group container
Browse files Browse the repository at this point in the history
  • Loading branch information
LEOYoon-Tsaw committed May 25, 2023
1 parent d435f5b commit 49a7747
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Chinese Time.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 18 additions & 5 deletions Shared/Delegates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -203,7 +208,11 @@ final class DataContainer: ObservableObject {
try? persistentContainer.viewContext.setQueryGenerationFrom(.current)
let managedContext = persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(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
{
Expand Down Expand Up @@ -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<NSManagedObject>(entityName: "Layout")
Expand Down
7 changes: 5 additions & 2 deletions Watch/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
Expand Down
7 changes: 5 additions & 2 deletions iOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 49a7747

Please sign in to comment.