Skip to content

yoching/SwiftElmButtonSample

Repository files navigation

Elm Button Sample implemented with Swift

This is a swift implementation of Buttons sample in Elm Official Guide.
For simplicity, commands and subscriptions are not included.(They are included in the v2.)
Also, incremental updates of views are not implemented.
(If you want to know them, please refer to referenced materials.)

I hope this will be a help to start learning The Elm Architecture with swift!

AppState

This corresponds to the Buttons sample in Elm Official Guide.

struct AppState {

    // MODEL
    var value: Int

    // UPDATE
    enum Message {
        case increment
        case decrement
    }

    mutating func update(_ message: Message) {
        switch message {
        case .increment:
            value = value + 1
        case .decrement:
            value = value - 1
        }
    }

    // VIEW
    var viewController: ViewController<Message> {
        return ._viewController(
            .stackView(
                views: [
                    .button(text: "-", onTap: .decrement),
                    .label(text: "\(value)"),
                    .button(text: "+", onTap: .increment)
                ],
                axis: .vertical,
                distriburtion: .fillEqually
            )
        )
    }
}

References

About

Elm Button Sample implemented with Swift

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages