From 10455f8715b2de2acca9880f2478c282632bfe50 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 31 May 2024 16:33:34 +0200 Subject: [PATCH] Fixes --- LeStorage/Store.swift | 6 ++++++ LeStorage/StoredCollection.swift | 28 +++++++++++++++++----------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index f089de3..e2df548 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -243,6 +243,12 @@ public class Store { } } + public func resetApiCalls() { + for collection in self._collections.values { + collection.resetApiCalls() + } + } + /// Loads all collection with the data from the server public func loadCollectionFromServer() { for collection in self._collections.values { diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index 31347ab..65b5b07 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -14,15 +14,20 @@ enum StoredCollectionError: Error { } protocol SomeCollection: Identifiable { + var resourceName: String { get } + var synchronized: Bool { get } + func allItems() -> [any Storable] func deleteById(_ id: String) throws func deleteApiCallById(_ id: String) throws - func reset() + func loadDataFromServerIfAllowed() async throws - var synchronized: Bool { get } + func hasPendingAPICalls() -> Bool - var resourceName: String { get } func contentOfApiCallFile() -> String? + + func reset() + func resetApiCalls() } extension Notification.Name { @@ -403,17 +408,18 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } if let existingCall = apiCallCollection.first(where: { $0.dataId == instance.id }) { - switch existingCall.method { - case HTTPMethod.post.rawValue, HTTPMethod.put.rawValue: + switch method { + case .delete: + try self.deleteApiCallById(existingCall.id) // delete the existing call as we don't need it + if existingCall.method == HTTPMethod.post.rawValue { + return nil // if the post has not been done, we can just stop here + } else { + return try self._createCall(instance, method: method) // otherwise it's a put and we want to send the delete + } + default: // here we should only trying to PUT, so we update the existing POST/PUT with the instance new values existingCall.body = try instance.jsonString() return existingCall - case HTTPMethod.delete.rawValue: - try self.deleteApiCallById(existingCall.id) - return nil - default: - throw StoredCollectionError.unmanagedHTTPMethod(method: existingCall.method) } - } else { return try self._createCall(instance, method: method) }