Skip to content

Commit

Permalink
Add persistent object storage (#5)
Browse files Browse the repository at this point in the history
* Add persistent object storage

* Add remove persistent value function

* Do persistent object storage a better way

* Fix tests

* Remove code that is no longer needed

* PR Update Sources/GKStorageKit/Storage/ObjectStorageService.swift

* PR Update Sources/GKStorageKit/Storage/ObjectStorageService.swift

Co-authored-by: Gligor Kotushevski <[email protected]>
  • Loading branch information
CrunchieDCD and gligorkot authored Nov 23, 2022
1 parent a0b5e5b commit a2ead58
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sources/GKStorageKit/Default Impl/DefaultStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public final class DefaultStorage: StorageProtocol {
public var storageIdentifier: String {
return "defaultStorage"
}

public var persistentStorageIdentifier: String {
return "persistentStorage"
}

public var secureStorage: SecureStorage {
guard let valetIdentifier = Identifier(nonEmpty: storageIdentifier) else {
Expand All @@ -27,6 +31,10 @@ public final class DefaultStorage: StorageProtocol {
public var userDefaults: UserDefaults {
return UserDefaults(suiteName: storageIdentifier)!
}

public var persistentUserDefaults: UserDefaults {
return UserDefaults(suiteName: persistentStorageIdentifier)!
}

public var fileStorageUrl: URL {
// documents storage is the default fileStorageUrl
Expand Down
2 changes: 2 additions & 0 deletions Sources/GKStorageKit/Protocol/StorageProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import Foundation

public protocol StorageProtocol {
var storageIdentifier: String { get }
var persistentStorageIdentifier: String { get }
var secureStorage: SecureStorage { get }
var userDefaults: UserDefaults { get }
var persistentUserDefaults: UserDefaults { get }
var fileStorageUrl: URL { get }
}
4 changes: 4 additions & 0 deletions Sources/GKStorageKit/StorageKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public final class StorageKit {
public class var objectStorage: ObjectStorageInterface {
return ObjectStorageService(storage: StorageKitConfiguration.shared.storage.userDefaults)
}

public class var persistentObjectStorage: ObjectStorageInterface {
return ObjectStorageService(storage: StorageKitConfiguration.shared.storage.persistentUserDefaults)
}

public class var fileStorage: FileStorageInterface {
return FileStorageService(documentsUrl: StorageKitConfiguration.shared.storage.fileStorageUrl)
Expand Down
16 changes: 16 additions & 0 deletions Tests/GKStorageKitTests/Test Helpers/TestStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ final class TestStorage: StorageProtocol {
var storageIdentifier: String {
return "testStorage"
}

var persistentStorageIdentifier: String {
return "testPersistentStorage"
}

var secureStorage: SecureStorage {
return Valet.iCloudValet(with: Identifier(nonEmpty: storageIdentifier)!, accessibility: .whenUnlocked)
Expand All @@ -23,6 +27,10 @@ final class TestStorage: StorageProtocol {
var userDefaults: UserDefaults {
return UserDefaults(suiteName: storageIdentifier)!
}

var persistentUserDefaults: UserDefaults {
return UserDefaults(suiteName: persistentStorageIdentifier)!
}

var fileStorageUrl: URL {
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
Expand All @@ -34,6 +42,10 @@ final class ErrorStorage: StorageProtocol {
var storageIdentifier: String {
return "errorTestStorage"
}

var persistentStorageIdentifier: String {
return "errorTestPersistentStorage"
}

var secureStorage: SecureStorage {
return ErrorValet()
Expand All @@ -42,6 +54,10 @@ final class ErrorStorage: StorageProtocol {
var userDefaults: UserDefaults {
return UserDefaults(suiteName: storageIdentifier)!
}

var persistentUserDefaults: UserDefaults {
return UserDefaults(suiteName: persistentStorageIdentifier)!
}

var fileStorageUrl: URL {
return URL(string: "errorUrl")!
Expand Down

0 comments on commit a2ead58

Please sign in to comment.