Skip to content

Commit

Permalink
Add static URL
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Apr 10, 2024
1 parent c250337 commit 16146b9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Sources/UserSettingsClient/UserSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ public struct UserSettings: Codable, Equatable {
(try? container.decode(Bool.self, forKey: .sendDailyChallengeSummary)) ?? true
}
}

extension URL {
public static let userSettings = Self.documentsDirectory
.appendingPathComponent("user-settings")
.appendingPathExtension("json")
}
11 changes: 2 additions & 9 deletions Sources/UserSettingsClient/UserSettingsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ public struct UserSettingsClient {

extension UserSettingsClient: DependencyKey {
public static var liveValue: UserSettingsClient {
let userSettingsFileURL = FileManager.default
.urls(for: .documentDirectory, in: .userDomainMask)
.first!
.appendingPathComponent(userSettingsFileName)
.appendingPathExtension("json")
let initialUserSettingsData = (try? Data(contentsOf: userSettingsFileURL)) ?? Data()
let initialUserSettingsData = (try? Data(contentsOf: .userSettings)) ?? Data()
let initialUserSettings =
(try? JSONDecoder().decode(UserSettings.self, from: initialUserSettingsData))
?? UserSettings()
Expand All @@ -49,7 +44,7 @@ extension UserSettingsClient: DependencyKey {
userSettings.withValue {
$0 = updatedUserSettings
subject.send(updatedUserSettings)
try? JSONEncoder().encode(updatedUserSettings).write(to: userSettingsFileURL)
try? JSONEncoder().encode(updatedUserSettings).write(to: .userSettings)
}
},
stream: {
Expand Down Expand Up @@ -84,5 +79,3 @@ extension DependencyValues {
set { self[UserSettingsClient.self] = newValue }
}
}

public let userSettingsFileName = "user-settings"

0 comments on commit 16146b9

Please sign in to comment.