Skip to content

Commit

Permalink
Add defaultFileStatePath
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLeif committed Jan 31, 2024
1 parent e0a5680 commit a8fd39c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Sources/AppState/Application/Application+public.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ public extension Application {
*/
func fileState<Value>(
initial: @escaping @autoclosure () -> Value,
path: String = "~/App",
path: String = FileManager.defaultFileStatePath,
filename: String
) -> FileState<Value> {
FileState(
Expand All @@ -1088,7 +1088,7 @@ public extension Application {
- Returns: The state of type `Value`.
*/
func fileState<Value>(
path: String = "~/App",
path: String = FileManager.defaultFileStatePath,
filename: String
) -> FileState<Value?> {
fileState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ extension FileManager {
var errorDescription: String? { rawValue }
}

public static var defaultFileStatePath: String {
guard let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
return "~/App"
}

#if !os(Linux) && !os(Windows)
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
return "\(path.path())/App"
} else {
return "\(path.path)/App"
}
#else
return "\(path.path)/App"
#endif
}

/// Creates a directory at the specified path.
///
/// - Parameters:
Expand Down
2 changes: 1 addition & 1 deletion Tests/AppStateTests/FileStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fileprivate extension Application {
}

var storedString: FileState<String?> {
fileState(path: "./AppStateTests", filename: "storedstring")
fileState(filename: "storedstring")
}
}

Expand Down

0 comments on commit a8fd39c

Please sign in to comment.