Adds method to delete all collections

multistore
Laurent 2 years ago
parent ba9a311965
commit c2bffaa559
  1. 4
      LeStorage/MicroStorage.swift
  2. 6
      LeStorage/Store.swift
  3. 10
      LeStorage/StoredCollection.swift
  4. 49
      LeStorage/Utils/FileUtils.swift

@ -19,7 +19,7 @@ public class MicroStorage<T : MicroStorable> {
init() { init() {
var instance: T? = nil var instance: T? = nil
do { do {
let url = try FileUtils.documentDirectoryURLForFileName(T.fileName) let url = try FileUtils.pathForFileInDocumentDirectory(T.fileName)
if FileManager.default.fileExists(atPath: url.absoluteString) { if FileManager.default.fileExists(atPath: url.absoluteString) {
let jsonString = try FileUtils.readDocumentFile(fileName: T.fileName) let jsonString = try FileUtils.readDocumentFile(fileName: T.fileName)
if let decoded: T = try jsonString.decode() { if let decoded: T = try jsonString.decode() {
@ -62,7 +62,7 @@ public class OptionalStorage<T : Codable> {
public init(fileName: String) { public init(fileName: String) {
self.fileName = fileName self.fileName = fileName
do { do {
let url = try FileUtils.documentDirectoryURLForFileName(fileName) let url = try FileUtils.pathForFileInDocumentDirectory(fileName)
if FileManager.default.fileExists(atPath: url.path) { if FileManager.default.fileExists(atPath: url.path) {
let jsonString = try FileUtils.readDocumentFile(fileName: fileName) let jsonString = try FileUtils.readDocumentFile(fileName: fileName)
if let decoded: T = try jsonString.decode() { if let decoded: T = try jsonString.decode() {

@ -238,4 +238,10 @@ public class Store {
return try await service.get() return try await service.get()
} }
public func reset() {
for collection in self._collections.values {
collection.reset()
}
}
} }

@ -17,6 +17,7 @@ protocol SomeCollection: Identifiable {
func allItems() -> [any Storable] func allItems() -> [any Storable]
func deleteById(_ id: String) throws func deleteById(_ id: String) throws
func deleteApiCallById(_ id: String) throws func deleteApiCallById(_ id: String) throws
func reset()
} }
extension Notification.Name { extension Notification.Name {
@ -82,7 +83,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
/// Migrates if necessary and asynchronously decodes the json file /// Migrates if necessary and asynchronously decodes the json file
fileprivate func _load() { fileprivate func _load() {
do { do {
let url = try FileUtils.documentDirectoryURLForFileName(T.fileName()) let url = try FileUtils.pathForFileInDocumentDirectory(T.fileName())
if FileManager.default.fileExists(atPath: url.path()) { if FileManager.default.fileExists(atPath: url.path()) {
if self.asynchronousIO { if self.asynchronousIO {
@ -245,6 +246,13 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
Logger.log("End write") Logger.log("End write")
} }
func reset() {
try? FileUtils.removeFileFromDocumentDirectory(fileName: T.fileName())
if let apiCallsCollection = self.apiCallsCollection {
apiCallsCollection.reset()
}
}
// MARK: - Synchronization // MARK: - Synchronization
fileprivate func _callForInstance(_ instance: T, method: Method) throws -> ApiCall<T>? { fileprivate func _callForInstance(_ instance: T, method: Method) throws -> ApiCall<T>? {

@ -19,23 +19,15 @@ class FileUtils {
} }
static func readDocumentFile(fileName: String) throws -> String { static func readDocumentFile(fileName: String) throws -> String {
let fileURL: URL = try self.documentDirectoryURLForFileName(fileName) let fileURL: URL = try self.pathForFileInDocumentDirectory(fileName)
// Logger.log("url = \(fileURL.absoluteString)")
return try String(contentsOf: fileURL, encoding: .utf8) return try String(contentsOf: fileURL, encoding: .utf8)
// if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
// let fileURL: URL = dir.appendingPathComponent(fileName)
// Logger.log("url = \(fileURL.absoluteString)")
// return try String(contentsOf: fileURL, encoding: .utf8)
// }
// throw FileError.documentDirectoryNotFound
} }
static func readFile(fileURL: URL) throws -> String { static func readFile(fileURL: URL) throws -> String {
return try String(contentsOf: fileURL, encoding: .utf8) return try String(contentsOf: fileURL, encoding: .utf8)
} }
static func documentDirectoryURLForFileName(_ fileName: String) throws -> URL { static func pathForFileInDocumentDirectory(_ fileName: String) throws -> URL {
if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
return dir.appendingPathComponent(fileName) return dir.appendingPathComponent(fileName)
} }
@ -43,23 +35,34 @@ class FileUtils {
} }
static func writeToDocumentDirectory(content: String, fileName: String) throws -> URL { static func writeToDocumentDirectory(content: String, fileName: String) throws -> URL {
let fileURL = try self.pathForFileInDocumentDirectory(fileName)
try content.write(to: fileURL, atomically: false, encoding: .utf8)
return fileURL
if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { // if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL: URL = dir.appendingPathComponent(fileName) // let fileURL: URL = dir.appendingPathComponent(fileName)
try content.write(to: fileURL, atomically: false, encoding: .utf8) // try content.write(to: fileURL, atomically: false, encoding: .utf8)
return fileURL // return fileURL
} // }
throw FileError.documentDirectoryNotFound // throw FileError.documentDirectoryNotFound
} }
@discardableResult static func writeToDocumentDirectory(data: Data, fileName: String) throws -> URL { @discardableResult static func writeToDocumentDirectory(data: Data, fileName: String) throws -> URL {
let fileURL = try self.pathForFileInDocumentDirectory(fileName)
if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { try data.write(to: fileURL)
let fileURL: URL = dir.appendingPathComponent(fileName) return fileURL
try data.write(to: fileURL)
return fileURL // if let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
} // let fileURL: URL = dir.appendingPathComponent(fileName)
throw FileError.documentDirectoryNotFound // try data.write(to: fileURL)
// return fileURL
// }
// throw FileError.documentDirectoryNotFound
}
static func removeFileFromDocumentDirectory(fileName: String) throws {
let fileURL = try self.pathForFileInDocumentDirectory(fileName)
try FileManager.default.removeItem(atPath: fileURL.path())
} }
} }

Loading…
Cancel
Save