Skip to content

Commit 642a9ec

Browse files
committed
Initial commit
0 parents  commit 642a9ec

File tree

6 files changed

+447
-0
lines changed

6 files changed

+447
-0
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [robb]

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Robert Böhnke
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Package.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version: 6.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "AtRandom",
8+
platforms: [
9+
.iOS(.v16),
10+
.macOS(.v13),
11+
.macCatalyst(.v16),
12+
.watchOS(.v9),
13+
.tvOS(.v16),
14+
.visionOS(.v1),
15+
],
16+
products: [
17+
// Products define the executables and libraries a package produces, making them visible to other packages.
18+
.library(
19+
name: "AtRandom",
20+
targets: ["AtRandom"]),
21+
],
22+
targets: [
23+
// Targets are the basic building blocks of a package, defining a module or a test suite.
24+
// Targets can depend on other targets in this package and products from dependencies.
25+
.target(
26+
name: "AtRandom"),
27+
]
28+
)

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# @Random
2+
3+
A property wrapper that assigns a stable random value to a member variable, persisting across multiple `body` invocations.
4+
5+
Use this to introduce variation in animations without relying on `@State`.
6+
7+
For example
8+
9+
```swift
10+
struct RandomOffsetTransition: Transition {
11+
@Random(distance: 20) var offset: CGSize
12+
13+
func body(content: Content, phase: TransitionPhase) -> some View {
14+
content.offset(offset)
15+
}
16+
}
17+
```
18+
19+
will create a transition that animates the view from a random offset.
20+
21+
## Fixed Seeds
22+
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`.
24+
25+
For example, assigning a model’s `hashValue` to `$greeting` ensures stability when the view gets recreated, say in a `LazyVStack`.
26+
27+
```swift
28+
struct Cell: View {
29+
@Random(in: "Hello", "Bonjour", "Willkommen") var greeting
30+
31+
var model: Model
32+
33+
init(model: Model) {
34+
self.model = model
35+
$greeting = model.hashValue
36+
}
37+
38+
var body: some View {
39+
// The same `model` will get the same `greeting`, every time.
40+
Text("\(greeting), \(model.name)!")
41+
}
42+
}
43+
```
44+
45+
This way, each `Cell` instance with the same model will always produce the same greeting.
46+
47+
> [!IMPORTANT]
48+
> Some Swift types, like `String`, generate different `hashValues` across app launches. For values that persist _across_ launches, use a stable identifier, such as a user ID.

0 commit comments

Comments
 (0)