|
|
|
|
@ -14,13 +14,14 @@ public protocol MicroStorable : Codable { |
|
|
|
|
|
|
|
|
|
public class MicroStorage<T : MicroStorable> { |
|
|
|
|
|
|
|
|
|
fileprivate(set) var item: T |
|
|
|
|
public fileprivate(set) var item: T |
|
|
|
|
|
|
|
|
|
init() { |
|
|
|
|
public init() { |
|
|
|
|
var instance: T? = nil |
|
|
|
|
do { |
|
|
|
|
let url = try FileUtils.pathForFileInDocumentDirectory(T.fileName) |
|
|
|
|
if FileManager.default.fileExists(atPath: url.absoluteString) { |
|
|
|
|
let fileExists = FileManager.default.fileExists(atPath: url.path()) |
|
|
|
|
if fileExists { |
|
|
|
|
let jsonString = try FileUtils.readDocumentFile(fileName: T.fileName) |
|
|
|
|
if let decoded: T = try jsonString.decode() { |
|
|
|
|
instance = decoded |
|
|
|
|
@ -33,7 +34,7 @@ public class MicroStorage<T : MicroStorable> { |
|
|
|
|
self.item = instance ?? T() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func update(handler: (T) -> ()) { |
|
|
|
|
public func update(handler: (T) -> ()) { |
|
|
|
|
handler(self.item) |
|
|
|
|
self._write() |
|
|
|
|
} |
|
|
|
|
|