This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
store the model used by the UI for rendering
We are using a flat file on the system, which is working but should be replaced with either db solution like SQLite[0], Realm[1], Sync[2], etc. I am doing it this way to save some time for other topics. Also added updated tests to check this and use new class names. Totally missed the naming breakage earlier... [0]: https://www.sqlite.org/index.html [1]: https://github.com/realm/realm-cocoa [2]: https://github.com/3lvis/Sync Closes: #9 (persist minimal data required to present the something)
- Loading branch information
Showing
6 changed files
with
95 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// StorageManager.swift | ||
// cantera | ||
// | ||
// Created by Alexander Alemayhu on 14/11/2018. | ||
// Copyright © 2018 Alexander Alemayhu. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
class StorageManager { | ||
|
||
private let persistedFilePath: String = { | ||
let dir = NSHomeDirectory() | ||
return "\(dir)/cached_payload.json" | ||
}() | ||
|
||
func persist (ads: [AdObject]) { | ||
let url = URL(fileURLWithPath: persistedFilePath) | ||
let encoder = JSONEncoder() | ||
do { | ||
let data = try encoder.encode(ads) | ||
try data.write(to: url, options: .atomic) | ||
print("Everyting should at \(persistedFilePath)") | ||
} catch { | ||
fatalError(error.localizedDescription) | ||
} | ||
} | ||
|
||
func savedAds() -> [AdObject]? { | ||
let url = URL(fileURLWithPath: persistedFilePath) | ||
do { | ||
let data = try Data(contentsOf: url) | ||
let ads = try JSONDecoder().decode([AdObject].self, from: data) | ||
return ads | ||
} catch { | ||
return nil | ||
} | ||
} | ||
|
||
func purge() { | ||
let url = URL(fileURLWithPath: persistedFilePath) | ||
do { | ||
try FileManager.default.removeItem(at: url) | ||
} catch { | ||
print("\(#function): \(error)") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters