Slide 1

Slide 1 text

LITHO: BEST PRACTICES FOR BUILDING EFFICIENT UI Sergey Ryabov Facebook

Slide 2

Slide 2 text

WHAT IS LITHO

Slide 3

Slide 3 text

WHAT IS LITHO

Slide 4

Slide 4 text

WHAT IS LITHO Declarative API

Slide 5

Slide 5 text

WHAT IS LITHO Declarative API Asynchronous layout

Slide 6

Slide 6 text

WHAT IS LITHO Declarative API Asynchronous layout Flatter view hierarchies

Slide 7

Slide 7 text

WHAT IS LITHO Declarative API Asynchronous layout Flatter view hierarchies Fine-grained recycling

Slide 8

Slide 8 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 9

Slide 9 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 10

Slide 10 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 11

Slide 11 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 12

Slide 12 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 13

Slide 13 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 14

Slide 14 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 15

Slide 15 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 16

Slide 16 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } class MainActivity : AppCompatActivity() { override fun onCreate(state: Bundle?) { super.onCreate(savedInstanceState) val c = ComponentContext(this) setContentView(LithoView.create(c, ListItem.create(c) .title("Title") .subtitle("Subtitle") .build())) } }

Slide 17

Slide 17 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } class MainActivity : AppCompatActivity() { override fun onCreate(state: Bundle?) { super.onCreate(savedInstanceState) val c = ComponentContext(this) setContentView(LithoView.create(c, ListItem.create(c) .title("Title") .subtitle("Subtitle") .build())) } }

Slide 18

Slide 18 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } class MainActivity : AppCompatActivity() { override fun onCreate(state: Bundle?) { super.onCreate(savedInstanceState) val c = ComponentContext(this) setContentView(LithoView.create(c, ListItem.create(c) .title("Title") .subtitle("Subtitle") .build())) } }

Slide 19

Slide 19 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } class MainActivity : AppCompatActivity() { override fun onCreate(state: Bundle?) { super.onCreate(savedInstanceState) val c = ComponentContext(this) setContentView(LithoView.create(c, ListItem.create(c) .title("Title") .subtitle("Subtitle") .build())) } }

Slide 20

Slide 20 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } class MainActivity : AppCompatActivity() { override fun onCreate(state: Bundle?) { super.onCreate(savedInstanceState) val c = ComponentContext(this) setContentView(LithoView.create(c, ListItem.create(c) .title("Title") .subtitle("Subtitle") .build())) } }

Slide 21

Slide 21 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 22

Slide 22 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } @LayoutSpec object ListItemWithImageSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop(resType=DRAWABLE) image: Drawable, @Prop title: String, @Prop subtitle: String): Component { return Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 23

Slide 23 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } @LayoutSpec object ListItemWithImageSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop(resType=DRAWABLE) image: Drawable, @Prop title: String, @Prop subtitle: String): Component { return Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 24

Slide 24 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } @LayoutSpec object ListItemWithImageSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop(resType=DRAWABLE) image: Drawable, @Prop title: String, @Prop subtitle: String): Component { return Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 25

Slide 25 text

@LayoutSpec object ListItemSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop title: String, @Prop subtitle: String ): Component { return Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } } @LayoutSpec object ListItemWithImageSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop(resType=DRAWABLE) image: Drawable, @Prop title: String, @Prop subtitle: String): Component { return Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 26

Slide 26 text

HOW DOES IT ALL WORK?

Slide 27

Slide 27 text

HOW DOES IT ALL WORK

Slide 28

Slide 28 text

HOW DOES IT ALL WORK 1. Create an Internal Tree

Slide 29

Slide 29 text

HOW DOES IT ALL WORK 1. Create an Internal Tree 2. Create LayoutState

Slide 30

Slide 30 text

HOW DOES IT ALL WORK 1. Create an Internal Tree 2. Create LayoutState 3. Mount LayoutState

Slide 31

Slide 31 text

@LayoutSpec object ListItemWithImageSpec { @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop(resType=DRAWABLE) image: Drawable, @Prop title: String, @Prop subtitle: String): Component { return Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 32

Slide 32 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 33

Slide 33 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } }

Slide 34

Slide 34 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } } ListItemWithImage

Slide 35

Slide 35 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } } Row ListItemWithImage

Slide 36

Slide 36 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } } Row ListItemWithImage

Slide 37

Slide 37 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build() } } Row ListItemWithImage Image

Slide 38

Slide 38 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build(). } } Row ListItemWithImage Image

Slide 39

Slide 39 text

@LayoutSpec object ListItemWithImageSpec { // ... Row.create(c) .child( Image.create(c) .drawable(image)) .child( ListItem.create(c) .title(title) .subtitle(subtitle)) .build(). } } Row ListItemWithImage Image ListItem

Slide 40

Slide 40 text

Row ListItemWithImage Image ListItem ListItem.create(c) .title(title) .subtitle(subtitle)) .build(). } } @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 41

Slide 41 text

Row ListItemWithImage Image Column ListItem @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 42

Slide 42 text

Row ListItemWithImage Image Column ListItem @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 43

Slide 43 text

Row ListItemWithImage Image Column ListItem Text @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 44

Slide 44 text

Row ListItemWithImage Image Column ListItem Text @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 45

Slide 45 text

Row ListItemWithImage Image Column ListItem Text Text @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 46

Slide 46 text

Row ListItemWithImage Image Column ListItem Text Text @LayoutSpec object ListItemSpec { // ... Column.create(c) .child( Text.create(c) .text(title)) .child( Text.create(c) .text(subtitle)) .build() } }

Slide 47

Slide 47 text

CREATE LAYOUTSTATE Row Image Column Text Text

Slide 48

Slide 48 text

CREATE LAYOUTSTATE Measure with Yoga Row Image Column Text Text

Slide 49

Slide 49 text

CREATE LAYOUTSTATE Measure with Yoga Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 50

Slide 50 text

CREATE LAYOUTSTATE Measure with Yoga Collect a list of items to be drawn and their positions Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 51

Slide 51 text

Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 52

Slide 52 text

Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Root

Slide 53

Slide 53 text

Root Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 54

Slide 54 text

Root Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 55

Slide 55 text

Root Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 56

Slide 56 text

Root Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 57

Slide 57 text

Root Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 58

Slide 58 text

Root Text Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 59

Slide 59 text

Root Text Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80)

Slide 60

Slide 60 text

Root Text Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Text

Slide 61

Slide 61 text

Root Text Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Text Im ageDraw able TextDraw able TextDraw able

Slide 62

Slide 62 text

Root Text Image Row Image Column Text Text (0, 0 - 640, 96) (88, 0 - 640, 96) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Text Im ageDraw able TextDraw able TextDraw able LayoutState

Slide 63

Slide 63 text

MOUNT LAYOUTSTATE Root Text Image Text Im ageDraw able TextDraw able TextDraw able LayoutState

Slide 64

Slide 64 text

MOUNT LAYOUTSTATE The only step that always happens on UI thread Root Text Image Text Im ageDraw able TextDraw able TextDraw able LayoutState

Slide 65

Slide 65 text

Root Text Image Text Im ageDraw able TextDraw able TextDraw able LayoutState Row Image Column Text Text

Slide 66

Slide 66 text

@MountSpec object GradientSpec { @OnCreateMountContent fun onCreateMountContent(context: Context): GradientDrawable { return GradientDrawable( ) } @OnMount fun onMount( c: ComponentContext, drawable: GradientDrawable, @Prop @ColorInt colors: IntArray) { drawable.colors = colors } // ... }

Slide 67

Slide 67 text

@MountSpec object GradientSpec { @OnCreateMountContent fun onCreateMountContent(context: Context): GradientDrawable { return GradientDrawable( ) } @OnMount fun onMount( c: ComponentContext, drawable: GradientDrawable, @Prop @ColorInt colors: IntArray) { drawable.colors = colors } // ... }

Slide 68

Slide 68 text

@MountSpec object GradientSpec { @OnCreateMountContent fun onCreateMountContent(context: Context): GradientDrawable { return GradientDrawable( ) } @OnMount fun.onMount( c: ComponentContext, drawable: GradientDrawable, @Prop @ColorInt colors: IntArray) { drawable.colors = colors } // ... }

Slide 69

Slide 69 text

@MountSpec object GradientSpec { @OnCreateMountContent fun onCreateMountContent(context: Context): GradientDrawable { return GradientDrawable( ) } @OnMount fun.onMount( c: ComponentContext, drawable: GradientDrawable, @Prop @ColorInt colors: IntArray) { drawable.colors = colors } // ... }

Slide 70

Slide 70 text

public @interface MountSpec { int poolSize() default.3; boolean isPureRender() default false; }

Slide 71

Slide 71 text

public @interface MountSpec { int poolSize() default.3; boolean isPureRender() default false; }

Slide 72

Slide 72 text

public @interface MountSpec { int poolSize() default.3; boolean isPureRender() default false; }

Slide 73

Slide 73 text

public @interface MountSpec { int poolSize() default 3; boolean isPureRender() default false; } @MountSpec(poolSize = 30, isPureRender = true) class ImageSpec {

Slide 74

Slide 74 text

OPTIMIZATIONS

Slide 75

Slide 75 text

OPTIMIZATIONS Layout/Mount Diffing

Slide 76

Slide 76 text

OPTIMIZATIONS Layout/Mount Diffing • Reuse Measurements

Slide 77

Slide 77 text

OPTIMIZATIONS Layout/Mount Diffing • Reuse Measurements • Reuse LayoutOutputs

Slide 78

Slide 78 text

Text Img Text (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Internal Tree Diff Tree

Slide 79

Slide 79 text

Text Img Text (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Internal Tree Diff Tree

Slide 80

Slide 80 text

Text Img Text (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Internal Tree Diff Tree

Slide 81

Slide 81 text

Text Img Text (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Internal Tree Diff Tree

Slide 82

Slide 82 text

(16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Text Text Img (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Root LayoutState Internal Tree Diff Tree

Slide 83

Slide 83 text

(16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) (16, 16 - 80, 80) (88, 16 - 624, 48) (88, 48 - 624, 80) Root Text Text Image LayoutState Internal Tree Diff Tree

Slide 84

Slide 84 text

OPTIMIZATIONS Layout/Mount Diffing • Reuse Measurements • Reuse LayoutOutputs

Slide 85

Slide 85 text

OPTIMIZATIONS Layout/Mount Diffing • Reuse Measurements • Reuse LayoutOutputs IncrementalMount

Slide 86

Slide 86 text

INCREMENTAL MOUNT

Slide 87

Slide 87 text

INCREMENTAL MOUNT

Slide 88

Slide 88 text

INCREMENTAL MOUNT

Slide 89

Slide 89 text

INCREMENTAL MOUNT

Slide 90

Slide 90 text

INCREMENTAL MOUNT

Slide 91

Slide 91 text

INCREMENTAL MOUNT

Slide 92

Slide 92 text

GOTCHAS

Slide 93

Slide 93 text

GOTCHAS Use isPureRender / @ShouldUpdate to reuse previous layout results

Slide 94

Slide 94 text

GOTCHAS Use isPureRender / @ShouldUpdate to reuse previous layout results Tweak MountPools according to you requirements

Slide 95

Slide 95 text

MANAGING STATE

Slide 96

Slide 96 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State count: Int): Component { return Column.create(c) .child( Text.create(c) .text("$count") ) .child( Text.create(c) .text("+") ) .build() }

Slide 97

Slide 97 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State count: Int): Component { return Column.create(c) .child( Text.create(c) .text("$count") ) .child( Text.create(c) .text("+") ) .build() }

Slide 98

Slide 98 text


 
 @OnUpdateState fun increment(count: StateValue) { count.set(count.get() + 1) }

Slide 99

Slide 99 text


 @OnUpdateState fun increment(count: StateValue) { count.set(count.get() + 1) }
 
 @OnEvent(ClickEvent::class) fun.onIncrement(c: ComponentContext) { RootComponent.increment(c) }

Slide 100

Slide 100 text


 @OnUpdateState fun increment(count: StateValue) { count.set(count.get() + 1) }
 
 @OnEvent(ClickEvent::class) fun.onIncrement(c: ComponentContext) { RootComponent.increment(c) } // ... .child( Text.create(c) .text("+") .clickHandler(RootComponent.onIncrement(c)) )

Slide 101

Slide 101 text

@OnUpdateState fun increment(count: StateValue) { count.set(count.get() + 1) }
 
 @OnEvent(ClickEvent::class) fun.onIncrement(c: ComponentContext) { RootComponent.increment(c) }

Slide 102

Slide 102 text

@OnUpdateState fun increment(count: StateValue) { count.set(count.get() + 1) }
 
 RootComponent.increment(c)

Slide 103

Slide 103 text

@OnUpdateState fun increment(count: StateValue) { count.set(count.get() + 1) }
 
 RootComponent.increment(c) 
 RootComponent.incrementSync(c)

Slide 104

Slide 104 text

State updates? I'll take two!

Slide 105

Slide 105 text

@OnUpdateState fun changeColor(color: StateValue, @Param newColor: Color) { color.set(newColor) } @OnUpdateState fun changeName(name: StateValue, @Param newName: String) { name.set(newName) }

Slide 106

Slide 106 text

@OnUpdateState fun changeColor(color: StateValue, @Param newColor: Color) { color.set(newColor) } @OnUpdateState fun changeName(name: StateValue, @Param newName: String) { name.set(newName) } ... ... RootComponent.changeColor(c, Color.BLUE) RootComponent.changeName(c, "Plum")

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

@OnUpdateState fun changeColor(color: StateValue, @Param newColor: Color) { color.set(newColor) } @OnUpdateState fun changeName(name: StateValue, @Param newName: String) { name.set(newName) } ... ... RootComponent.changeColor(c, Color.BLUE) RootComponent.changeName(c, "Plum")

Slide 111

Slide 111 text

@OnUpdateState fun changeFruit( color: StateValue, name: StateValue, @Param newColor: Color, @Param newName: String) { color.set(newColor) name.set(newName) } ... ... RootComponent.changeFruit(c, Color.BLUE, "Plum")

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

What if State is not tied to rendering?

Slide 115

Slide 115 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State count: Int, ): Component { return Column.create(c) // ... .build() }

Slide 116

Slide 116 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State count: Int, @State(canUpdateLazily = true) step: Int ): Component { return Column.create(c) // ... .child( TextInput.create(c).initialText("$step")) .build() }

Slide 117

Slide 117 text

@State(canUpdateLazily = true) step: Int RootComponent.lazyUpdateStep(c, value)

Slide 118

Slide 118 text

@State(canUpdateLazily = true) step: Int RootComponent.lazyUpdateStep(c, value)

Slide 119

Slide 119 text

Can I use State for caching?

Slide 120

Slide 120 text

@OnCalculateCachedValue(name = "heavyConfig") fun onCalculateHeavyConfig(@Prop prop: Long): HeavyConfig { return calculateHeavyConfig(prop) } @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop prop: Long, @CachedValue heavyConfig: HeavyConfig @State state: Boolean): Component { return ... }

Slide 121

Slide 121 text

@OnCalculateCachedValue(name = "heavyConfig") fun onCalculateHeavyConfig(@Prop prop: Long): HeavyConfig { return calculateHeavyConfig(prop) } @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop prop: Long, @CachedValue heavyConfig: HeavyConfig @State state: Boolean): Component { return ... }

Slide 122

Slide 122 text

GOTCHAS

Slide 123

Slide 123 text

GOTCHAS Batch your State updates

Slide 124

Slide 124 text

GOTCHAS Batch your State updates Use lazy State updates where possible

Slide 125

Slide 125 text

GOTCHAS Batch your State updates Use lazy State updates where possible Use CachedValues for heavy lifting

Slide 126

Slide 126 text

ANIMATE ALL THE THINGS

Slide 127

Slide 127 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State alignToEnd: Boolean): Component { val align = if (alignToEnd) FLEX_END else FLEX_START return Column.create(c) .justifyContent(CENTER) .child( Button.create(c) .text("Catch me!") .alignSelf(align) .clickHandler(RootComponent.onClick(c)) ) .build() }

Slide 128

Slide 128 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State alignToEnd: Boolean): Component { val align = if (alignToEnd) FLEX_END else FLEX_START return Column.create(c) .justifyContent(CENTER) .child( Button.create(c) .text("Catch me!") .transitionKey("button") .alignSelf(align) .clickHandler(RootComponent.onClick(c)) ) .build() }

Slide 129

Slide 129 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State alignToEnd: Boolean): Component { val align = if (alignToEnd) FLEX_END else FLEX_START return Column.create(c) .justifyContent(CENTER) .child( Button.create(c) .text("Catch me!") .transitionKey("button") .alignSelf(align) .clickHandler(RootComponent.onClick(c)) ) .build() } @OnCreateTransition fun.onCreateTransition(c: ComponentContext): Transition { return.Transition.create("button") .animate(AnimatedProperties.X) }

Slide 130

Slide 130 text

@OnCreateLayout fun onCreateLayout( c: ComponentContext, @State alignToEnd: Boolean): Component { val align = if (alignToEnd) FLEX_END else FLEX_START return Column.create(c) .justifyContent(CENTER) .child( Button.create(c) .text("Catch me!") .transitionKey("button") .alignSelf(align) .clickHandler(RootComponent.onClick(c)) ) .build() } @OnCreateTransition fun.onCreateTransition(c: ComponentContext): Transition { return.Transition.create("button") .animate(AnimatedProperties.X) }

Slide 131

Slide 131 text

@OnCreateTransition fun.onCreateTransition(c: ComponentContext): Transition { return.Transition.create("button") .animate(AnimatedProperties.X) }

Slide 132

Slide 132 text

@OnCreateTransition fun.onCreateTransition(c: ComponentContext): Transition { return.Transition.allLayout() }

Slide 133

Slide 133 text

ANIMATIONS

Slide 134

Slide 134 text

ANIMATIONS onCreateTransition is called on every onCreateLayout

Slide 135

Slide 135 text

ANIMATIONS onCreateTransition is called on every onCreateLayout Even if you don't need to animate that specific UI update

Slide 136

Slide 136 text

ANIMATIONS onCreateTransition is called on every onCreateLayout Even if you don't need to animate that specific UI update More granular control?

Slide 137

Slide 137 text

@OnCreateTransition fun onCreateTransition(c: ComponentContext): Transition { return Transition.allLayout() }

Slide 138

Slide 138 text

@OnCreateTransition fun onCreateTransition(c: ComponentContext, @Prop prop: Diff, @State state: Diff): Transition? { return if (shouldAnimate(prop, state)) Transition.allLayout() else null }

Slide 139

Slide 139 text

@OnUpdateState fun updateAlignment(alignToEnd: StateValue) { alignToEnd.set(!alignToEnd.get()) }

Slide 140

Slide 140 text

@OnUpdateStateWithTransition fun updateAlignment(alignToEnd: StateValue): Transition { alignToEnd.set(!alignToEnd.get()) return Transition.create("button") .animate(AnimatedProperties.X) }

Slide 141

Slide 141 text

GOTCHAS

Slide 142

Slide 142 text

GOTCHAS Use Diff for more granular control over transitions

Slide 143

Slide 143 text

GOTCHAS Use Diff for more granular control over transitions Can use @OnUpdateStateWithTransition if animating only State changes

Slide 144

Slide 144 text

GRADUAL ADOPTION

Slide 145

Slide 145 text

GRADUAL ADOPTION

Slide 146

Slide 146 text

GRADUAL ADOPTION Can't migrate the whole screen at once? Do step by step:

Slide 147

Slide 147 text

GRADUAL ADOPTION Can't migrate the whole screen at once? Do step by step: • Complex UI parts can be replaced with a LithoView and underlying Component structure

Slide 148

Slide 148 text

GRADUAL ADOPTION Can't migrate the whole screen at once? Do step by step: • Complex UI parts can be replaced with a LithoView and underlying Component structure • Custom Views can be wrapped in MountSpecs

Slide 149

Slide 149 text

ADVANCED CONFIGURATION

Slide 150

Slide 150 text

COMPONENTTREE BUILDER

Slide 151

Slide 151 text

COMPONENTTREE BUILDER Set of advanced settings val tree = ComponentTree.create(c) // ... .build() lithoView.componentTree = tree

Slide 152

Slide 152 text

COMPONENTTREE BUILDER Set of advanced settings • LayoutHandler val tree = ComponentTree.create(c) .layoutThreadHandler(lithoHandler) .build() lithoView.componentTree = tree

Slide 153

Slide 153 text

COMPONENTTREE BUILDER Set of advanced settings • LayoutHandler • Reconciliation val tree = ComponentTree.create(c) .isReconciliationEnabled(true) .build() lithoView.componentTree = tree

Slide 154

Slide 154 text

SOMETHING DOESN'T WORK!

Slide 155

Slide 155 text

TOOLS

Slide 156

Slide 156 text

TOOLS Yoga Playground

Slide 157

Slide 157 text

yogalayout.com/playground

Slide 158

Slide 158 text

TOOLS Yoga Playground Flipper + Layout plugin

Slide 159

Slide 159 text

fbflipper.com

Slide 160

Slide 160 text

TOOLS Yoga Playground Flipper + Layout plugin Litho IntelliJ Plugin (Beta)

Slide 161

Slide 161 text

No content

Slide 162

Slide 162 text

github.com/facebook/litho/tree/master/litho-intellij-plugin

Slide 163

Slide 163 text

TOOLS Yoga Playground Flipper + Layout plugin Litho IntelliJ Plugin (Beta) Lithography Sample app

Slide 164

Slide 164 text

No content

Slide 165

Slide 165 text

RESOURCES Official Docs: fblitho.com Intro to Litho: youtu.be/uzCK4Vnme7o Lithography Sample app: github.com/facebook/litho/tree/master/sample Litho Codelabs (early preview): github.com/facebook/litho/tree/master/codelabs Dive into Litho Threading Model: youtu.be/78BUH1LZaZ4

Slide 166

Slide 166 text

THANK YOU Sergey Ryabov @colriot