Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/timdonnelly/SwiftUITimeTravel
An experimental time traveling state store for SwiftUI
https://github.com/timdonnelly/SwiftUITimeTravel
Last synced: 2 months ago
JSON representation
An experimental time traveling state store for SwiftUI
- Host: GitHub
- URL: https://github.com/timdonnelly/SwiftUITimeTravel
- Owner: timdonnelly
- License: mit
- Created: 2019-06-05T08:24:40.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-10-31T04:54:13.000Z (about 5 years ago)
- Last Synced: 2024-10-28T11:45:21.823Z (3 months ago)
- Language: Swift
- Homepage:
- Size: 777 KB
- Stars: 141
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-swiftui - timdonnelly/SwiftUITimeTravel - An experimental time traveling state store for SwiftUI (Samples)
- fucking-about-SwiftUI - SwiftUITimeTravel
README
# SwiftUI Time Travel
A SwiftUI state store and view that allow you to scrub through an application's state.
This is a super rough prototype: it's only meant to serve as an example of what can be done using SwiftUI.
![animated gif](image.gif)
-----
### How this works
Any view hierarchy can use time travel: in the provided todo app, `ContentView` adds time travel like this:
```swift
struct ContentView : View {
var body: some View {
TimeTravelView(initialState: TodoState()) {
TodoListView()
}
}
}
```Then you can use `@EnvironmentObject` to access the state store from views within the hierarchy.
```swift
struct MyView: View {@EnvironmentObject var store: Store
var body: some View {
Text("Count: \(store.state.todoItems.count)")
}
}
```To update your application's state, use the `dispatch` method on the store:
```swift
struct MyView: View {@EnvironmentObject var store: Store
var body: some View {
VStack(spacing: 10) {
Text("Count: \(store.state.todoItems.count)")
HStack(spacing: 10) {
Button(action: { self.store.dispatch(event: .someEventName) }) {
Text("Do Something")
}
}
}
}
}
```For time travel to work, all state must be accessed like this. Any state that is stored outside of the single `Store` will not be controlled by the time travel view. This means that local view state (using `@State`) should be used sparingly (if at all).
Internally, the store keeps a stack of all states that have occured – the slider controls which of those states is used as the active (current) state.