Giter Site home page Giter Site logo

swiftuitimetravel's Introduction

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


How this works

Any view hierarchy can use time travel: in the provided todo app, ContentView adds time travel like this:

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.

struct MyView: View {

    @EnvironmentObject var store: Store<TodoState>

    var body: some View {
        Text("Count: \(store.state.todoItems.count)")
    }
}

To update your application's state, use the dispatch method on the store:

struct MyView: View {

    @EnvironmentObject var store: Store<MyState>

    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<StateType> 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.

swiftuitimetravel's People

Contributors

timdonnelly avatar inamiy avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.