Add option to manually provide directory for state

This commit is contained in:
david-swift 2024-01-03 11:22:21 +01:00
parent da29f92c91
commit 03d259811b
5 changed files with 26 additions and 6 deletions

View File

@ -13,6 +13,10 @@ The stored value.
The storage key.
### `folder`
The folder path.
## Methods
### `init(value:)`

View File

@ -3,12 +3,15 @@
# `State`
## Methods
### `init(wrappedValue:_:)`
### `init(wrappedValue:_:folder:)`
Initialize a property representing a state in the view.
- Parameters:
- key: The unique storage key of the property.
- wrappedValue: The wrapped value.
- key: The unique storage key of the property.
- folder: The path to the folder containing the JSON file.
The folder path will be appended to the XDG data home directory.
### `checkFile()`

View File

@ -91,6 +91,8 @@ public struct State<Value>: StateProtocol {
public var value: Any
/// The storage key.
public var key: String?
/// The folder path.
public var folder: String?
/// Initialize the storage.
/// - Parameters:
@ -111,7 +113,9 @@ public struct State<Value>: StateProtocol {
/// Get the settings directory path.
/// - Returns: The path.
private func dirPath() -> URL {
NativePeer.getUserDataDirectory().appendingPathComponent(GTUIApp.appID, isDirectory: true)
NativePeer
.getUserDataDirectory()
.appendingPathComponent(content.storage.folder ?? GTUIApp.appID, isDirectory: true)
}
/// Get the settings file path.
@ -126,11 +130,15 @@ extension State where Value: Codable {
/// Initialize a property representing a state in the view.
/// - Parameters:
/// - key: The unique storage key of the property.
/// - wrappedValue: The wrapped value.
public init(wrappedValue: Value, _ key: String) {
/// - key: The unique storage key of the property.
/// - folder: The path to the folder containing the JSON file.
///
/// The folder path will be appended to the XDG data home directory.
public init(wrappedValue: Value, _ key: String, folder: String? = nil) {
content = .init(storage: .init(value: wrappedValue))
content.storage.key = key
content.storage.folder = folder
checkFile()
readValue()
self.writeValue = writeCodableValue

View File

@ -12,7 +12,7 @@ import Libadwaita
struct CounterDemo: View {
@State("count")
@State("count", folder: "io.github.AparokshaUI.Demo/count")
private var count = 0
var view: Body {

View File

@ -142,3 +142,8 @@ Use the following syntax, where `"text"` is a unique identifier.
```swift
@State("text") private var text = "world"
```
You can organize your content by specifying a custom folder path which will be appended to the XDG data home directory.
```swift
@State("text", folder: "io.github.david_swift.HelloWorld/my-view") private var text = "world"
```