diff --git a/LeStorage/Codables/Settings.swift b/LeStorage/Codables/Settings.swift index 6b68ba4..cf35de2 100644 --- a/LeStorage/Codables/Settings.swift +++ b/LeStorage/Codables/Settings.swift @@ -16,6 +16,6 @@ class Settings: MicroStorable { var userId: String? = nil var username: String? = nil var deviceId: String? = nil - var lastSynchronization: Date? = nil + var lastSynchronization: Date = Date() } diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index 960fb93..91853ee 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -319,8 +319,9 @@ final public class Store { } /// Returns whether all collections have loaded locally - public func collectionsAllLoaded() -> Bool { - return self._collections.values.allSatisfy { $0.hasLoaded } + public func fileCollectionsAllLoaded() -> Bool { + let fileCollections = self._collections.values.filter { $0.inMemory == false } + return fileCollections.allSatisfy { $0.hasLoaded } } } diff --git a/LeStorage/StoreCenter.swift b/LeStorage/StoreCenter.swift index 4194f04..e979d55 100644 --- a/LeStorage/StoreCenter.swift +++ b/LeStorage/StoreCenter.swift @@ -195,7 +195,7 @@ public class StoreCenter { self._settingsStorage.update { settings in settings.username = nil settings.userId = nil - settings.lastSynchronization = nil + settings.lastSynchronization = Date() self._webSocketManager = nil } @@ -413,22 +413,17 @@ public class StoreCenter { func synchronizeLastUpdates() async throws { - if let lastSync = self._settingsStorage.item.lastSynchronization { - - let syncGetCollection: ApiCallCollection = try self.apiCallCollection() - if await syncGetCollection.hasPendingCalls() { - await syncGetCollection.rescheduleApiCallsIfNecessary() - } else { - let getSyncData = GetSyncData() - getSyncData.lastUpdate = lastSync - try await syncGetCollection.sendGetRequest(instance: getSyncData) - } + let lastSync = self._settingsStorage.item.lastSynchronization + + let syncGetCollection: ApiCallCollection = try self.apiCallCollection() + if await syncGetCollection.hasPendingCalls() { + await syncGetCollection.rescheduleApiCallsIfNecessary() } else { - Logger.w("Can't sync due to missing saved date") + let getSyncData = GetSyncData() + getSyncData.lastUpdate = lastSync + try await syncGetCollection.sendGetRequest(instance: getSyncData) } -// let lastSync: Date? = self._settingsStorage.item.lastSynchronization -// try await self._services?.synchronizeLastUpdates(since: lastSync) } func userDataAccessRetrieved(_ data: Data) { @@ -473,7 +468,7 @@ public class StoreCenter { } if let revocations = json["revocations"] as? [String: Any] { - try self._parseSyncRevocations(revocations, parents: json["revocation_parents"] as? [String: Any]) + try self._parseSyncRevocations(revocations, parents: json["revocation_parents"] as? [[String: Any]]) } if let dateString = json["date"] as? String, @@ -538,7 +533,7 @@ public class StoreCenter { } } - fileprivate func _parseSyncRevocations(_ deletions: [String: Any], parents: [String: Any]?) throws { + fileprivate func _parseSyncRevocations(_ deletions: [String: Any], parents: [[String: Any]]?) throws { for (className, revocationData) in deletions { guard let revokedItems = revocationData as? [Any] else { Logger.w("Invalid update data for \(className)") @@ -556,22 +551,23 @@ public class StoreCenter { } if let parents { - for (className, parentData) in parents { - guard let parentItems = parentData as? [Any] else { - Logger.w("Invalid update data for \(className): \(parentData)") - continue - } - for parentItem in parentItems { - do { - let data = try JSONSerialization.data(withJSONObject: parentItem, options: []) - let revokedObject = try JSON.decoder.decode(ObjectIdentifier.self, from: data) - StoreCenter.main.synchronizationRevoke(id: revokedObject.modelId, model: className, storeId: revokedObject.storeId) - } catch { - Logger.error(error) + for level in parents { + for (className, parentData) in level { + guard let parentItems = parentData as? [Any] else { + Logger.w("Invalid update data for \(className): \(parentData)") + continue + } + for parentItem in parentItems { + do { + let data = try JSONSerialization.data(withJSONObject: parentItem, options: []) + let revokedObject = try JSON.decoder.decode(ObjectIdentifier.self, from: data) + StoreCenter.main.synchronizationRevoke(id: revokedObject.modelId, model: className, storeId: revokedObject.storeId) + } catch { + Logger.error(error) + } } } } - } } diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index 2b6aed7..bf2f540 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -17,6 +17,7 @@ protocol CollectionHolder { protocol SomeCollection: CollectionHolder, Identifiable { var resourceName: String { get } var hasLoaded: Bool { get } + var inMemory: Bool { get } func allItems() -> [any Storable] func referenceCount(type: S.Type, id: String) -> Int