|
|
|
|
@ -19,9 +19,6 @@ public class StoreCenter { |
|
|
|
|
/// A KeychainStore object used to store the user's token |
|
|
|
|
var keychainStore: KeychainStore? = nil |
|
|
|
|
|
|
|
|
|
/// Indicates to Stored Collection if they can synchronize |
|
|
|
|
public var collectionsCanSynchronize: Bool = true |
|
|
|
|
|
|
|
|
|
/// Force the absence of synchronization |
|
|
|
|
public var forceNoSynchronization: Bool = false |
|
|
|
|
|
|
|
|
|
@ -287,7 +284,7 @@ public class StoreCenter { |
|
|
|
|
do { |
|
|
|
|
try await apiCallCollection.loadFromFile() |
|
|
|
|
let count = await apiCallCollection.items.count |
|
|
|
|
Logger.log("collection \(T.resourceName()) loaded with \(count)") |
|
|
|
|
// Logger.log("collection \(T.resourceName()) loaded with \(count)") |
|
|
|
|
await apiCallCollection.rescheduleApiCallsIfNecessary() |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
@ -378,10 +375,10 @@ public class StoreCenter { |
|
|
|
|
|
|
|
|
|
/// Retry API calls immediately |
|
|
|
|
fileprivate func _resumeApiCalls() { |
|
|
|
|
guard self.collectionsCanSynchronize else { |
|
|
|
|
guard self.forceNoSynchronization == false else { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
Logger.log("_resumeApiCalls") |
|
|
|
|
// Logger.log("_resumeApiCalls") |
|
|
|
|
Task { |
|
|
|
|
for collection in self._apiCallCollections.values { |
|
|
|
|
await collection.resumeApiCalls() |
|
|
|
|
@ -391,7 +388,7 @@ public class StoreCenter { |
|
|
|
|
|
|
|
|
|
/// Reschedule an ApiCall by id |
|
|
|
|
func rescheduleApiCalls<T: SyncedStorable>(type: T.Type) async throws { |
|
|
|
|
guard self.collectionsCanSynchronize else { |
|
|
|
|
guard self.forceNoSynchronization == false else { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
let collection: ApiCallCollection<T> = try self.apiCallCollection() |
|
|
|
|
@ -419,7 +416,8 @@ public class StoreCenter { |
|
|
|
|
|
|
|
|
|
/// Returns whether the collection can synchronize |
|
|
|
|
fileprivate func _canSynchronise() -> Bool { |
|
|
|
|
return !self.forceNoSynchronization && self.collectionsCanSynchronize |
|
|
|
|
return !self.forceNoSynchronization |
|
|
|
|
&& self.isAuthenticated |
|
|
|
|
&& self.userIsAllowed() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -465,8 +463,8 @@ public class StoreCenter { |
|
|
|
|
return try await self.service().get(identifier: identifier) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func itemsRetrieved<T: SyncedStorable>(_ results: [T], storeId: String?) async { |
|
|
|
|
await self._store(id: storeId).loadCollectionItems(results) |
|
|
|
|
func itemsRetrieved<T: SyncedStorable>(_ results: [T], storeId: String?, clear: Bool) async { |
|
|
|
|
await self._store(id: storeId).loadCollectionItems(results, clear: clear) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns the names of all collections |
|
|
|
|
@ -488,8 +486,8 @@ public class StoreCenter { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Loads all the data from the server for the users |
|
|
|
|
public func initialSynchronization() { |
|
|
|
|
Store.main.loadCollectionsFromServer() |
|
|
|
|
public func initialSynchronization(clear: Bool) { |
|
|
|
|
Store.main.loadCollectionsFromServer(clear: clear) |
|
|
|
|
|
|
|
|
|
// request data that has been shared with the user |
|
|
|
|
Task { |
|
|
|
|
@ -522,11 +520,13 @@ public class StoreCenter { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func sendGetRequest<T: SyncedStorable>(_ type: T.Type, storeId: String?) async throws { |
|
|
|
|
if self.canPerformGet(T.self) { |
|
|
|
|
let apiCallCollection: ApiCallCollection<T> = try self.apiCallCollection() |
|
|
|
|
try await apiCallCollection.sendGetRequest(storeId: storeId) |
|
|
|
|
func sendGetRequest<T: SyncedStorable>(_ type: T.Type, storeId: String?, clear: Bool) async throws { |
|
|
|
|
guard self._canSynchronise(), self.canPerformGet(T.self) else { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let apiCallCollection: ApiCallCollection<T> = try self.apiCallCollection() |
|
|
|
|
try await apiCallCollection.sendGetRequest(storeId: storeId, clear: clear) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func canPerformGet<T: SyncedStorable>(_ type: T.Type) -> Bool { |
|
|
|
|
@ -540,7 +540,8 @@ public class StoreCenter { |
|
|
|
|
let json = try JSONSerialization.jsonObject(with: data, options: []) |
|
|
|
|
as? [String: Any] |
|
|
|
|
else { |
|
|
|
|
Logger.w("data unrecognized") |
|
|
|
|
let string = String(data: data, encoding: .utf8) ?? "--" |
|
|
|
|
Logger.w("data unrecognized: \(string)") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
try await self._parseSyncUpdates(json, shared: true) |
|
|
|
|
|