|
|
|
|
@ -17,21 +17,21 @@ public enum StoreError: Error { |
|
|
|
|
case cannotSyncCollection(name: String) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public struct StoreIdentifier { |
|
|
|
|
var value: String |
|
|
|
|
var parameterName: String |
|
|
|
|
|
|
|
|
|
public init(value: String, parameterName: String) { |
|
|
|
|
self.value = value |
|
|
|
|
self.parameterName = parameterName |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var urlComponent: String { |
|
|
|
|
return "?\(self.parameterName)=\(self.value)" |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//public struct StoreIdentifier { |
|
|
|
|
// var value: String |
|
|
|
|
// var parameterName: String |
|
|
|
|
// |
|
|
|
|
// public init(value: String, parameterName: String) { |
|
|
|
|
// self.value = value |
|
|
|
|
// self.parameterName = parameterName |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// var urlComponent: String { |
|
|
|
|
// return "?\(self.parameterName)=\(self.value)" |
|
|
|
|
// } |
|
|
|
|
//} |
|
|
|
|
|
|
|
|
|
open class Store { |
|
|
|
|
final public class Store { |
|
|
|
|
|
|
|
|
|
/// The Store singleton |
|
|
|
|
public static let main = Store() |
|
|
|
|
@ -43,7 +43,7 @@ open class Store { |
|
|
|
|
static let storageDirectory = "storage" |
|
|
|
|
|
|
|
|
|
/// The store identifier, used to name the store directory, and to perform filtering requests to the server |
|
|
|
|
fileprivate(set) var identifier: StoreIdentifier? = nil |
|
|
|
|
fileprivate(set) var identifier: String? = nil |
|
|
|
|
|
|
|
|
|
/// Indicates whether the store directory has been created at the init |
|
|
|
|
fileprivate var _created: Bool = false |
|
|
|
|
@ -52,8 +52,8 @@ open class Store { |
|
|
|
|
self._createDirectory(directory: Store.storageDirectory) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public required init(identifier: String, parameter: String) { |
|
|
|
|
self.identifier = StoreIdentifier(value: identifier, parameterName: parameter) |
|
|
|
|
public required init(identifier: String) { |
|
|
|
|
self.identifier = identifier |
|
|
|
|
let directory = "\(Store.storageDirectory)/\(identifier)" |
|
|
|
|
self._createDirectory(directory: directory) |
|
|
|
|
} |
|
|
|
|
@ -145,6 +145,16 @@ open class Store { |
|
|
|
|
throw StoreError.collectionNotRegistered(type: T.resourceName()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
subscript<T>(_ type: T.Type) -> StoredCollection<T> { |
|
|
|
|
get { |
|
|
|
|
do { |
|
|
|
|
return try self.collection() |
|
|
|
|
} catch { |
|
|
|
|
fatalError(error.localizedDescription) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Loads all collection with the data from the server |
|
|
|
|
public func loadCollectionsFromServer() { |
|
|
|
|
for collection in self._syncedCollections() { |
|
|
|
|
@ -216,7 +226,7 @@ open class Store { |
|
|
|
|
/// Returns the directory URL of the store |
|
|
|
|
fileprivate func _directoryPath() throws -> URL { |
|
|
|
|
var url = try FileUtils.pathForDirectoryInDocuments(directory: Store.storageDirectory) |
|
|
|
|
if let identifier = self.identifier?.value { |
|
|
|
|
if let identifier { |
|
|
|
|
url.append(component: identifier) |
|
|
|
|
} |
|
|
|
|
return url |
|
|
|
|
@ -289,42 +299,4 @@ open class Store { |
|
|
|
|
return self._collections.values.allSatisfy { $0.hasLoaded } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// fileprivate var _validIds: [String] = [] |
|
|
|
|
// |
|
|
|
|
// fileprivate func _migrate<T : Storable>(_ collection: StoredCollection<T>, identifier: StoreIdentifier, type: T.Type) { |
|
|
|
|
// |
|
|
|
|
// self._validIds.append(identifier.value) |
|
|
|
|
// |
|
|
|
|
// let oldCollection: StoredCollection<T> = StoredCollection<T>(synchronized: false, store: Store.main, asynchronousIO: false) |
|
|
|
|
// |
|
|
|
|
// let filtered: [T] = oldCollection.items.filter { item in |
|
|
|
|
// var propertyValue: String? = item.stringForPropertyName(identifier.parameterName) |
|
|
|
|
// if propertyValue == nil { |
|
|
|
|
// let values = T.relationshipNames.map { item.stringForPropertyName($0) } |
|
|
|
|
// propertyValue = values.compactMap { $0 }.first |
|
|
|
|
// } |
|
|
|
|
// return self._validIds.first(where: { $0 == propertyValue }) != nil |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// if filtered.count > 0 { |
|
|
|
|
// self._validIds.append(contentsOf: filtered.map { $0.stringId }) |
|
|
|
|
// try? collection.addOrUpdateNoSync(contentOfs: filtered) |
|
|
|
|
// Logger.log("Migrated \(filtered.count) \(T.resourceName())") |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//fileprivate extension Storable { |
|
|
|
|
// |
|
|
|
|
// func stringForPropertyName(_ propertyName: String) -> String? { |
|
|
|
|
// let mirror = Mirror(reflecting: self) |
|
|
|
|
// for child in mirror.children { |
|
|
|
|
// if let label = child.label, label == "_\(propertyName)" { |
|
|
|
|
// return child.value as? String |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// return nil |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
//} |
|
|
|
|
|