Cleanup and document

multistore
Laurent 1 year ago
parent 2f493fe485
commit 3b9c12c868
  1. 2
      LeStorage/Services.swift
  2. 1
      LeStorage/Store.swift
  3. 37
      LeStorage/StoredCollection.swift

@ -56,6 +56,7 @@ public class Services {
Logger.log("create keystore with id: \(url)") Logger.log("create keystore with id: \(url)")
} }
/// The base API URL to send requests
fileprivate(set) var baseURL: String fileprivate(set) var baseURL: String
fileprivate var jsonEncoder: JSONEncoder = { fileprivate var jsonEncoder: JSONEncoder = {
@ -357,6 +358,7 @@ public class Services {
try self.keychainStore.deleteToken() try self.keychainStore.deleteToken()
} }
/// Returns whether the Service has an associated token
public func hasToken() -> Bool { public func hasToken() -> Bool {
do { do {
_ = try self.keychainStore.getToken() _ = try self.keychainStore.getToken()

@ -337,7 +337,6 @@ public class Store {
guard let userName = self.userName() else { guard let userName = self.userName() else {
return true return true
} }
let isAllowed = !self.blackListedUserName.contains(where: { $0 == userName } )
return !self.blackListedUserName.contains(where: { $0 == userName } ) return !self.blackListedUserName.contains(where: { $0 == userName } )
} }

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

Loading…
Cancel
Save