Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Storage/Classes/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public protocol KeyValueStorable: AnyObject {
func synchronize() -> Bool
}

#if canImport(Darwin)
extension NSUbiquitousKeyValueStore: KeyValueStorable {}
#endif

/// A class that provides a simple way to store and retrieve Codable objects.
/// The `Storage` class supports different storage types such as cache, document, and user defaults.
Expand All @@ -24,13 +26,22 @@ public final class Storage<T> where T: Codable {
/// - Parameters:
/// - storageType: The type of storage to use (cache, document, or user defaults).
/// - filename: The name of the file to store the data.
/// - ubiquitousStore: Optional KeyValueStorable instance for .ubiquitousKeyValueStore type (defaults to NSUbiquitousKeyValueStore.default)
/// - ubiquitousStore: Optional KeyValueStorable instance for .ubiquitousKeyValueStore type (defaults to platform specific store if available)
#if canImport(Darwin)
public init(storageType: StorageType, filename: String, ubiquitousStore: KeyValueStorable? = NSUbiquitousKeyValueStore.default) {
self.ubiquitousStore = ubiquitousStore
self.type = storageType
self.filename = filename
createFolderIfNotExists()
}
#else
public init(storageType: StorageType, filename: String, ubiquitousStore: KeyValueStorable? = nil) {
self.ubiquitousStore = ubiquitousStore
self.type = storageType
self.filename = filename
createFolderIfNotExists()
}
#endif

/// Saves the given object to the specified storage type.
///
Expand Down Expand Up @@ -116,7 +127,7 @@ public final class Storage<T> where T: Codable {

try? FileManager.default.removeItem(at: folder)
}
try? fileManager.createDirectory(at: folder, withIntermediateDirectories: false, attributes: nil)
try? fileManager.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
}

/// Clears the stored data from the specified storage type.
Expand Down
6 changes: 4 additions & 2 deletions Tests/StorageTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ class StorageTypeTests: XCTestCase {
func testCacheDirectory() {
let storageType = StorageType.cache
let folder = storageType.folder
XCTAssertTrue(folder.path.contains("Caches"), "Cache directory path should contain 'Caches'")
let expected = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
XCTAssertTrue(folder.path.hasPrefix(expected), "Cache directory should be inside the system caches directory")
}

func testDocumentDirectory() {
let storageType = StorageType.document
let folder = storageType.folder
XCTAssertTrue(folder.path.contains("Documents"), "Document directory path should contain 'Documents'")
let expected = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
XCTAssertTrue(folder.path.hasPrefix(expected), "Document directory should be inside the system documents directory")
}
}