From 66d23489f73e11855296420fa490daca661616ea Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 3 May 2024 16:29:53 +0200 Subject: [PATCH] Adds StoreObject class to store a single item --- LeStorage.xcodeproj/project.pbxproj | 8 ++++++-- LeStorage/Store.swift | 28 ++++++++++++++++++++++------ LeStorage/StoredCollection.swift | 1 + LeStorage/StoredObject.swift | 27 +++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 LeStorage/StoredObject.swift diff --git a/LeStorage.xcodeproj/project.pbxproj b/LeStorage.xcodeproj/project.pbxproj index c4e47b2..3647d37 100644 --- a/LeStorage.xcodeproj/project.pbxproj +++ b/LeStorage.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ C425D4442B6D24E1002A7B48 /* LeStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C425D4432B6D24E1002A7B48 /* LeStorageTests.swift */; }; C425D4452B6D24E1002A7B48 /* LeStorage.h in Headers */ = {isa = PBXBuildFile; fileRef = C425D4372B6D24E1002A7B48 /* LeStorage.h */; settings = {ATTRIBUTES = (Public, ); }; }; C425D4582B6D2519002A7B48 /* Store.swift in Sources */ = {isa = PBXBuildFile; fileRef = C425D4572B6D2519002A7B48 /* Store.swift */; }; + C456EFE22BE52379007388E2 /* StoredObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = C456EFE12BE52379007388E2 /* StoredObject.swift */; }; C49EF0242BD6BDC50077B5AA /* FileManager+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C49EF0232BD6BDC50077B5AA /* FileManager+Extensions.swift */; }; C4A47D4F2B6D280200ADC637 /* StoredCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A47D4E2B6D280200ADC637 /* StoredCollection.swift */; }; C4A47D512B6D2C4E00ADC637 /* Codable+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A47D502B6D2C4E00ADC637 /* Codable+Extensions.swift */; }; @@ -47,6 +48,7 @@ C425D43E2B6D24E1002A7B48 /* LeStorageTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = LeStorageTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; C425D4432B6D24E1002A7B48 /* LeStorageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LeStorageTests.swift; sourceTree = ""; }; C425D4572B6D2519002A7B48 /* Store.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Store.swift; sourceTree = ""; }; + C456EFE12BE52379007388E2 /* StoredObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoredObject.swift; sourceTree = ""; }; C49EF0232BD6BDC50077B5AA /* FileManager+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileManager+Extensions.swift"; sourceTree = ""; }; C4A47D4E2B6D280200ADC637 /* StoredCollection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoredCollection.swift; sourceTree = ""; }; C4A47D502B6D2C4E00ADC637 /* Codable+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Codable+Extensions.swift"; sourceTree = ""; }; @@ -114,6 +116,7 @@ C425D4572B6D2519002A7B48 /* Store.swift */, C4A47D642B6E92FE00ADC637 /* Storable.swift */, C4A47D4E2B6D280200ADC637 /* StoredCollection.swift */, + C456EFE12BE52379007388E2 /* StoredObject.swift */, C4A47D932B7CF7C500ADC637 /* MicroStorage.swift */, C4A47D822B7665BC00ADC637 /* Wip */, C4A47D582B6D352900ADC637 /* Utils */, @@ -277,6 +280,7 @@ C4A47DAF2B85FD3800ADC637 /* Errors.swift in Sources */, C4A47D612B6D3C1300ADC637 /* Services.swift in Sources */, C4A47D552B6D2DBF00ADC637 /* FileUtils.swift in Sources */, + C456EFE22BE52379007388E2 /* StoredObject.swift in Sources */, C4A47D652B6E92FE00ADC637 /* Storable.swift in Sources */, C4A47D6D2B71364600ADC637 /* ModelObject.swift in Sources */, C4A47D4F2B6D280200ADC637 /* StoredCollection.swift in Sources */, @@ -362,7 +366,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 16.4; + IPHONEOS_DEPLOYMENT_TARGET = 17.2; LOCALIZATION_PREFERS_STRING_CATALOGS = YES; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; @@ -422,7 +426,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 16.4; + IPHONEOS_DEPLOYMENT_TARGET = 17.2; LOCALIZATION_PREFERS_STRING_CATALOGS = YES; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index 14deaab..41f5bdd 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -40,11 +40,6 @@ public class Store { /// The services performing the API calls fileprivate var _services: Services? - /// The service instance -// public var service: Services? { -// return self._services -// } - public func service() throws -> Services { if let service = self._services { return service @@ -92,6 +87,15 @@ public class Store { return collection } + public func registerObject(synchronized: Bool, inMemory: Bool = false, sendsUpdate: Bool = true) -> StoredObject { + + // register collection + let storedObject = StoredObject(synchronized: synchronized, store: Store.main, inMemory: inMemory, sendsUpdate: sendsUpdate, loadCompletion: nil) + self._collections[T.resourceName()] = storedObject + + return storedObject + } + // MARK: - Settings func setUserUUID(uuidString: String) { @@ -123,12 +127,18 @@ public class Store { } } - public func disconnect() { + public func disconnect(resetAll: Bool = false) { try? self.service().disconnect() self.settingsStorage.update { settings in settings.username = nil settings.userId = nil } + + if resetAll { + for collection in self._collections.values { + collection.reset() + } + } } public func hasToken() -> Bool { @@ -255,4 +265,10 @@ public class Store { } } + public func loadCollections() { + for collection in self._collections.values { + try? collection.loadDataFromServer() + } + } + } diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index 8ec2c94..c1c8726 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -18,6 +18,7 @@ protocol SomeCollection: Identifiable { func deleteById(_ id: String) throws func deleteApiCallById(_ id: String) throws func reset() + func loadDataFromServer() throws } extension Notification.Name { diff --git a/LeStorage/StoredObject.swift b/LeStorage/StoredObject.swift new file mode 100644 index 0000000..1882583 --- /dev/null +++ b/LeStorage/StoredObject.swift @@ -0,0 +1,27 @@ +// +// StoredObject.swift +// LeStorage +// +// Created by Laurent Morvillier on 03/05/2024. +// + +import Foundation + +public class StoredObject: StoredCollection { + + public func setItem(_ instance: T) throws { + self.reset() + try self.addOrUpdate(instance: instance) + } + + public func update() throws { + if let item = self.item() { + try self.addOrUpdate(instance: item) + } + } + + public func item() -> T? { + return self.items.first + } + +}