From 787d0d736676b0ffb851c9f22f09241c4ef24a28 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 11 Apr 2025 09:50:08 +0200 Subject: [PATCH 1/8] adds tryPutBeforeUpdating method --- LeStorage/StoredSingleton.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/LeStorage/StoredSingleton.swift b/LeStorage/StoredSingleton.swift index 89f439d..2f3435e 100644 --- a/LeStorage/StoredSingleton.swift +++ b/LeStorage/StoredSingleton.swift @@ -27,6 +27,13 @@ public class StoredSingleton: SyncedCollection { return self.items.first } + public func tryPutBeforeUpdating(_ instance: T) async throws { + if let result = try await StoreCenter.main.service().put(instance) { + self.setItemNoSync(result) + } + } + + // MARK: - Protects from use public override func addOrUpdate(contentOfs sequence: any Sequence) { From e4dac9ff43b2c576ff57a6b482654acc7ff747ef Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 11 Apr 2025 11:12:16 +0200 Subject: [PATCH 2/8] Fixes issue with copyServerResponse --- LeStorage/Codables/DataAccess.swift | 1 + LeStorage/Codables/FailedAPICall.swift | 3 ++- LeStorage/Codables/GetSyncData.swift | 3 ++- LeStorage/Codables/Log.swift | 1 + LeStorage/ModelObject.swift | 4 ---- LeStorage/SyncedStorable.swift | 2 -- 6 files changed, 6 insertions(+), 8 deletions(-) diff --git a/LeStorage/Codables/DataAccess.swift b/LeStorage/Codables/DataAccess.swift index ab428ce..f791c62 100644 --- a/LeStorage/Codables/DataAccess.swift +++ b/LeStorage/Codables/DataAccess.swift @@ -12,6 +12,7 @@ class DataAccess: SyncedModelObject, SyncedStorable { static func tokenExemptedMethods() -> [HTTPMethod] { return [] } static func resourceName() -> String { return "data-access" } static func relationships() -> [Relationship] { return [] } + static var copyServerResponse: Bool = false override required init() { super.init() diff --git a/LeStorage/Codables/FailedAPICall.swift b/LeStorage/Codables/FailedAPICall.swift index 53786dd..6b340bd 100644 --- a/LeStorage/Codables/FailedAPICall.swift +++ b/LeStorage/Codables/FailedAPICall.swift @@ -8,10 +8,11 @@ import Foundation class FailedAPICall: SyncedModelObject, SyncedStorable { - + static func resourceName() -> String { return "failed-api-calls" } static func tokenExemptedMethods() -> [HTTPMethod] { return [] } static func relationships() -> [Relationship] { return [] } + static var copyServerResponse: Bool = false override required init() { self.callId = "" diff --git a/LeStorage/Codables/GetSyncData.swift b/LeStorage/Codables/GetSyncData.swift index 7d246b8..a8378fe 100644 --- a/LeStorage/Codables/GetSyncData.swift +++ b/LeStorage/Codables/GetSyncData.swift @@ -20,7 +20,8 @@ class GetSyncData: SyncedModelObject, SyncedStorable, URLParameterConvertible { } static func tokenExemptedMethods() -> [HTTPMethod] { return [] } - + static var copyServerResponse: Bool = false + static func resourceName() -> String { return "sync-data" } diff --git a/LeStorage/Codables/Log.swift b/LeStorage/Codables/Log.swift index 5a223ac..ab9f2a7 100644 --- a/LeStorage/Codables/Log.swift +++ b/LeStorage/Codables/Log.swift @@ -12,6 +12,7 @@ class Log: SyncedModelObject, SyncedStorable { static func resourceName() -> String { return "logs" } static func tokenExemptedMethods() -> [HTTPMethod] { return [] } static func relationships() -> [Relationship] { return [] } + static var copyServerResponse: Bool = false override required init() { super.init() diff --git a/LeStorage/ModelObject.swift b/LeStorage/ModelObject.swift index f7f5160..e13f03f 100644 --- a/LeStorage/ModelObject.swift +++ b/LeStorage/ModelObject.swift @@ -53,10 +53,6 @@ open class SyncedModelObject: BaseModelObject { public var lastUpdate: Date = Date() public var shared: Bool? - open func copyFromServerInstance(_ instance: any Storable) -> Bool { - return false - } - public override init() { super.init() } diff --git a/LeStorage/SyncedStorable.swift b/LeStorage/SyncedStorable.swift index f997542..8c98f2e 100644 --- a/LeStorage/SyncedStorable.swift +++ b/LeStorage/SyncedStorable.swift @@ -32,8 +32,6 @@ public protocol SideStorable { extension SyncedStorable { - public static var copyServerResponse: Bool { return false } - func copy() -> Self { let copy = Self() copy.copy(from: self) From 044900fc1dee0c7901c1f27f47be0960d1142d5d Mon Sep 17 00:00:00 2001 From: Raz Date: Fri, 11 Apr 2025 21:22:11 +0200 Subject: [PATCH 3/8] add refund system --- LeStorage/Services.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LeStorage/Services.swift b/LeStorage/Services.swift index 90b6f99..a1971f3 100644 --- a/LeStorage/Services.swift +++ b/LeStorage/Services.swift @@ -238,7 +238,7 @@ public class Services { /// - method: the HTTP method to execute /// - requiresToken: An optional boolean to indicate if the token is required /// - identifier: an optional StoreIdentifier that allows to filter GET requests with the StoreIdentifier values - fileprivate func _baseRequest( + public func _baseRequest( servicePath: String, method: HTTPMethod, requiresToken: Bool? = nil, identifier: String? = nil, getArguments: [String : String]? = nil ) throws -> URLRequest { From 1ed6a9a295289c36a333354de258ef20f9ae49f2 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 14 Apr 2025 10:43:44 +0200 Subject: [PATCH 4/8] Fix tryPutBeforeUpdating to graciously fail --- LeStorage/Services.swift | 12 ++++++------ LeStorage/StoredSingleton.swift | 9 ++++----- LeStorage/SyncedStorable.swift | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/LeStorage/Services.swift b/LeStorage/Services.swift index 90b6f99..b38cbe3 100644 --- a/LeStorage/Services.swift +++ b/LeStorage/Services.swift @@ -496,22 +496,22 @@ public class Services { return try await self._runRequest(getRequest) } - /// Executes a POST request - - public func post(_ instance: T) async throws -> T { + /// Executes a POST request on the generated DRF services corresponding to T + public func rawPost(_ instance: T) async throws -> T { var postRequest = try self._postRequest(type: T.self) postRequest.httpBody = try JSON.encoder.encode(instance) return try await self._runRequest(postRequest) } - /// Executes a PUT request - public func put(_ instance: T) async throws -> T { + /// Executes a PUT request on the generated DRF services corresponding to T + public func rawPut(_ instance: T) async throws -> T { var postRequest = try self._putRequest(type: T.self, id: instance.stringId) postRequest.httpBody = try JSON.encoder.encode(instance) return try await self._runRequest(postRequest) } - public func delete(_ instance: T) async throws -> T { + /// Executes a DELETE request on the generated DRF services corresponding to T + public func rawDelete(_ instance: T) async throws -> T { let deleteRequest = try self._deleteRequest(type: T.self, id: instance.stringId) return try await self._runRequest(deleteRequest) } diff --git a/LeStorage/StoredSingleton.swift b/LeStorage/StoredSingleton.swift index 2f3435e..459da1c 100644 --- a/LeStorage/StoredSingleton.swift +++ b/LeStorage/StoredSingleton.swift @@ -27,13 +27,12 @@ public class StoredSingleton: SyncedCollection { return self.items.first } - public func tryPutBeforeUpdating(_ instance: T) async throws { - if let result = try await StoreCenter.main.service().put(instance) { - self.setItemNoSync(result) - } + @discardableResult public func tryPutBeforeUpdating(_ instance: T) async throws -> T? { + let result = try await StoreCenter.main.service().rawPut(instance) + self.setItemNoSync(result) + return result } - // MARK: - Protects from use public override func addOrUpdate(contentOfs sequence: any Sequence) { diff --git a/LeStorage/SyncedStorable.swift b/LeStorage/SyncedStorable.swift index 8c98f2e..53094be 100644 --- a/LeStorage/SyncedStorable.swift +++ b/LeStorage/SyncedStorable.swift @@ -30,7 +30,7 @@ public protocol SideStorable { var storeId: String? { get set } } -extension SyncedStorable { +public extension SyncedStorable { func copy() -> Self { let copy = Self() From 4686a42a80210ac1bf6d642258124b904120daad Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 14 Apr 2025 11:04:32 +0200 Subject: [PATCH 5/8] Fix issue with tryPutBeforeUpdating --- LeStorage/StoredSingleton.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/LeStorage/StoredSingleton.swift b/LeStorage/StoredSingleton.swift index 459da1c..35dc82d 100644 --- a/LeStorage/StoredSingleton.swift +++ b/LeStorage/StoredSingleton.swift @@ -27,10 +27,11 @@ public class StoredSingleton: SyncedCollection { return self.items.first } - @discardableResult public func tryPutBeforeUpdating(_ instance: T) async throws -> T? { + public func tryPutBeforeUpdating(_ instance: T) async throws { let result = try await StoreCenter.main.service().rawPut(instance) - self.setItemNoSync(result) - return result + if let item = self.item() { + item.copy(from: result) + } } // MARK: - Protects from use From 5570309d6eb02883d6f3e6bce76dc26962f4d588 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 14 Apr 2025 11:05:54 +0200 Subject: [PATCH 6/8] adds missing write on tryPutBeforeUpdating --- LeStorage/StoredSingleton.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/LeStorage/StoredSingleton.swift b/LeStorage/StoredSingleton.swift index 35dc82d..2f59e94 100644 --- a/LeStorage/StoredSingleton.swift +++ b/LeStorage/StoredSingleton.swift @@ -31,6 +31,7 @@ public class StoredSingleton: SyncedCollection { let result = try await StoreCenter.main.service().rawPut(instance) if let item = self.item() { item.copy(from: result) + self.addOrUpdate(instance: item) } } From 16e38f79fbb6b2393c6271f9531b46ac749058be Mon Sep 17 00:00:00 2001 From: Raz Date: Tue, 15 Apr 2025 08:59:52 +0200 Subject: [PATCH 7/8] fix online payment stuff --- LeStorage/Utils/Codable+Extensions.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/LeStorage/Utils/Codable+Extensions.swift b/LeStorage/Utils/Codable+Extensions.swift index 5fd14e7..5294b6b 100644 --- a/LeStorage/Utils/Codable+Extensions.swift +++ b/LeStorage/Utils/Codable+Extensions.swift @@ -7,9 +7,9 @@ import Foundation -class JSON { +public class JSON { - static var encoder: JSONEncoder = { + public static var encoder: JSONEncoder = { let encoder = JSONEncoder() encoder.keyEncodingStrategy = .convertToSnakeCase #if DEBUG @@ -23,7 +23,7 @@ class JSON { return encoder }() - static var decoder: JSONDecoder = { + public static var decoder: JSONDecoder = { let decoder = JSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase decoder.dateDecodingStrategy = .custom { decoder in From 28239c1110e68626c6f08226ecd2a2da2f0156f7 Mon Sep 17 00:00:00 2001 From: Raz Date: Thu, 24 Apr 2025 09:40:27 +0200 Subject: [PATCH 8/8] fix memory leak --- LeStorage/Utils/Date+Extensions.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/LeStorage/Utils/Date+Extensions.swift b/LeStorage/Utils/Date+Extensions.swift index c3d81e6..41f6c98 100644 --- a/LeStorage/Utils/Date+Extensions.swift +++ b/LeStorage/Utils/Date+Extensions.swift @@ -9,19 +9,19 @@ import Foundation extension Date { - static var iso8601Formatter: ISO8601DateFormatter { + static var iso8601Formatter: ISO8601DateFormatter = { let iso8601Formatter = ISO8601DateFormatter() iso8601Formatter.timeZone = TimeZone(abbreviation: "CET") iso8601Formatter.formatOptions = [.withInternetDateTime, .withTimeZone] return iso8601Formatter - } + }() - public static var iso8601FractionalFormatter: ISO8601DateFormatter { + public static var iso8601FractionalFormatter: ISO8601DateFormatter = { let iso8601Formatter = ISO8601DateFormatter() iso8601Formatter.timeZone = TimeZone(abbreviation: "CET") iso8601Formatter.formatOptions = [.withInternetDateTime, .withTimeZone, .withFractionalSeconds] return iso8601Formatter - } + }() public static var microSecondFormatter: DateFormatter = { let formatter = DateFormatter()