-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add protocols for dependencies (#114)
- Loading branch information
Showing
6 changed files
with
84 additions
and
58 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
Sources/AppState/Application/Types/Helper/FileManaging.swift
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,32 @@ | ||
/// A protocol that provides methods for reading, writing, and deleting files in a type-safe, sendable manner. | ||
public protocol FileManaging: Sendable { | ||
|
||
/// Reads a file from the given path and decodes its contents into the specified type. | ||
/// - Parameters: | ||
/// - path: The directory path where the file is located. Defaults to the current directory `"."`. | ||
/// - filename: The name of the file to read. | ||
/// - Returns: The decoded value of the file's content as the specified type. | ||
/// - Throws: An error if the file cannot be found or decoded. | ||
func `in`<Value: Decodable>(path: String, filename: String) throws -> Value | ||
|
||
/// Encodes and writes the given value to a file at the specified path. | ||
/// - Parameters: | ||
/// - value: The value to encode and write to the file. It must conform to `Encodable`. | ||
/// - path: The directory path where the file will be written. Defaults to the current directory `"."`. | ||
/// - filename: The name of the file to write. | ||
/// - base64Encoded: Whether to encode the content as Base64. Defaults to `true`. | ||
/// - Throws: An error if the file cannot be written. | ||
func `out`<Value: Encodable>(_ value: Value, path: String, filename: String, base64Encoded: Bool) throws | ||
|
||
/// Deletes a file at the specified path. | ||
/// - Parameters: | ||
/// - path: The directory path where the file is located. Defaults to the current directory `"."`. | ||
/// - filename: The name of the file to delete. | ||
/// - Throws: An error if the file cannot be deleted. | ||
func `delete`(path: String, filename: String) throws | ||
|
||
/// Removes a file or directory at the specified path. | ||
/// - Parameter path: The full path of the file or directory to remove. | ||
/// - Throws: An error if the item cannot be removed. | ||
func removeItem(atPath path: String) throws | ||
} |
23 changes: 23 additions & 0 deletions
23
Sources/AppState/Application/Types/Helper/UbiquitousKeyValueStoreManaging.swift
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,23 @@ | ||
#if !os(Linux) && !os(Windows) | ||
import Foundation | ||
|
||
/// A protocol that provides a thread-safe interface for interacting with `NSUbiquitousKeyValueStore`, | ||
/// which synchronizes key-value data across the user's iCloud-enabled devices. | ||
public protocol UbiquitousKeyValueStoreManaging: Sendable { | ||
|
||
/// Retrieves data stored in iCloud for the specified key. | ||
/// - Parameter key: The key used to retrieve the associated data from the `NSUbiquitousKeyValueStore`. | ||
/// - Returns: The `Data` object associated with the key, or `nil` if no data is found. | ||
func data(forKey key: String) -> Data? | ||
|
||
/// Sets a `Data` object for the specified key in iCloud's key-value store. | ||
/// - Parameters: | ||
/// - value: The `Data` object to store. Pass `nil` to remove the data associated with the key. | ||
/// - key: The key with which to associate the data. | ||
func set(_ value: Data?, forKey key: String) | ||
|
||
/// Removes the value associated with the specified key from iCloud's key-value store. | ||
/// - Parameter key: The key whose associated value should be removed. | ||
func removeObject(forKey key: String) | ||
} | ||
#endif |
19 changes: 19 additions & 0 deletions
19
Sources/AppState/Application/Types/Helper/UserDefaultsManaging.swift
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,19 @@ | ||
/// A protocol that provides a thread-safe interface for interacting with `UserDefaults`, | ||
/// allowing the storage, retrieval, and removal of user preferences and data. | ||
public protocol UserDefaultsManaging: Sendable { | ||
|
||
/// Retrieves an object from `UserDefaults` for the given key. | ||
/// - Parameter key: The key used to retrieve the associated value from `UserDefaults`. | ||
/// - Returns: The value stored in `UserDefaults` for the given key, or `nil` if no value is associated with the key. | ||
func object(forKey key: String) -> Any? | ||
|
||
/// Removes the value associated with the specified key from `UserDefaults`. | ||
/// - Parameter key: The key whose associated value should be removed. | ||
func removeObject(forKey key: String) | ||
|
||
/// Sets the value for the specified key in `UserDefaults`. | ||
/// - Parameters: | ||
/// - value: The value to store in `UserDefaults`. Can be `nil` to remove the value associated with the key. | ||
/// - key: The key with which to associate the value. | ||
func set(_ value: Any?, forKey key: String) | ||
} |
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
20 changes: 4 additions & 16 deletions
20
Sources/AppState/Application/Types/State/Application+StoredState.swift
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