Skip to content

Commit

Permalink
change file save/load area to Document that is more appropriate becau…
Browse files Browse the repository at this point in the history
…se favorite data aren't customizing app's behavior
  • Loading branch information
ewa1989 committed Mar 20, 2024
1 parent a7a611b commit 0e8b403
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions MyLibrary/Sources/DataClient/FileClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,34 @@ public struct FileClient {
extension FileClient: DependencyKey {
static public var liveValue: FileClient = .init(
loadFavorites: {
guard let saveData = loadDataFromUserDefaults(key: "Favorites") else {
guard let saveData = loadDataFromFile(named: "Favorites") else {
return .init(eachConferenceFavorites: [])
}
let response = try jsonDecoder.decode(Favorites.self, from: saveData)
return response
},
saveFavorites: { favorites in
saveDataToUserDefaults(favorites, as: "Favorites")
guard let data = try? jsonEncoder.encode(favorites) else {
return
}
saveDataToFile(data, named: "Favorites")
}
)

static func saveDataToUserDefaults(_ favorites : Favorites, as key: String) {
let data = try? jsonEncoder.encode(favorites)
UserDefaults.standard.set(data, forKey: key)
static func saveDataToFile(_ data : Data, named fileName: String) {
guard let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
return
}
let fileURL = documentPath.appendingPathComponent(fileName + ".json")
try? data.write(to: fileURL)
}

static func loadDataFromUserDefaults(key: String) -> Data? {
return UserDefaults.standard.data(forKey: key)
static func loadDataFromFile(named fileName: String) -> Data? {
guard let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
return nil
}
let fileURL = documentPath.appendingPathComponent(fileName + ".json")
return try? Data(contentsOf: fileURL)
}
}

Expand Down

0 comments on commit 0e8b403

Please sign in to comment.