diff --git a/Documentation/Reference/classes/State.Storage.md b/Documentation/Reference/classes/State.Storage.md index e9a378a..c284024 100644 --- a/Documentation/Reference/classes/State.Storage.md +++ b/Documentation/Reference/classes/State.Storage.md @@ -13,6 +13,10 @@ The stored value. The storage key. +### `folder` + +The folder path. + ## Methods ### `init(value:)` diff --git a/Documentation/Reference/extensions/State.md b/Documentation/Reference/extensions/State.md index b83e9ae..5e237e9 100644 --- a/Documentation/Reference/extensions/State.md +++ b/Documentation/Reference/extensions/State.md @@ -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()` diff --git a/Sources/Adwaita/Model/Data Flow/State.swift b/Sources/Adwaita/Model/Data Flow/State.swift index 9f1d5f7..9b4738c 100644 --- a/Sources/Adwaita/Model/Data Flow/State.swift +++ b/Sources/Adwaita/Model/Data Flow/State.swift @@ -91,6 +91,8 @@ public struct State: 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: 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 diff --git a/Tests/CounterDemo.swift b/Tests/CounterDemo.swift index 2ddb674..4b82cf4 100644 --- a/Tests/CounterDemo.swift +++ b/Tests/CounterDemo.swift @@ -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 { diff --git a/user-manual/Basics/CreatingViews.md b/user-manual/Basics/CreatingViews.md index 33741c9..d9fbfe7 100644 --- a/user-manual/Basics/CreatingViews.md +++ b/user-manual/Basics/CreatingViews.md @@ -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" +```