You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
LeStorage/LeStorage/StoredCollection.swift

502 lines
16 KiB

//
// StoredCollection.swift
// LeStorage
//
// Created by Laurent Morvillier on 02/02/2024.
//
import Foundation
enum StoredCollectionError: Error {
case unmanagedHTTPMethod(method: String)
case missingApiCallCollection
case missingInstance
}
protocol CollectionHolder {
associatedtype Item
var items: [Item] { get }
}
extension CollectionHolder {
}
protocol SomeCollection: Identifiable {
var resourceName: String { get }
var synchronized: Bool { get }
func allItems() -> [any Storable]
func deleteById(_ id: String) throws
func loadDataFromServerIfAllowed() async throws
func reset()
func resetApiCalls()
func deleteApiCallById(_ id: String) async throws
func apiCallById(_ id: String) async -> (any SomeCall)?
func hasPendingAPICalls() async -> Bool
func contentOfApiCallFile() async -> String?
}
extension Notification.Name {
public static let CollectionDidLoad: Notification.Name = Notification.Name.init("notification.collectionDidLoad")
public static let CollectionDidChange: Notification.Name = Notification.Name.init("notification.collectionDidChange")
}
public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollection, CollectionHolder {
/// If true, will synchronize the data with the provided server located at the Store's synchronizationApiURL
let synchronized: Bool
/// Doesn't write the collection in a file
fileprivate var _inMemory: Bool = false
/// Indicates if the synchronized collection sends update to the API
fileprivate var _sendsUpdate: Bool = true
/// The list of stored items
@Published public fileprivate(set) var items: [T] = []
/// The reference to the Store
fileprivate var _store: Store
/// Notifies the closure when the loading is done
fileprivate var loadCompletion: ((StoredCollection<T>) -> ())? = nil
/// Provides fast access for instances if the collection has been instanced with [indexed] = true
fileprivate var _indexes: [String : T]? = nil
/// Collection of API calls used to store HTTP calls
fileprivate var apiCallsCollection: ApiCallCollection<T>? = nil
/// Indicates whether the collection has changed, thus requiring a write operation
fileprivate var _hasChanged: Bool = false {
didSet {
if self._hasChanged == true {
self._scheduleWrite()
DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name.CollectionDidChange, object: self)
}
self._hasChanged = false
}
}
}
/// Denotes a collection that loads and writes asynchronously
fileprivate var asynchronousIO: Bool = true
/// Indicates if the collection has loaded objects from the server
fileprivate(set) public var hasLoadedFromServer: Bool = false
init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, inMemory: Bool = false, sendsUpdate: Bool = true, loadCompletion: ((StoredCollection<T>) -> ())? = nil) {
self.synchronized = synchronized
self.asynchronousIO = asynchronousIO
if indexed {
self._indexes = [:]
}
self._inMemory = inMemory
self._sendsUpdate = sendsUpdate
self._store = store
self.loadCompletion = loadCompletion
if synchronized {
let apiCallCollection = ApiCallCollection<T>(store: store)
self.apiCallsCollection = apiCallCollection
Task {
do {
try await apiCallCollection.loadFromFile()
} catch {
Logger.error(error)
}
}
}
self._load()
}
var resourceName: String {
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
fileprivate func _load() {
do {
if self._inMemory {
Task {
try await self.loadDataFromServerIfAllowed()
}
} else {
try self._loadFromFile()
}
} catch {
Logger.error(error)
}
}
/// Starts the JSON file decoding synchronously or asynchronously
fileprivate func _loadFromFile() throws {
if self.asynchronousIO {
Task(priority: .high) {
try self._decodeJSONFile()
}
} else {
try self._decodeJSONFile()
}
}
/// Decodes the json file into the items array
fileprivate func _decodeJSONFile() throws {
let fileURL = try T.urlForJSONFile()
if FileManager.default.fileExists(atPath: fileURL.path()) {
let jsonString: String = try FileUtils.readFile(fileURL: fileURL)
let decoded: [T] = try jsonString.decodeArray() ?? []
DispatchQueue.main.async {
Logger.log("loaded \(T.fileName()) with \(decoded.count) items")
self.items = decoded
self._updateIndexIfNecessary()
self.loadCompletion?(self)
NotificationCenter.default.post(name: NSNotification.Name.CollectionDidLoad, object: self)
}
} 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)
}
}
}
/// Updates the whole index with the items array
fileprivate func _updateIndexIfNecessary() {
if let _ = self._indexes {
self._indexes = self.items.dictionary { $0.stringId }
}
}
/// Retrieves the data from the server and loads it into the items array
public func loadDataFromServerIfAllowed() async throws {
guard self.synchronized, !(self is StoredSingleton<T>) else {
throw StoreError.unSynchronizedCollection
}
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 {
Logger.error(error)
}
}
// MARK: - Basic operations
/// Adds or updates the provided instance inside the collection
/// Adds it if its id is not found, and otherwise updates it
public func addOrUpdate(instance: T) throws {
defer {
self._hasChanged = true
}
// update
if let index = self.items.firstIndex(where: { $0.id == instance.id }) {
self.items[index] = instance
self._sendUpdateIfNecessary(instance)
} else { // insert
self.items.append(instance)
self._sendInsertionIfNecessary(instance)
}
self._indexes?[instance.stringId] = instance
}
public func writeChangeAndInsertOnServer(instance: T) {
defer {
self._hasChanged = true
}
self._sendInsertionIfNecessary(instance)
}
/// A method the treat the collection as a single instance holder
func setSingletonNoSync(instance: T) {
defer {
self._hasChanged = true
}
self.items.removeAll()
self.items.append(instance)
}
/// Deletes the instance in the collection by id
public func delete(instance: T) throws {
defer {
self._hasChanged = true
}
try instance.deleteDependencies()
self.items.removeAll { $0.id == instance.id }
self._indexes?.removeValue(forKey: instance.stringId)
self._sendDeletionIfNecessary(instance)
}
/// Deletes all items of the sequence by id
public func delete(contentOfs sequence: any Sequence<T>) throws {
defer {
self._hasChanged = true
}
for instance in sequence {
try instance.deleteDependencies()
self.items.removeAll { $0.id == instance.id }
self._indexes?.removeValue(forKey: instance.stringId)
self._sendDeletionIfNecessary(instance)
}
}
/// Adds or update a sequence of elements
public func addOrUpdate(contentOfs sequence: any Sequence<T>) throws {
try self._addOrUpdate(contentOfs: sequence)
}
/// Inserts or updates all items in the sequence
fileprivate func _addOrUpdate(contentOfs sequence: any Sequence<T>, shouldSync: Bool = true) throws {
defer {
self._hasChanged = true
}
for instance in sequence {
if let index = self.items.firstIndex(where: { $0.id == instance.id }) {
self.items[index] = instance
if shouldSync {
self._sendUpdateIfNecessary(instance)
}
} else { // insert
self.items.append(instance)
if shouldSync {
self._sendInsertionIfNecessary(instance)
}
}
self._indexes?[instance.stringId] = instance
}
}
/// Returns the instance corresponding to the provided [id]
public func findById(_ id: String) -> T? {
if let index = self._indexes, let instance = index[id] {
return instance
}
return self.items.first(where: { $0.id == id })
}
/// Deletes the instance corresponding to the provided [id]
public func deleteById(_ id: String) throws {
if let instance = self.findById(id) {
try self.delete(instance: instance)
}
}
/// Proceeds to "hard" delete the items without synchronizing them
/// Also removes related API calls
public func deleteDependencies(_ items: any Sequence<T>) {
defer {
self._hasChanged = true
}
for item in items {
self.items.removeAll(where: { $0.id == item.id })
Task {
/// Remove related API call if existing
await self.apiCallsCollection?.deleteByDataId(item.stringId)
}
}
}
/// Proceeds to delete all instance of the collection, properly cleaning up dependencies and sending API calls
public func deleteAll() throws {
try self.delete(contentOfs: self.items)
}
// MARK: - Some Collection
func deleteApiCallById(_ id: String) async throws {
await self.apiCallsCollection?.deleteById(id)
}
func apiCallById(_ id: String) async -> (any SomeCall)? {
return await self.apiCallsCollection?.findById(id)
}
// MARK: - SomeCall
/// Returns the collection items as [any Storable]
func allItems() -> [any Storable] {
return self.items
}
// MARK: - File access
/// Schedules a write operation
fileprivate func _scheduleWrite() {
guard !self._inMemory else { return }
if self.asynchronousIO {
DispatchQueue(label: "lestorage.queue.write", qos: .utility).asyncAndWait { // sync to make sure we don't have writes performed at the same time
self._write()
}
} else {
self._write()
}
}
/// Writes all the items as a json array inside a file
fileprivate func _write() {
Logger.log("Start write to \(T.fileName())...")
do {
let jsonString: String = try self.items.jsonString()
try T.writeToStorageDirectory(content: jsonString, fileName: T.fileName())
} catch {
Logger.error(error) // TODO how to notify the main project
}
Logger.log("End write")
}
/// Simply clears the items of the collection
func clear() {
self.items.removeAll()
}
/// Removes the items of the collection, deletes the corresponding file, and also reset the related API calls collection
public func reset() {
self.items.removeAll()
do {
let url: URL = try T.urlForJSONFile()
if FileManager.default.fileExists(atPath: url.path()) {
try FileManager.default.removeItem(at: url)
}
} catch {
Logger.error(error)
}
self.resetApiCalls()
}
/// Removes the collection related API calls collection
public func resetApiCalls() {
if let apiCallsCollection = self.apiCallsCollection {
Task {
await apiCallsCollection.reset()
}
}
}
// MARK: - Reschedule calls
/// Sends an insert api call for the provided [instance]
fileprivate func _sendInsertionIfNecessary(_ instance: T) {
guard self.synchronized, Store.main.collectionsCanSynchronize else {
return
}
Task {
await self.apiCallsCollection?.sendInsertion(instance)
}
}
/// Sends an update api call for the provided [instance]
fileprivate func _sendUpdateIfNecessary(_ instance: T) {
guard self.synchronized, self._sendsUpdate, Store.main.collectionsCanSynchronize else {
return
}
Task {
await self.apiCallsCollection?.sendUpdate(instance)
}
}
/// Sends an delete api call for the provided [instance]
fileprivate func _sendDeletionIfNecessary(_ instance: T) {
guard self.synchronized, Store.main.collectionsCanSynchronize else {
return
}
Task {
await self.apiCallsCollection?.sendDeletion(instance)
}
}
/// Reschedule the api calls if possible
func rescheduleApiCallsIfNecessary() {
Task {
await self.apiCallsCollection?.rescheduleApiCallsIfNecessary()
}
}
/// Returns the content of the API call file as a String
func contentOfApiCallFile() async -> String? {
return await self.apiCallsCollection?.contentOfApiCallFile()
}
/// Returns if the API call collection is not empty
func hasPendingAPICalls() async -> Bool {
guard let apiCallsCollection else { return false }
return await apiCallsCollection.items.isNotEmpty
}
// MARK: - RandomAccessCollection
public var startIndex: Int { return self.items.startIndex }
public var endIndex: Int { return self.items.endIndex }
public func index(after i: Int) -> Int {
return self.items.index(after: i)
}
open subscript(index: Int) -> T {
get {
return self.items[index]
}
set(newValue) {
self.items[index] = newValue
self._hasChanged = true
}
}
}