for SwiftUI 1 • Both use virtual views • Some experiments by Swift developers since 2017 (longer than SwiftUI) • Knowing Elm & TEA brings us insight into SwiftUI 1 https://medium.com/swlh/clean-architecture-for-swiftui-6d6c4eb1cf6a 6
my perspective) 2017 Inami presentations, gist about Elm inspired frameworks 2018 May: AppArchitecture book 2018 June: WWDC - no updates about virtual UI 2018 Dec: my experiments 2019 June: SwiftUI came 8
was a bit difficult for me to understand. Tried to understand them by making them. • yoching/SwiftElmButtonSample 6 • yoching/SwiftElmSample2 7 • mostly done in 12.2018 7 https://github.com/yoching/SwiftElmSample2 6 https://github.com/yoching/SwiftElmButtonSample 17
: Model init = 0 -- UPDATE type Msg = Increment | Decrement update : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1 20
value: Int // UPDATE enum Message { case increment, decrement } mutating func update(_ message: Message) { switch message { case .increment: value = value + 1 case .decrement: value = value - 1 } } ... 22
enum View<Message> { case _label(Label) case _button(Button<Message>) case _stackView(StackView<Message>) ... } • e.g. Virtual DOM (React) • (SwiftUI is basically virtual view) 24
Save button: save the value to UserDefaults (= side effect without callback) • Load button: load the value from UserDefaults (= side effect with callback) • Load value when app becomes active • Save value when app enters background 25
-> [Command<Message>] { switch message { case .increment: value = value + 1 return [] case .decrement: value = value - 1 return [] case .save: return [.save(value: value)] case .load: return [.load(available: { .loaded($0) })] case .loaded(let value): self.value = value return [] } } • need to prepare custom commands 28
new architecture, actions are often represented as types (usually enum) • e.g. actions are represented as functions/methods in other architectures like MVC, MVVM • easier to test, easier to handle because they are value types • Lots of architecture are going towards this direction 35