From db0602dcf7d31049fe1533648fb0888450327057 Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 2 Jul 2024 18:38:02 +0200 Subject: [PATCH] Adds load state --- LeStorage/Store.swift | 4 ++++ LeStorage/StoredCollection.swift | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index e2952bd..6eadb7b 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -253,6 +253,10 @@ open class Store { } } + public func collectionsAllLoaded() -> Bool { + return self._collections.values.allSatisfy { $0.hasLoadedLocally } + } + fileprivate var _validIds: [String] = [] fileprivate func _migrate(_ collection: StoredCollection, identifier: StoreIdentifier, type: T.Type) { diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index ca9514b..0fad05b 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -23,6 +23,7 @@ protocol CollectionHolder { protocol SomeCollection: CollectionHolder, Identifiable { var resourceName: String { get } var synchronized: Bool { get } + var hasLoadedLocally: Bool { get } func allItems() -> [any Storable] @@ -72,7 +73,10 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti /// Denotes a collection that loads and writes asynchronously fileprivate var asynchronousIO: Bool = true - + + /// Indicates if the collection has loaded locally, with or without a file + fileprivate(set) public var hasLoadedLocally: Bool = false + /// Indicates if the collection has loaded objects from the server fileprivate(set) public var hasLoadedFromServer: Bool = false @@ -136,6 +140,9 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti /// Decodes the json file into the items array fileprivate func _decodeJSONFile() throws { + + defer { self.hasLoadedLocally = true } + let fileURL = try self._store.fileURL(type: T.self) if FileManager.default.fileExists(atPath: fileURL.path()) { @@ -228,6 +235,7 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } + /// Sends a POST request for the instance, and changes the collection to perform a write public func writeChangeAndInsertOnServer(instance: T) { defer { self._hasChanged = true