|
|
|
|
@ -19,10 +19,6 @@ protocol CollectionHolder { |
|
|
|
|
var items: [Item] { get } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
extension CollectionHolder { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protocol SomeCollection: Identifiable { |
|
|
|
|
var resourceName: String { get } |
|
|
|
|
var synchronized: Bool { get } |
|
|
|
|
@ -125,24 +121,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
return T.resourceName() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Paths |
|
|
|
|
|
|
|
|
|
// fileprivate func _storageDirectoryPath() throws -> URL { |
|
|
|
|
// return try FileUtils.pathForDirectoryInDocuments(directory: Store.storageDirectory) |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// fileprivate func _writeToStorageDirectory(content: String, fileName: String) throws { |
|
|
|
|
// var fileURL = try self._storageDirectoryPath() |
|
|
|
|
// fileURL.append(component: fileName) |
|
|
|
|
// try content.write(to: fileURL, atomically: false, encoding: .utf8) |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// fileprivate func _urlForJSONFile() throws -> URL { |
|
|
|
|
// var storageDirectory = try self._storageDirectoryPath() |
|
|
|
|
// storageDirectory.append(component: T.fileName()) |
|
|
|
|
// return storageDirectory |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
// MARK: - Loading |
|
|
|
|
|
|
|
|
|
/// Migrates if necessary and asynchronously decodes the json file |
|
|
|
|
@ -191,7 +169,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
// Logger.log("collection \(T.fileName()) has no file yet") |
|
|
|
|
self.loadCompletion?(self) |
|
|
|
|
NotificationCenter.default.post(name: NSNotification.Name.CollectionDidLoad, object: self) |
|
|
|
|
} |
|
|
|
|
@ -214,7 +191,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
do { |
|
|
|
|
let items: [T] = try await self._store.getItems() |
|
|
|
|
try self._addOrUpdate(contentOfs: items, shouldSync: false) |
|
|
|
|
// self._hasChanged = true |
|
|
|
|
self.hasLoadedFromServer = true |
|
|
|
|
NotificationCenter.default.post(name: NSNotification.Name.CollectionDidLoad, object: self) |
|
|
|
|
} catch { |
|
|
|
|
@ -356,10 +332,16 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
|
|
|
|
|
// MARK: - Some Collection |
|
|
|
|
|
|
|
|
|
/// Deletes an API Call by its id |
|
|
|
|
/// - Parameters: |
|
|
|
|
/// - id: the id of the API Call |
|
|
|
|
func deleteApiCallById(_ id: String) async throws { |
|
|
|
|
await self.apiCallsCollection?.deleteById(id) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns an API Call by its id |
|
|
|
|
/// - Parameters: |
|
|
|
|
/// - id: the id of the API Call |
|
|
|
|
func apiCallById(_ id: String) async -> (any SomeCall)? { |
|
|
|
|
return await self.apiCallsCollection?.findById(id) |
|
|
|
|
} |
|
|
|
|
@ -432,6 +414,8 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
// MARK: - Reschedule calls |
|
|
|
|
|
|
|
|
|
/// Sends an insert api call for the provided [instance] |
|
|
|
|
/// - Parameters: |
|
|
|
|
/// - instance: the object to POST |
|
|
|
|
fileprivate func _sendInsertionIfNecessary(_ instance: T) { |
|
|
|
|
guard self.synchronized, self._canSynchronise() else { |
|
|
|
|
return |
|
|
|
|
@ -442,6 +426,8 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sends an update api call for the provided [instance] |
|
|
|
|
/// - Parameters: |
|
|
|
|
/// - instance: the object to PUT |
|
|
|
|
fileprivate func _sendUpdateIfNecessary(_ instance: T) { |
|
|
|
|
guard self.synchronized, self._sendsUpdate, self._canSynchronise() else { |
|
|
|
|
return |
|
|
|
|
@ -452,6 +438,8 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sends an delete api call for the provided [instance] |
|
|
|
|
/// - Parameters: |
|
|
|
|
/// - instance: the object to DELETE |
|
|
|
|
fileprivate func _sendDeletionIfNecessary(_ instance: T) { |
|
|
|
|
guard self.synchronized, self._canSynchronise() else { |
|
|
|
|
return |
|
|
|
|
@ -461,6 +449,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns whether the collection can synchronize |
|
|
|
|
fileprivate func _canSynchronise() -> Bool { |
|
|
|
|
return Store.main.collectionsCanSynchronize && Store.main.userIsAllowed() |
|
|
|
|
} |
|
|
|
|
|