diff --git a/DataKernel/Classes/Contracts/Refs/StoreRef.swift b/DataKernel/Classes/Contracts/Refs/StoreRef.swift index ec3f98e..0b05ba1 100644 --- a/DataKernel/Classes/Contracts/Refs/StoreRef.swift +++ b/DataKernel/Classes/Contracts/Refs/StoreRef.swift @@ -9,17 +9,42 @@ import Foundation public enum StoreRef: Equatable { - case named(String) case url(URL) - + case named(String) + case namedInGroup(name: String, appGroup: String) + public func location() -> URL { switch self { - case .url(let url): return url - case .named(let name): return Foundation.URL(fileURLWithPath: FileUtils.documents()).appendingPathComponent(name) + case let .url(url): + return url + + case let .named(name): + return Foundation.URL(fileURLWithPath: FileUtils.documents()).appendingPathComponent(name) + + case let .namedInGroup(name, appGroup): + let containerUrl = FileUtils.documents(in: appGroup) + guard let dbDirUrl = containerUrl?.appendingPathComponent(dataKernelPath) else { + assert(false) + return StoreRef.named(name).location() + } + return dbDirUrl.appendingPathComponent(name) } - } + } + + // NOTE: If we move the store from a local directory to a shared one, we'd like to know where to migrate it from + func previousLocation() -> URL? { + switch self { + case let .namedInGroup(name, _): + return StoreRef.named(name).location() + + default: + return nil + } + } } public func == (lhs: StoreRef, rhs: StoreRef) -> Bool { return lhs.location() == rhs.location() } + +private let dataKernelPath = "DataKernel" diff --git a/DataKernel/Classes/Helpers/FileUtils.swift b/DataKernel/Classes/Helpers/FileUtils.swift index 243986d..11740b7 100644 --- a/DataKernel/Classes/Helpers/FileUtils.swift +++ b/DataKernel/Classes/Helpers/FileUtils.swift @@ -12,4 +12,8 @@ class FileUtils { static func documents() -> String { return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] } + + static func documents(in appGroup: String) -> URL? { + return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) + } } diff --git a/DataKernelTests/Classes/Helpers/FileUtilsTests.swift b/DataKernelTests/Classes/Helpers/FileUtilsTests.swift index d653cdb..712ab94 100644 --- a/DataKernelTests/Classes/Helpers/FileUtilsTests.swift +++ b/DataKernelTests/Classes/Helpers/FileUtilsTests.swift @@ -16,5 +16,20 @@ class FileUtilsTests: XCTestCase { XCTAssertEqual(path, NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0], "should return the proper documents directory") } - + +// NOTE: disabled because for using app groups there should be a capability with entitlements, but for public project it is impossible +// NOTE: you can test such thing in your own project +// func testProperPathInAppGroup() { +// guard let url = FileUtils.documents(in: "group") else { +// XCTAssert(false, "url should be set") +// return +// } +// +// guard let expected = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group") else { +// XCTAssert(false, "expected should be set") +// return +// } +// +// XCTAssertEqual(url, expected, "should return the proper documents directory in app group 'group'") +// } }