Skip to content

Commit

Permalink
Using URL static properties for directories on iOS 16/macOS 13
Browse files Browse the repository at this point in the history
  • Loading branch information
mergesort committed Aug 24, 2023
1 parent d7110ab commit b8468ee
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions Sources/Bodega/FileManager.Directory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,40 @@ public extension FileManager.Directory {
/// Returns a URL to a subfolder created in the documents directory based on the `pathComponent`.
/// - Parameter pathComponent: A path to append to the platform's documents directory.
static func documents(appendingPath pathComponent: String) -> Self {
self.init(
url: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent)
)
let url: URL
if #available(iOS 16.0, macOS 13.0, *) {
url = URL.documentsDirectory.appending(path: pathComponent)
} else {
url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent)
}

return self.init(url: url)
}

/// Returns a URL to a subfolder created in the caches directory based on the `pathComponent`.
/// - Parameter pathComponent: A path to append to the platform's caches directory.
static func caches(appendingPath pathComponent: String) -> Self {
self.init(
url: FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent)
)
let url: URL
if #available(iOS 16.0, macOS 13.0, *) {
url = URL.cachesDirectory.appending(path: pathComponent)
} else {
url = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent)
}

return self.init(url: url)
}

/// Returns a URL to a subfolder created in the temporary directory based on the `pathComponent`.
/// - Parameter pathComponent: A path to append to the platform's temporary directory.
static func temporary(appendingPath pathComponent: String) -> Self {
self.init(
url: FileManager.default.temporaryDirectory.appendingPathComponent(pathComponent)
)
let url: URL
if #available(iOS 16.0, macOS 13.0, *) {
url = URL.temporaryDirectory.appending(path: pathComponent)
} else {
url = FileManager.default.temporaryDirectory.appendingPathComponent(pathComponent)
}

return self.init(url: url)
}

/// For apps that use the App Groups feature this function returns a URL that
Expand All @@ -61,12 +76,18 @@ public extension FileManager.Directory {
)
}

@available(macOS 11, *)
/// Returns a URL to a subfolder created in the Application Support directory based on the `pathComponent`.
/// - pathComponent: A path to append to the app's group shared directory.
#if os(macOS)
static func applicationSupport(appendingPath pathComponent: String) -> Self {
self.init(
url: FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent)
)
let url: URL
if #available(macOS 13.0, *) {
url = URL.applicationSupportDirectory.appending(path: pathComponent)
} else {
url = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!.appendingPathComponent(pathComponent)
}

return self.init(url: url)
}
#endif
}

0 comments on commit b8468ee

Please sign in to comment.