Skip to content

Commit 206c535

Browse files
committed
Move seed to Wrapper type
1 parent b16f2c8 commit 206c535

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ will create a transition that animates the view from a random offset.
2020

2121
## Fixed Seeds
2222

23-
By default, different instances of the same `View` or `ViewModifier` generate unique `@Random` values. To ensure consistency, you can provide a custom seed via the property wrapper’s `projectedValue`.
23+
By default, different instances of the same `View` or `ViewModifier` generate unique `@Random` values. To ensure consistency, you can provide a custom seed via the property wrapper’s `projectedValue`'s `seed` value.
2424

2525
For example, assigning a model’s `hashValue` to `$greeting` ensures stability when the view gets recreated, say in a `LazyVStack`.
2626

@@ -32,7 +32,7 @@ struct Cell: View {
3232

3333
init(model: Model) {
3434
self.model = model
35-
$greeting = model.hashValue
35+
$greeting.seed = model.hashValue
3636
}
3737

3838
var body: some View {

Sources/AtRandom/AtRandom.swift

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,34 @@ import SwiftUI
44
/// invocations of ``View.body``.
55
@propertyWrapper
66
public struct Random<Value>: DynamicProperty {
7+
/// A wrapper of the underlying random number generator that can override
8+
/// the seed value used.
9+
public struct Wrapper {
10+
/// A value used by the property wrapper to generate the value.
11+
///
12+
/// Assign a value derived from your model object to this property to
13+
/// produce repetable random values across view instances.
14+
///
15+
/// ```swift
16+
/// struct Cell: View {
17+
/// @Random(in: "Hello", "Bonjour", "Willkommen") var greeting
18+
///
19+
/// var model: Model
20+
///
21+
/// init(model: Model) {
22+
/// self.model = model
23+
/// $greeting.seed = model.hashValue
24+
/// }
25+
///
26+
/// var body: some View {
27+
/// // The same `model` will get the same `greeting`, every time.
28+
/// Text("\(greeting), \(model.name)!")
29+
/// }
30+
/// }
31+
/// ```
32+
public var seed: Int
33+
}
34+
735
enum Source {
836
case fixed(Int)
937
case namespace
@@ -54,7 +82,7 @@ public struct Random<Value>: DynamicProperty {
5482
///
5583
/// init(model: Model) {
5684
/// self.model = model
57-
/// $greeting = model.hashValue
85+
/// $greeting.seed = model.hashValue
5886
/// }
5987
///
6088
/// var body: some View {
@@ -63,17 +91,17 @@ public struct Random<Value>: DynamicProperty {
6391
/// }
6492
/// }
6593
/// ```
66-
public var projectedValue: Int {
94+
public var projectedValue: Wrapper {
6795
get {
6896
switch source {
6997
case .fixed(let int):
70-
int
98+
.init(seed: int)
7199
case .namespace:
72-
namespace.hashValue
100+
.init(seed: namespace.hashValue)
73101
}
74102
}
75103
set {
76-
source = .fixed(newValue)
104+
source = .fixed(newValue.seed)
77105
}
78106
}
79107
}

0 commit comments

Comments
 (0)