Skip to content

Latest commit

 

History

History
58 lines (48 loc) · 1.77 KB

File metadata and controls

58 lines (48 loc) · 1.77 KB

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