From e23c81306c52da374348c2011593514fb4176c8e Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 19 Jun 2024 16:00:09 +0200 Subject: [PATCH] Adds blacklist system for sync --- LeStorage/Store.swift | 14 ++++++++++++++ LeStorage/StoredCollection.swift | 10 +++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index ece0ab4..5bb20cb 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -71,6 +71,8 @@ public class Store { fileprivate var _failedAPICallsCollection: StoredCollection? = nil + fileprivate var blackListedUserName: [String] = [] + public init() { FileManager.default.createDirectoryInDocuments(directoryName: Store.storageDirectory) } @@ -327,4 +329,16 @@ public class Store { } + public func blackListUserName(_ userName: String) { + self.blackListedUserName.append(userName) + } + + func userIsAllowed() -> Bool { + guard let userName = self.userName() else { + return true + } + let isAllowed = !self.blackListedUserName.contains(where: { $0 == userName } ) + return !self.blackListedUserName.contains(where: { $0 == userName } ) + } + } diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index c14558e..d300e68 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -433,7 +433,7 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti /// Sends an insert api call for the provided [instance] fileprivate func _sendInsertionIfNecessary(_ instance: T) { - guard self.synchronized, Store.main.collectionsCanSynchronize else { + guard self.synchronized, self._canSynchronise() else { return } Task { @@ -443,7 +443,7 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti /// Sends an update api call for the provided [instance] fileprivate func _sendUpdateIfNecessary(_ instance: T) { - guard self.synchronized, self._sendsUpdate, Store.main.collectionsCanSynchronize else { + guard self.synchronized, self._sendsUpdate, self._canSynchronise() else { return } Task { @@ -453,7 +453,7 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti /// Sends an delete api call for the provided [instance] fileprivate func _sendDeletionIfNecessary(_ instance: T) { - guard self.synchronized, Store.main.collectionsCanSynchronize else { + guard self.synchronized, self._canSynchronise() else { return } Task { @@ -461,6 +461,10 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } } + fileprivate func _canSynchronise() -> Bool { + return Store.main.collectionsCanSynchronize && Store.main.userIsAllowed() + } + /// Reschedule the api calls if possible func rescheduleApiCallsIfNecessary() { Task {