forked from aparoksha/adwaita-swift
Add option to manually provide directory for state
This commit is contained in:
parent
da29f92c91
commit
03d259811b
@ -13,6 +13,10 @@ The stored value.
|
|||||||
|
|
||||||
The storage key.
|
The storage key.
|
||||||
|
|
||||||
|
### `folder`
|
||||||
|
|
||||||
|
The folder path.
|
||||||
|
|
||||||
## Methods
|
## Methods
|
||||||
### `init(value:)`
|
### `init(value:)`
|
||||||
|
|
||||||
|
|||||||
@ -3,12 +3,15 @@
|
|||||||
# `State`
|
# `State`
|
||||||
|
|
||||||
## Methods
|
## Methods
|
||||||
### `init(wrappedValue:_:)`
|
### `init(wrappedValue:_:folder:)`
|
||||||
|
|
||||||
Initialize a property representing a state in the view.
|
Initialize a property representing a state in the view.
|
||||||
- Parameters:
|
- Parameters:
|
||||||
- key: The unique storage key of the property.
|
|
||||||
- wrappedValue: The wrapped value.
|
- 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()`
|
### `checkFile()`
|
||||||
|
|
||||||
|
|||||||
@ -91,6 +91,8 @@ public struct State<Value>: StateProtocol {
|
|||||||
public var value: Any
|
public var value: Any
|
||||||
/// The storage key.
|
/// The storage key.
|
||||||
public var key: String?
|
public var key: String?
|
||||||
|
/// The folder path.
|
||||||
|
public var folder: String?
|
||||||
|
|
||||||
/// Initialize the storage.
|
/// Initialize the storage.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
@ -111,7 +113,9 @@ public struct State<Value>: StateProtocol {
|
|||||||
/// Get the settings directory path.
|
/// Get the settings directory path.
|
||||||
/// - Returns: The path.
|
/// - Returns: The path.
|
||||||
private func dirPath() -> URL {
|
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.
|
/// Get the settings file path.
|
||||||
@ -126,11 +130,15 @@ extension State where Value: Codable {
|
|||||||
|
|
||||||
/// Initialize a property representing a state in the view.
|
/// Initialize a property representing a state in the view.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - key: The unique storage key of the property.
|
|
||||||
/// - wrappedValue: The wrapped value.
|
/// - 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 = .init(storage: .init(value: wrappedValue))
|
||||||
content.storage.key = key
|
content.storage.key = key
|
||||||
|
content.storage.folder = folder
|
||||||
checkFile()
|
checkFile()
|
||||||
readValue()
|
readValue()
|
||||||
self.writeValue = writeCodableValue
|
self.writeValue = writeCodableValue
|
||||||
|
|||||||
@ -12,7 +12,7 @@ import Libadwaita
|
|||||||
|
|
||||||
struct CounterDemo: View {
|
struct CounterDemo: View {
|
||||||
|
|
||||||
@State("count")
|
@State("count", folder: "io.github.AparokshaUI.Demo/count")
|
||||||
private var count = 0
|
private var count = 0
|
||||||
|
|
||||||
var view: Body {
|
var view: Body {
|
||||||
|
|||||||
@ -142,3 +142,8 @@ Use the following syntax, where `"text"` is a unique identifier.
|
|||||||
```swift
|
```swift
|
||||||
@State("text") private var text = "world"
|
@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"
|
||||||
|
```
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user