Slide 1

Slide 1 text

Prepping For A SwiftUI Future Veronica Ray, Senior Software Engineer at Compass

Slide 2

Slide 2 text

Flux Missing features Functional reactive programming MVVM Combine Testing Declarative Black box Less code to write Better performance App architecture SwiftUI Fun Composable Architecture Interoperability Missing documentation Collaboration with designers

Slide 3

Slide 3 text

Preparing Your Codebase

Slide 4

Slide 4 text

What App Architecture Is Best For SwiftUI? • MVVM • Flux • The Composable Architecture

Slide 5

Slide 5 text

Animal Crossing Creators • Creators post IDs online that you use to download outfits • There’s no way to save these IDs in the game • Animal Crossing Creators allows you to save your favorite creators

Slide 6

Slide 6 text

Hat tip to MA-6549-3814-5086 for this amazing outfit!

Slide 7

Slide 7 text

Why Use MVVM? • The “default” choice for SwiftUI • Many UIKit apps already use this successfully

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Managing State

Slide 10

Slide 10 text

Why Use Flux? • Small amount of code needed • 3rd party libraries for it are lightweight

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Why Use Composable Architecture? • Inspired by Flux, but more opinionated and heavyweight • Open source library with great documentation and examples • Example apps covering many domains • Videos showing thought process

Slide 13

Slide 13 text

Flux Pain Points

Slide 14

Slide 14 text

Views Contain The Data Needed To Render Themselves And Their Children

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

var body: some View { WithViewStore(self.store.scope(state: \.view)) { viewStore in

Slide 17

Slide 17 text

extension AppState { var view: AppView.ViewState { .init(editMode: self.editMode) } }

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Once You Go iOS 13+ Only

Slide 20

Slide 20 text

Write All New Code In SwiftUI

Slide 21

Slide 21 text

A Pragmatic Approach • Don’t need a big rewrite of existing app • Take advantage of the faster development speed of SwiftUI in new projects • Need to master the art of interoperability with UIKit

Slide 22

Slide 22 text

If You’re Already Doing FRP

Slide 23

Slide 23 text

You Can Adopt SwiftUI Without Combine

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Downsides Of Not Adopting Combine

Slide 26

Slide 26 text

https://github.com/CombineCommunity/rxswift-to-combine-cheatsheet/blob/master/Data/operators.csv

Slide 27

Slide 27 text

https://github.com/quickbirdstudios/CombineRxSwiftPerformance

Slide 28

Slide 28 text

Can’t Follow Standard SwiftUI Resources

Slide 29

Slide 29 text

Interoperability Spikes

Slide 30

Slide 30 text

Using SwiftUI Views Within UIKit Is Easy

Slide 31

Slide 31 text

private let backButton = UIHostingController(rootView: SearchBarBackButton())

Slide 32

Slide 32 text

Using UIKit Within SwiftUI Is NOT Easy

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Step 1: UIViewRepresentable

Slide 35

Slide 35 text

public struct LabelRepresentation: UIViewRepresentable { var textColor: UIColor var font: UIFont var text: String public func makeUIView(context: UIViewRepresentableContext) -> Label { let label = Label() label.textColor = textColor label.font = font return label } public func updateUIView(_ uiView: Label, context: UIViewRepresentableContext) { uiView.text = text } }

Slide 36

Slide 36 text

public struct LabelRepresentation: UIViewRepresentable { var textColor: UIColor var font: UIFont var text: String public func makeUIView(context: UIViewRepresentableContext) -> Label { let label = Label() label.textColor = textColor label.font = font return label } public func updateUIView(_ uiView: Label, context: UIViewRepresentableContext) { uiView.text = text } }

Slide 37

Slide 37 text

public struct LabelRepresentation: UIViewRepresentable { var textColor: UIColor var font: UIFont var text: String public func makeUIView(context: UIViewRepresentableContext) -> Label { let label = Label() label.textColor = textColor label.font = font return label } public func updateUIView(_ uiView: Label, context: UIViewRepresentableContext) { uiView.text = text } }

Slide 38

Slide 38 text

public struct LabelRepresentation: UIViewRepresentable { var textColor: UIColor var font: UIFont var text: String public func makeUIView(context: UIViewRepresentableContext) -> Label { let label = Label() label.textColor = textColor label.font = font return label } public func updateUIView(_ uiView: Label, context: UIViewRepresentableContext) { uiView.text = text } }

Slide 39

Slide 39 text

Step 2: Add UIRepresentable View To SwiftUI View

Slide 40

Slide 40 text

public struct PageIndicatorView: View { @ObservedObject var model: PageIndicatorModel public var body: some View { LabelRepresentation(textColor: .white, font: .compassSansMedium(12), text: model.text) } }

Slide 41

Slide 41 text

Step 3: UIHostingController

Slide 42

Slide 42 text

imageCarouselView.didScrollHandler.subscribe(onNext: { [weak self] _ in guard self?.images.count ?? 0 > 1 else { return } self?.pageIndicatorView.rootView.model.index = (self?.currentPageIndex ?? 0) + 1 self?.pageIndicatorView.rootView.model.count = self?.images.count ?? 0 })

Slide 43

Slide 43 text

Summary • Decide now if you want to adopt a new app architecture • When you can, write all new code in SwiftUI • Adopt SwiftUI and Combine together unless you have a great reason to not use Combine • Do interoperability spikes in your production app